Hello fellow coders and newbies! Hopefully this works out in everyones best interest!
I myself am most definitely a newbie and hoping for a little schooling in the right direction. I've done enough reading to alter this code I've found for sequencing buttons but am having trouble applying the millis() function into the code.
My goal (as best I can explain it) in english first:
- 4 buttons triggered in a specific order turns on led and waits for reset button (wrong order resets waiting for another entry)
- Once any of the 4 buttons are triggered, I would like "x seconds" of time to pass and then have the loop restart.
i.e. if one button is triggered and left for 10 seconds the program goes back to the top waiting for new entry. if 3 buttons are triggered and 10 seconds passes still go back to the top and wait for new entry.
//CONSTANTS
const int button1 = 2; //first button is on pin 2
const int button2 = 3; //second is on pin 3
const int button3 = 4; //third is pin 4
const int button4 = 5; //fourth is pin 5
const int button5 = 6; //reset button
const int LED[] = {14,15,16,17};
const int greenLed = 9; //green LED is pin 9
void checkEntered1(int button);
//VARIABLES
int code[] = {1,2,3,4}; //the desired code is entered in this array, separated by commas
int read[] = {0,0,0,0}; //array for each switch to only activate once per reset
int entered[5]; //create a new empty array for the code
// MILLIS
unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
unsigned long period = 10000;
// SETUP
void setup(){ //run once at sketch startup
Serial.begin(9600); //begin Serial
pinMode(button1, INPUT_PULLUP); //button 1 is an input
pinMode(button2, INPUT_PULLUP); //button 2 is an input
pinMode(button3, INPUT_PULLUP); //button 3 is an input
pinMode(button4, INPUT_PULLUP); //button 4 is an input
pinMode(button5, INPUT_PULLUP); //reset button
pinMode(greenLed, OUTPUT); // the green LED is an output
for (int i = 0; i < 4;i++){ //work through numbers 0-3
Serial.println(code[i]); //print each digit of the code
Serial.println(entered[i]); //print each element of the entered[]
//array (this was for me to check that it
//started at 0
pinMode(LED[i],OUTPUT);
}
}
//LOOP
void loop(){ //run repeatedly
currentMillis = millis();
if (digitalRead(button1) == LOW && read[0] !=1 ){ //if button1 is pressed
read[0]=1;
checkEntered1(1); //call checkEntered and pass it a 1
previousMillis = currentMillis;
}
else if (digitalRead(button2) == LOW && read[1] !=1 ){ //if button2 is pressed
read[1]=1;
checkEntered1(2); //call checkEntered1 and pass it a 2
previousMillis = currentMillis;
}
else if (digitalRead(button3) == LOW && read[2] !=1 ){ //if button3 is pressed
read[2]=1;
checkEntered1(3); //call checkEntered1 and pass it a 3
previousMillis = currentMillis;
}
else if (digitalRead(button4) == LOW && read[3] !=1 ){ //if button4 is pressed
read[3]=1;
checkEntered1(4); //call checkEntered1 and pass it a 4
previousMillis = currentMillis;
}
else if (digitalRead(button5) == LOW){
reset(); // button inputs reset to be triggered again
close_all(); //turn off all LED's
digitalWrite(greenLed, LOW); // Turn on maglock
}
else if(currentMillis - previousMillis >= period);{
reset();
close_all;
loop();
previousMillis = currentMillis;
}
}
void checkEntered1(int button){ //check the first element of the entered[] array
digitalWrite(LED[button-1],HIGH);
if (entered[0] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered2(button); //move on to checkEntered2, passing it "button"
}
else if(entered[0] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[0] = button; //set the first element as the button that has been pressed
Serial.print("1: ");Serial.println(entered[0]); //for debugging
}
}
void checkEntered2(int button){ //check the second element of the entered[] array
digitalWrite(LED[button-1],HIGH);
if (entered[1] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered3(button); //move on to checkEntered3, passing it "button"
}
else if(entered[1] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[1] = button; //set the second element as the button that has been pressed
Serial.print("2: ");Serial.println(entered[1]); //for debugging
}
}
void checkEntered3(int button){ //check the third element of the entered[] array
digitalWrite(LED[button-1],HIGH);
if (entered[2] != 0){ //if it is not a zero, i.e. it has already been inputted
checkEntered4(button); //move on to checkEntered4, passing it "button"
}
else if (entered[2] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[2] = button; //set the third element as the button that has been pressed
Serial.print("3: ");Serial.println(entered[2]); //for debugging
}
}
void checkEntered4(int button){ //check the third element of the entered[] array
digitalWrite(LED[button-1],HIGH);
if (entered[3] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
entered[3] = button; //set the third element as the button that has been pressed
Serial.print("4: ");Serial.println(entered[3]); //for debugging
delay(100); //allow time for processing
compareCode(); //call the compareCode function
}
}
void compareCode(){ //checks if the code entered is correct by comparing the code[] array with the entered[] array
for (int i = 0; i<4;i++){ //these three lines are for debugging
Serial.println(entered[i]);
}
if ((entered[0]==code[0]) && (entered[1]==code[1]) && (entered[2]==code[2]) && (entered[3]==code[3])){ //if all the elements of each array are equal
digitalWrite(greenLed, HIGH); //turn the green LED on
for (int i = 0; i < 5; i++){ //this next loop is for debugging
entered[i] = 0;
}
loop(); //return to loop() (not really necessary)
}
else { //if you (or the intruder) get the code wrong
for (int i = 0; i < 5; i++){ //this next loop is for debugging
entered[i] = 0;
}
delay(1000);
close_all();
reset();
}
}
void reset(){
read[0]=0;
read[1]=0;
read[2]=0;
read[3]=0;
}
void close_all(){
digitalWrite(LED[0],LOW);
digitalWrite(LED[1],LOW);
digitalWrite(LED[2],LOW);
digitalWrite(LED[3],LOW);
}
I realize that my millis() code is kind of scattered, I'm not exactly sure where to put it... (the reason for the post) I've copied and pasted and cut every which way but to no avail. Can someone lend a hand? Thank you so much in advanced for your efforts and time!