Problem creating a safecode

Hello everyone,

I am creating a lock with my Arduino UNO with 3 different buttons. To unlock the safe (turning on a green led) you have to press a specific combination of buttons. The problem is that I dont know where to start.

When developping in c# I would just add a character to a string at each buttonpress and than check at the end of the sequence if my string matches with my "code" string. Any ideas how I can make this work?

My code:

const int buttonPinBlack = 2; // the number of the pushbutton pin
const int buttonPinBlue = 3;
const int buttonPinWhite = 4;

const int ledPinGreen = 6; // the number of the LED pin
const int ledPinRed = 6; // the number of the LED pin

// count is used to check that the buttons are pressed 4 times
int count = 0;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPinGreen, OUTPUT);
pinMode(ledPinRed, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPinBlack, INPUT);
pinMode(buttonPinBlue, INPUT);
pinMode(buttonPinWhite, INPUT);
}

void loop() {
if (digitalRead(buttonPinBlack) == HIGH) {
count ++;
delay(200);
}
if (digitalRead(buttonPinWhite) == HIGH) {
count ++;
delay(200);
}
if (digitalRead(buttonPinBlue) == HIGH) {
count ++;
delay(200);
}
if ( count == 4) {
digitalWrite(ledPinGreen, HIGH);
}
else {

}
}

The problem is that I dont know where to start.

Google "state machine"

Start with the state change detection example. You do NOT need to use delay().

You can count the changes. Whenever any switch is pressed, increment the counter.

Use the counter as an index into a character array, and store a character, followed by a NULL.

When the appropriate number of presses has happened, or every time there is a change, compare the assembled string to the known string. If they match, open the door.

Change these to INPUT_PULLUP to turn on the internal pullup resistor

pinMode(buttonPinBlack, INPUT);

Wire the buttons to connect to Gnd when pressed.
Change these to look for a LOW

if (digitalRead(buttonPinBlack) == HIGH) {

Keep track of your button presses in one variable. The order has to be black-white-blue,
then after a button press see if the prior order was correct, if so update the variable, and if not clear it.
Something like this pseudo code:

if (black pressed){
was variable == 000? yes, make it 001. no, make it 000
}
if (white pressed){
was variable == 001? yes, make it 010. no, make it 000
}
if (blue pressed){
was variable == 010? yes, make it 100. no, make it 000
}
if (variable == 100){
turn on green led,  hold it for a second or whatever
clear variable back to 000
}
else{ turn it off
}

PaulS:
Start with the state change detection example. You do NOT need to use delay().

You can count the changes. Whenever any switch is pressed, increment the counter.

Use the counter as an index into a character array, and store a character, followed by a NULL.

When the appropriate number of presses has happened, or every time there is a change, compare the assembled string to the known string. If they match, open the door.

Thanks! Now my buttonpress code is a lot more reliable, yet I am still puzzled about how I am going to check if the buttons are pressed in the right sequence. It’s my first day coding in C…

Sk1dz0r:
Thanks! Now my buttonpress code is a lot more reliable, yet I am still puzzled about how I am going to check if the buttons are pressed in the right sequence. It's my first day coding in C...

That's what the code does.

aarg:
That's what the code does.

...but only if you manage the sequence in one cycle of loop()

yet I am still puzzled about how I am going to check if the buttons are pressed in the right sequence.

Suppose you have three switches, since pressing buttons on your shirt isn't going to do any good.

Suppose that every time switch 1 is pressed you store a 1 and increment a counter.
Suppose that every time switch 2 is pressed you store a 2 and increment a counter.
Suppose that every time switch 3 is pressed you store a 3 and increment a counter.

Suppose that the array starts empty and the counter is 0.

Suppose that switch 1 is pressed. The array becomes "1" and the counter becomes 1.
Suppose that switch 3 is pressed. What does the array look like? What is the counter value?
Suppose that switch 2 is pressed. What does the array look like? What is the counter value?Suppose that switch 1 is pressed. What does the array look like? What is the counter value?

If you answered "1321" and 4 to the last two questions, you would be right. Now, can you see the order that the switches were pressed in?