Key de-bouncing in 4x4 matrix Keypad. I need the output to the 2 7-segment LED.
ID: 3873712 • Letter: K
Question
Key de-bouncing in 4x4 matrix Keypad. I need the output to the 2 7-segment LED. It will show last two number. For example if I press 5, it shows 5 on the right. If I keep press 5 again, it will show 5 on the left, 5 on the right. If I press 8, it will show 5 on the left, 8 on the right.
Please add key de-bouncing to my code to make it work.
void read(void) {
char left_val = 'n';
char right_val = 'n';
char next_val = readKeyPad();
while(1){
next_val = readKeyPad();
if (right_val == next_val || next_val == 'n'){
printChar(left_val, Left);
delay(FRAME_PERIOD);
printChar(right_val, Right);
delay(FRAME_PERIOD);
}
else {
left_val = right_val;
right_val = next_val;
}
}
}
Explanation / Answer
long lastDebounceTime = 0; // the last time the output led was toggled
long debounceDelay = FRAME_PERIOD; // the debounce time; increase if the output flickers
void read(void)
{
char left_val = 'n';
char right_val = 'n';
char next_val = readKeyPad();
char last_val = 'n';
while(1)
{
next_val = readKeyPad();
// If the switch changed, due to noise or pressing:
if (next_val != last_val)
{
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (next_val != right_val && next_val!='n')
{
left_val = right_val;
right_val = next_val ;
}
}
// Frame period is removed from here and added to debounceDelay
printChar(left_val, Left);
printChar(right_val, Right);
last_val = next_val ;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.