I want to make a box boxed with an electromagnet that opens after a specific button sequence is pressed.
However, I want the individual buttons to light up as they are pressed if the sequence is correct and clear (turn off) if the wrong button is pressed. Can anyone provide guidance on the programming aspect of this? Normally I would google it but I don't know what to google.
There are several tutorials on this site for reading digital inputs (for buttons) and digital outputs for LEDs. I would start there and get the basic logic working for performing the unlock. I would then, and only then, worry about adding the electromagnet.
Thanks, I was hoping to incorporate a button that both pushes and is an LED. I would assume that would be different than a normal input output situation. While I had a little help getting the bugs out of the code I was able to make a working proton pack so I have a little knowledge/experience.
jremington:
Google "arduino keypad" for starter tutorials. There are a couple that demonstrate use of a numeric password.
I found those, however I am not looking for a numerical input. Rather, someone presses the LED buttons and need to figure out the correct sequence that allows all of the lights to be lit up without clearing.
Thanks, I was trying out different things in the search function and found the below. Based upon the OPs description it appears that it is the EXACT application I am looking for.
Alright, now that I have all of the parts I need (I think) I was taking a hard look at the below code. I really don't know what I am doing on the coding side (pretty much the entire thing) but it appears that I am missing some set up values.
Correct me if I am wrong but I need to define the led and button pins. I also attempted to take this from a 7 digit code to a 4 so I may have some remnants left over. Can someone nudge me in the right direction?
int btnPin[] = { 0,4,3,2 };
int ledPin[] = { 0,11,10,9,8 };
int buttonState[] = { 0,0,0,0,0,0,0 };
int lastButtonState[] = { 0,0,0,0,0,0,0 };
int led = 0;
int btn = 0;
int i = 0;
int ctrl = 0;
int lastBtn = 0;
void setup() {
Serial.begin(9600);
Serial.println("Starting");
for (led = 8; led <= 13; led++) { pinMode(led, OUTPUT); }
for (btn = 2; btn <= 7; btn++) { pinMode(btn, INPUT); }
}
void loop() {
for (i = 1; i <= 6; i++) {
if (wasBtnPushed(i) > 0) {
if (wasBtnPushed(i) != lastBtn) {
Serial.print("last_btn_"); Serial.print(lastBtn); Serial.print(" curr_btn_"); Serial.print(i); Serial.print(" initial_ctrl_"); Serial.print(ctrl);
if (ctrl == 0 && wasBtnPushed(i) == 1) { ctrl = 1; digitalWrite(ledPin[i], HIGH); }
if (ctrl == 1 && wasBtnPushed(i) == 2) { ctrl = 2; digitalWrite(ledPin[i], HIGH);}
if (ctrl == 2 && wasBtnPushed(i) == 3) { ctrl = 3; digitalWrite(ledPin[i], HIGH);}
if (ctrl == 3 && wasBtnPushed(i) == 4) { ctrl = 4; digitalWrite(ledPin[i], HIGH);}
if (ctrl == 0 && wasBtnPushed(i) == 2) { reset(); }
if (ctrl == 0 && wasBtnPushed(i) == 3) { reset(); }
if (ctrl == 0 && wasBtnPushed(i) == 4) { reset(); }
if (ctrl == 1 && wasBtnPushed(i) == 3) { reset(); }
if (ctrl == 1 && wasBtnPushed(i) == 4) { reset(); }
if (ctrl == 2 && wasBtnPushed(i) == 1) { reset(); }
if (ctrl == 2 && wasBtnPushed(i) == 4) { reset(); }
if (ctrl == 3 && wasBtnPushed(i) == 1) { reset(); }
if (ctrl == 3 && wasBtnPushed(i) == 2) { reset(); }
if (ctrl == 4 && wasBtnPushed(i) == 1) { reset(); }
if (ctrl == 4 && wasBtnPushed(i) == 2) { reset(); }
if (ctrl == 4 && wasBtnPushed(i) == 3) { reset(); }
Serial.print(" ctrl_"); Serial.println(ctrl);
lastBtn = wasBtnPushed(i);
delay(100);
}
if (ctrl == 4) { Serial.print("Correct!"); delay(2000); reset(); }
}
}
}
void reset() {
for(led = 1; led <= 4; led++) {
digitalWrite(ledPin[led], LOW);
}
Serial.print(" has_reset");
ctrl = 0;
lastBtn = 0;
}
int wasBtnPushed(int x) {
buttonState[x] = digitalRead(btnPin[x]);
if (buttonState[x] != lastButtonState[x]) {
if (buttonState[x] == HIGH) { return x; }
}
lastButtonState[x] = buttonState[x];
}