Hello dear makers!
I am currently trying to implement a button control using a single push button. The idea is, that the microcontroller detects the duration the button was being pushed down upon release. There should be three distinguishable states:
- held down to short
- held down for a short threshold
- held down for a long threshold
The detection works very well, however, I get multiple calls of the same event. So after a short push was detcted two more (or so) short pushes are detected. I've been looking through my code for about an hour now and I can't seem to find my mistake. Because by having a third state for being pushed down too short this should eliminate the button bouncing because even though there might be multiple presses registered they would be simply too short to cause an event.
I'd grately appreciate any help on this. maybe I just overlooked something.
Thank you in advance!
Jocobes
// --------------------------------------- relevant variables ---------------------------------------------
char last_button_state;
unsigned long button_timer;
enum button_presses{
LONG,
SHORT,
NONE
};
enum button_presses push_type = NONE;
// ------------------------------------------- main loop -------------------------------------------------
void loop()
{
if(detectPress()==1)
{
switch(push_type)
{
case LONG:
Serial.print("LONG\n");
break;
case SHORT:
Serial.print("SHORT\n");
break;
}
}
}
// --------------------------------------------- functions -----------------------------------------------------
int detectPress() //function that detects a long press
{
if(digitalRead(BUTTON_PIN) == HIGH) //if BTN is being pushed
{
switch(last_button_state)
{
case 0:
button_timer = millis(); //starting timer
last_button_state = 1; //setting last_button_state to on
return 0;
break;
case 1:
return 0;
break;
}
}
else
{
switch(last_button_state)
{
case 0:
return 0;
break;
case 1:
if((millis()-button_timer)>=LONG_PRESS_THRESHOLD)
{
push_type = LONG;
last_button_state = 0;
return 1;
}
else if((millis()-button_timer)>=SHORT_PRESS_THRESHOLD)
{
push_type = SHORT;
last_button_state = 0;
return 1;
}
else
return 0;
break;
}
}
}