Chapter 11: 3, 9
Question 3:
Describe the operation of the setDebounceTime library function and discuss why it required is?
Answer
Sets the amount of milliseconds to wait before accepting a new key press or key event.
If the setDebounceTime () function is not called, the default time is set to 10 ms.
Question 9:
Design an Arduino C program without comments that serially transmits at 9600 baud rate, the state of
the key when pressed The Arduino row pins A5, A4, 3 and 2 along with column pins 12, 11 and 10 are
connected to a standard 3 x 4 numeric matrix keypad Additionally, set the debounce time to 10 ms and
the key press hold time to 250 ms The following serially transmitted strings:: “PRESSED”, “HOLD”,
REASED” and “IDLE“must appear on separate lines as the different states occur?
Answer
#include <Keypad.h>
byte rowPins[4] = {A5, A4, 3, 2};
byte colPins[3] = {12, 11, 10}; char
keys[4][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
Keypad kyp = Keypad(makeKeymap(keys), rowPins, colPins, 4, 3);
volatile boolean blinkLED = false;
void setup() { Serial.begin(9600);
kyp.setDebounceTime(10);
kyp.setHoldTime(250);
pinMode(LED_BUILTIN, OUTPUT);
kyp.addEventListener(kypEvent);
}
void loop() { char ckey =
kyp.getKey(); if (ckey) {
Serial.println(ckey);
}
if (blinkLED) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(100);
}
}
void kypEvent(KeypadEvent key) {
switch (kyp.getState()) { case
PRESSED:
Serial.println("PRESSED");
break; case HOLD:
Serial.println("HOLD"); if (key
== '*') blinkLED = true;
break; case RELEASED:
Serial.println("RELEASED"); if
(key == '*') blinkLED = false;
break; case IDLE:
Serial.println("IDLE");
break;
}
}