Skip to content

Instantly share code, notes, and snippets.

@vindolin
Created September 1, 2014 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vindolin/f5aed3092eaefab55cf8 to your computer and use it in GitHub Desktop.
Save vindolin/f5aed3092eaefab55cf8 to your computer and use it in GitHub Desktop.
#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