Hi all!
I want use analog keypad for my project. I write this code to check buttons is pressed for short time or long time. I read analog pin and when change form 0 to another number( button is pressed) run a timer. when the button is release, check timer(for what the elapsed time) and button but...
I'm confused because short press not recognized
void key()
{
byte button;
//get the latest button pressed, also the buttonJustPressed, buttonJustReleased flags
button = ReadButtons();
// if the button state changes to pressed, remember the start time
if (button != 0 && previous == 0 && (millis() - firstTime) > 200) {
pressed_button = button;
firstTime = millis();
}
millis_held = (millis() - firstTime);
// This if statement is a basic debouncing tool, the button must be pushed for at least
// 20milliseconds in a row for it to be considered as a push.
if (millis_held > 20) {
// check if the button was released since we last checked
if (button == 0 && previous != 0) {
if (millis_held < 1000 && pressed_button == 1) {
lcd.clear();
lcd.print("short right");
pressed_button = 0;
delay (1000);
}
if (millis_held < 1000 && pressed_button == 2) {
lcd.clear();
lcd.print("short up");
pressed_button = 0;
delay (1000);
}
if (millis_held < 1000 && pressed_button == 3) {
lcd.clear();
lcd.print("short down");
pressed_button = 0;
delay (1000);
}
if (millis_held < 1000 && pressed_button == 4) {
lcd.clear();
lcd.print("short left");
pressed_button = 0;
delay (1000);
}
if (millis_held < 1000 && pressed_button == 5) {
lcd.clear();
lcd.print("short select");
pressed_button = 0;
delay (1000);
}
if (millis_held >= 1000 && pressed_button == 1) {
lcd.clear();
lcd.print("long right");
pressed_button = 0;
delay (1000);
}
if (millis_held >= 1000 && pressed_button == 2) {
lcd.clear();
lcd.print("long up");
pressed_button = 0;
delay (1000);
}
if (millis_held >= 1000 && pressed_button == 3) {
lcd.clear();
lcd.print("long down");
pressed_button = 0;
delay (1000);
}
if (millis_held >= 1000 && pressed_button == 4) {
lcd.clear();
lcd.print("long left");
pressed_button = 0;
delay (1000);
}
if (millis_held >= 1000 && pressed_button == 5) {
lcd.clear();
lcd.print("long select");
pressed_button = 0;
delay (1000);
}
}
}
previous = button;
prev_secs_held = secs_held;
}