Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KaiaKitten/ad4b6f22b192b5cc9884 to your computer and use it in GitHub Desktop.
Save KaiaKitten/ad4b6f22b192b5cc9884 to your computer and use it in GitHub Desktop.
const int LED=9;
const int BUTTON1=2;
const int BUTTON2=3;
const String CODE = "ABAB";
String rec = "";
String lastRec = "";
boolean lastButton1 = LOW;
boolean currentButton1 = LOW;
boolean lastButton2 = LOW;
boolean currentButton2 = LOW;
boolean ledOn = false;
void setup() {
Serial.begin(9600);
pinMode(LED,OUTPUT);
pinMode(BUTTON1,INPUT);
pinMode(BUTTON2,INPUT);
Serial.println("Hello World");
}
boolean debounce(boolean last, int button)
{
boolean current = digitalRead(button);
if(last != current)
{
delay(5);
current = digitalRead(button);
}
return current;
}
void loop() {
currentButton1 = debounce(lastButton1,BUTTON1);
if(lastButton1 == LOW && currentButton1 == HIGH)
{
rec+="A";
}
lastButton1 = currentButton1;
currentButton2 = debounce(lastButton2,BUTTON2);
if(lastButton2 == LOW && currentButton2 == HIGH)
{
rec+="B";
}
lastButton2 = currentButton2;
if(rec.endsWith(CODE))
{
ledOn = true;
}else
{
ledOn= false;
}
if(rec != "")
{
if(lastRec != rec) //only output rec if there is a change
{
Serial.println(rec);
lastRec = rec;
}
}
digitalWrite(LED,ledOn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment