Created
September 1, 2014 15:23
-
-
Save vindolin/f5aed3092eaefab55cf8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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