Hi, I've been tinkering with this for a while now (1+month on and off), correcting bugs and errors, but I suspect I've come across something deeper than my (scarce) skills with arduino coding, so I might aswell share it and see if someone has a better understanding of what is going on.
The project is fairly simple:
I have 5 pushbuttons connected in a pull-down configuration to pins 2 to 6,
an electronic lock on pin 8 (actually it will be a 5v relay for a 12v lock but right now I'm just measuring the voltage with a multimeter so nevermind),
and a bunch of pushbuttons all connected in parallel (in pulldown configuration through a 1200ohm resistor too) to pin 7 which I haven't even hooked up for this trial so we can ignore it aswell
The idea is you have to press the pushbuttons 1 to 5 in order to open the lock, and if the order is not correct or any of the other bait buttons (on pin7) are pressed it just restarts and waits for the correct order.
Board is an Arduino Uno and all the electronics parts have been thoroughly tested and work fine so the problem's not there, allthough in case it has something to do I should say all pushbutton's grounds are wired together and to the arduino's ground (pushbuttons 1 to 5 have separate individual positive sides which go to the pins 2 to 6, error pushbuttons' positives all go to pin 7) and there's a substantial amount of cable (about 1,5m).
Here's the whole code:
int note[] = {1, 2, 3, 4, 5, 6}; //array for checking password advance
int j = 0; //var changed by buttons
int correct = 0; //var for succesfull entry
void setup() {
Serial.begin(9600);
pinMode(2, INPUT); //button 1 (all pushbuttons on pull-down configuration with 1200ohms resistors)
pinMode(3, INPUT); //button 2
pinMode(4, INPUT); //button 3
pinMode(5, INPUT); //button 4
pinMode(6, INPUT); //button 5
pinMode(7, INPUT); //error buttons all connected to pin 7
pinMode(8, OUTPUT); //lock
}
void loop() {
digitalWrite(8, HIGH); //lock is engaged
Serial.println("start"); //let's me know the loop has started from the beginning
while (correct == 0) {
Serial.println(note[j]); //gives me the place on the array at which we're on, incidently tells me which button I have to push next
if (digitalRead(2) == HIGH) { //when pushbutton 1 is pressed
while (digitalRead(2) != LOW) { } //stops the whole process while the button is still being pushed
delay(100);
if (1 == note[j]) {
j++; //checks the correct order and sums up 1 for next step proofing if correct
}
else {
j = 0; //if the order is not right all is reset to starting point
correct = 0;
}
}
if (digitalRead(3) == HIGH) { //same procedure for button 2
while (digitalRead(3) != LOW) { }
delay(100);
if (2 == note[j]) {
j++;
}
else {
j = 0;
correct = 0;
}
}
if (digitalRead(4) == HIGH) { //same procedure for button 3
while (digitalRead(4) != LOW) { }
delay(100);
if (3 == note[j]) {
j++;
}
else {
j = 0;
correct = 0;
}
}
if (digitalRead(5) == HIGH) { //same procedure for button 4
while (digitalRead(5) != LOW) { }
delay(100);
if (4 == note[j]) {
j++;
}
else {
j = 0;
correct = 0;
}
}
if (digitalRead(6) == HIGH) { //same procedure for button 5
while (digitalRead(6) != LOW) { }
delay(100);
if (5 == note[j]) {
j++;
}
else {
j = 0;
correct = 0;
}
}
if (digitalRead(7) == HIGH) { //if error button is pressed at any moment, all resets
delay(100);
j = 0; correct = 0;
}
if (note[j] == 6) {
delay(100); //when all correct pushbuttons have been preseed in the right order, the correct var turns to 1 thus exiting this while loop...
correct = 1;
}
}
while (correct == 1) { //...and entering this while loop which opens the lock
Serial.println("lock open"); //lets me know we've succesfully entered this while loop
digitalWrite(8, LOW); //lock gets open
delay(1000); //gives it a second so the mechanism doesn't jam
Serial.println("reset"); //lets me know we're done
correct = 0;
j = 0;
delay(5);
}
}
Feel free to copy, use and modify it, just please let me know you've done so (you know: comment, send me a PM or whatever but please let me know! )
What's the issue? Well the thing is it does work, but only with 4 buttons.
If I delete the last pressbutton check, e.g.:
if(digitalRead(6)==HIGH){ //same procedure for button 5
while(digitalRead(6) != LOW){ }
delay(100);
if(5==note[j]){j++;}
else {j=0;correct=0;}}
and set the
if(note[j]==6){delay(100); correct=1;}
to
if(note[j]==5){delay(100); correct=1;}
the whole thing runs smoothly, the while loop for opening the lock runs, I get my 0V on the multimeter wired to pins ground and 8 and I'm happy
But if I run it as it is, it never exits the first while loop, changing my note[] value to 1 and asking for the correct combination again and again...
This has me completely bedazzled and I finally came here (after much frustration) just to see if there's a deeper issue I'm not seeing (as I'm fairly new to this whole arduino coding game) or (gods forgive me) I've writen some mistake I'm not seeing.
Things I've tried:
-running it on 4 and 5 pushbuttons with and without changing the note[] array length (1 to 5 and 1 to 6)
-changing the pin order and the pin numbers in almost every configuration
-checking my circuit (whole hardware side) until I dream with it at night
results so far: it only wants to work with 4 pushbuttons.
So if any fresh eyes could illuminate me and others on this issue, I'ld be forever gratefull. Also general knowledge about arduino coding and or rambling about arduino coding issue's related frustration you might think I haven't grasped yet is more than welcome.
Thanks in advance, I'll keep tinkering and posting my advances.
Also pardon my french, I'm not a native English speaker