A question on how to do a button sequence detectuion was asked in a FB group about escape room technology. This can be done in many ways, even without any microcontrollers. What we’re looking for is a state machine. Going from one state to the next require that the previous state was set. Let’s try:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
//template by Hans Peter //embryonic.dk, November 2016 //define input buttons #define button1 3 #define button2 4 #define button3 5 #define button4 6 #define button5 7 #define button6 8 #define output 9 //a state counter char state=0; void setup() { //set the port directions (all should be input by default so this is for clarity) pinMode(button1, INPUT); pinMode(button2, INPUT); pinMode(button3, INPUT); pinMode(button4, INPUT); pinMode(button5, INPUT); pinMode(button6, INPUT); pinMode(output,OUTPUT); //set the output pin direction //enable pull-ups on inputs. this stabilize the signals digitalWrite(button1,HIGH); digitalWrite(button2,HIGH); digitalWrite(button3,HIGH); digitalWrite(button4,HIGH); digitalWrite(button5,HIGH); digitalWrite(button6,HIGH); digitalWrite(output,LOW); //make sure the output is not enabled when we start } void loop() { //now check the buttons and update the state accordingly if(digitalRead(button1)==0) //if the button is pressed { if(state==0) //check if the state is correct and we can advance to the next state=1; else if (state!=0) //else reset the whole state counter so we need to start over state=0; } //do the same thing for all the other buttons (as few lines as possible) if(digitalRead(button2)==0) { if(state==1) { state=2; } else if (state!=1) { state=0; } } //button 2 if(digitalRead(button3)==0) { if(state==2) { state=3; } else if (state!=2) { state=0; } } //button 3 if(digitalRead(button4)==0) { if(state==3) { state=4; } else if (state!=3) { state=0; } } //button 4 if(digitalRead(button5)==0) { if(state==4) { state=5; } else if (state!=4) { state=0; } } //button 5 if(digitalRead(button6)==0) { if(state==5) { state=6; } else if (state!=5) { state=0; } } //button 6 if(state==6) { digitalWrite(output, !digitalRead(output); // change the state of the output signal state=0 //reset the state } } |
Please be advised that I haven’t tested this code.
Use it on your own risk!
I suggest this quick fix:
void loop()
{
if(digitalRead(button1)==0) { if(state==0) state=1; else if(state!=1) state=0; }
if(digitalRead(button2)==0) { if(state==1) state=2; else if(state!=2) state=0; }
if(digitalRead(button3)==0) { if(state==2) state=3; else if(state!=3) state=0; }
if(digitalRead(button4)==0) { if(state==3) state=4; else if(state!=4) state=0; }
if(digitalRead(button5)==0) { if(state==4) state=5; else if(state!=5) state=0; }
if(digitalRead(button6)==0) { if(state==5) state=6; else if(state!=6) state=0; }
if(state==6)
{
digitalWrite(output,HIGH);
delay(1000);
digitalWrite(output,LOW);
state=0;
}
}
Thanks for the fix! This should avoid problems with debounce which I completely forgot to think about (I have only checked that the code is compiling 😉
I will update the code example, but I will keep unlock procedure – you are correct but my approach will only toggle the output state where you reset after a second.