Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#define DEBOUNCE_DELAY 100
class Button
{
public:
Button(int p_Pin) /*Constructor*/
{
l_Pin = p_Pin;
pinMode(l_Pin,INPUT);
l_Last_Read_Value = digitalRead(l_Pin);
l_Last_Read_Time = millis();
}
boolean Read()
{
boolean l_Current_Value;
unsigned long l_Current_Time;
boolean l_Return_Value;
l_Current_Time = millis();
l_Current_Value = digitalRead(l_Pin);
if (l_Current_Value == l_Last_Read_Value && (l_Current_Time - l_Last_Read_Time) > DEBOUNCE_DELAY)
l_Return_Value = l_Current_Value;
else
l_Return_Value = l_Last_Read_Value;
l_Last_Read_Value = l_Current_Value;
l_Last_Read_Time = l_Current_Time;
delay(1);
return l_Return_Value;
}
private:
byte l_Pin;
unsigned long l_Last_Read_Time ;
boolean l_Last_Read_Value;
};
# usage:
Button Btn_Start(START_BTN);
Button Btn_Select(SELECT_STATE_BTN);
void loop()
{
if(Btn_Start.Read() == HIGH)
{
while(Btn_Start.Read() != LOW); //wait for button release
//do something
}
if(Btn_Start.Btn_Select() == HIGH)
{
while(Btn_Start.Btn_Select() != LOW); //wait for button release
//do something else
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment