Help me verify my code so far and answer question

This is my first ever code, can someone please verify things look correct and also answer a question?

  1. after I have put the correct combination to turn on the greenLed how would I go about adding code to put a different combination to turn off that greenLED when I was ready to turn it off instead of the 1000000 delay I have in there now, that way I can keep it on until I want to turn it off
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; //third is pin 6
const int button6 = 7; //fourth is pin 7



const int greenLed = 9; //green LED is pin 9
void checkEntered1(int button);

int code[] = {6,5,5,4,3,2}; //the desired code is entered in this array,
                        //separated by commas

int entered[7]; //create a new empty array for the code entered by
                //the user (has 4 elements)

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); //button 5 is an input
  pinMode(button6, INPUT_PULLUP); //button 6 is an input

  pinMode(greenLed, OUTPUT); // the green LED is an output

  for (int i = 0; i < 6;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[]


  }
}

void loop(){ //run repeatedly
  if (digitalRead(button1) == LOW){ //if button1 is pressed
    checkEntered1(1); //call checkEntered and pass it a 1
    
    delay(250);//wait, needed for correct functioning, otherwise
               //buttons are deemed to be pressed more than once
    
  }
  else if (digitalRead(button2) == LOW){ //if button2 is pressed
    checkEntered1(2); //call checkEntered1 and pass it a 2
    
    delay(250); //wait
    
  }
  else if (digitalRead(button3) == LOW){ //if button3 is pressed
    checkEntered1(3); //call checkEntered1 and pass it a 3
    
    delay(250); //wait
    
  }
  else if (digitalRead(button4) == LOW){ //if button4 is pressed
    checkEntered1(4); //call checkEntered1 and pass it a 4
    
    delay(250); //wait
    
  }
    else if (digitalRead(button5) == LOW){ //if button4 is pressed
    checkEntered1(5); //call checkEntered1 and pass it a 4
    
    delay(250); //wait
    
  }
    else if (digitalRead(button6) == LOW){ //if button4 is pressed
    checkEntered1(6); //call checkEntered1 and pass it a 4
    
    delay(250); //wait
    
  }
  

}

void checkEntered1(int button){ //check the first element of the entered[] array

  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

  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

  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

  if (entered[3] != 0){ //if it is not a zero, i.e. it has already been inputted
    checkEntered5(button); //move on to checkEntered4, passing it "button"
  }
  
  else 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
  }
  
}


void checkEntered5(int button){  //check the third element of the entered[] array

  if (entered[4] != 0){ //if it is not a zero, i.e. it has already been inputted
    checkEntered6(button); //move on to checkEntered4, passing it "button"
  }
  
  else if (entered[4] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
    entered[4] = button; //set the third element as the button that has been pressed
    Serial.print("5: ");Serial.println(entered[4]); //for debugging
  }
  
}

void checkEntered6(int button){ //check the fourth element of the entered[] array

  if (entered[5] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
    entered[5] = button; //set the final element as the button that has been pressed
    Serial.print("6: ");Serial.println(entered[5]); //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<6;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]) && (entered[4]==code[4])&& (entered[5]==code[5])){ //if all the elements of each array are equal

    digitalWrite(greenLed, HIGH); //turn the green LED on
    delay(100000); //wait for a bit
    digitalWrite(greenLed, LOW); //turn the green LED off


    
    for (int i = 0; i < 7; 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
    

    delay(1000);

    Serial.println("Red OFF");
    for (int i = 0; i < 7; i++){ //this next loop is for debugging
      entered[i] = 0;
     
    }
    
  }
  close_all();
}



void close_all(){
//digitalWrite(LED[0],LOW);
//digitalWrite(LED[1],LOW);
//digitalWrite(LED[2],LOW);
//digitalWrite(LED[3],LOW);
//digitalWrite(LED[4],LOW);
//digitalWrite(LED[5],LOW);
}

In point form, write down exactly what you are trying to accomplish, and show this to us.


When you are dealing with a switch that needs an action, look for switch changes in sate not the state the switch is sitting in.

Avoid using delay( ) like the plague as it slows code execution and blocks other code from running while the delay is executing.

Yea i am getting rid of the delay. Do I just basically repeat all my code with a different combination to turn the greenLed to low state? I want to just hit the button 1 three times to turn greenLed low

All you want to do is press/close the switch 3 times and have the green LED come ON i.e. on the 3rd press ?

What turns off the LED .

no, my 6 digit combination turns the greenLed ON now I want to be able to press a different 3 digit combination to turn the greenLed OFF.

Do I just repeat my code again but with only 3 digits required?

You want to press the correct sequence of switches to get the LED to come ON ?

What turns off the LED ?

Im already pressing the sequence to turn it ON im asking how to press a different sequence to turn it OFF!! Do I just repeat my code over again at the bottom?

Before adding extra functionality, you should improve your code...
Otherwise you will need to change things twice.
You should not leave a function by calling loop().
You should use return.
Using a for loop will save a lot of code...

Some rules to live by:

When you have three or more variables with the same name except for a trailing number, make them an array.

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; //third is pin 6
const int button6 = 7; //fourth is pin 7

should be:

const int ButtonCount = 6;
const byte ButtonPins[ButtonCount] = {2, 3, 4, 5, 6, 7};

When you have arrays, use loops:

const int ButtonCount = 6;
const byte ButtonPins[ButtonCount] = {2, 3, 4, 5, 6, 7}; //first button is on pin 2

const int greenLed = 9; //green LED is pin 9

int TurnOnCode[] = {6, 5, 5, 4, 3, 2};
const int TurnOnCodeCount = sizeof TurnOnCode / sizeof TurnOnCode[0];
int TurnOnCorrectCount = 0; // How many have been entered

int TurnOffCode[] = {6, 4, 2};
const int TurnOffCodeCount = sizeof TurnOffCode / sizeof TurnOffCode[0];
int TurnOffCorrectCount = 0; // How many have been entered

void setup()  //run once at sketch startup
{
  Serial.begin(9600); //begin Serial

  for (int i = 0; i < ButtonCount; i++)
    pinMode(ButtonPins[i], INPUT_PULLUP);

  pinMode(greenLed, OUTPUT); // the green LED is an output

  Serial.print("Turn On code: ");
  for (int i = 0; i < TurnOnCodeCount; i++)
  {
    Serial.print(TurnOnCode[i]); //print each digit of the code
  }
  Serial.println();

  Serial.print("Turn Off code: ");
  for (int i = 0; i < TurnOffCodeCount; i++)
  {
    Serial.print(TurnOffCode[i]); //print each digit of the code
  }
  Serial.println();
}

void loop()  //run repeatedly
{
  // Check each button
  for (int i = 0; i < ButtonCount; i++)
  {
    if (digitalRead(ButtonPins[i]) == LOW)  //if button is pressed
    {
      // Button is pressed
      checkEntered(i + 1); //call checkEntered and pass it the button number

      delay(250);  //wait, needed for correct functioning, otherwise
      //buttons are deemed to be pressed more than once
    }
  }
}

void checkEntered(int button)  //check the button press
{
  // Check against turn-on code
  if (button == TurnOnCode[TurnOnCorrectCount])
  {
    TurnOnCorrectCount++;
    if (TurnOnCorrectCount == TurnOnCodeCount)
    {
      // Success!
      digitalWrite(greenLed, HIGH); //turn the green LED on
      // Start over
      TurnOnCorrectCount = 0;
    }
  }
  else // Not the correct button
  {
    TurnOnCorrectCount = 0; // Start over
  }

  // Check against turn-off code
  if (button == TurnOffCode[TurnOffCorrectCount])
  {
    TurnOffCorrectCount++;
    if (TurnOffCorrectCount == TurnOffCodeCount)
    {
      // Success!
      digitalWrite(greenLed, LOW); //turn the green LED off
      // Start over
      TurnOffCorrectCount = 0;
    }
  }
  else // Not the correct button
  {
    TurnOffCorrectCount = 0; // Start over
  }
}
1 Like

Thank You, this is the first helpful comment Ive gotten on this forum. Everybody expects a beginner to understand what they are doing already lol

If you do not understand what is in the posts, you may ask for explanation. For us (helpers) it is sometimes difficult to estimate how much a beginner we are dealing with.
Also it is sometimes difficult to derive what you actually try to achieve... that is why some ask a lot of questions...

If I understand what the OP trying to do. A numerical ( keypad) entry system by entering a 4 digit number to turn on a led ( could be a relay / selenoid lock ) and another 4 digit to turn off led ( relay / selenoid lock )

Interesting little project.

close what I am doing is enter a 6 digit code to turn the LED on and a 3 digit code to turn the led off, its for a custom gun safe im building and using an access control power supply and a electromagnetic lock with a battery backup

@57buick

Try this google link. Check some of these projects. Some code modifications may need to be done.

https://www.google.com/search?q=Arduino+combination+lock&sxsrf=ALiCzsakqSzOf6D8fGgNgWWetFUE-PC3Xw%3A1662933353680&source=hp&ei=aVkeY-uQJL7bkPIPmNmxyAY&iflsig=AJiK0e8AAAAAYx5nedWr4uRVYvzZC0gq4w7mC4wbvJa9&ved=0ahUKEwir6c2-3Y36AhW-LUQIHZhsDGkQ4dUDCAg&uact=5&oq=Arduino+combination+lock&gs_lcp=Cgdnd3Mtd2l6EAMyBQgAEIAEMgUIABCABDIGCAAQHhAWMgYIABAeEBYyBggAEB4QFjIGCAAQHhAWMgYIABAeEBYyBggAEB4QFjIGCAAQHhAWMgYIABAeEBY6BAgjECc6BQgAEJECOhEILhCABBCxAxCDARDHARDRAzoLCAAQgAQQsQMQgwE6CAgAELEDEIMBOgsILhCABBDHARCvAToLCC4QsQMQgwEQ1AI6BQgAELEDOg4ILhCABBCxAxCDARDUAjoICAAQgAQQsQM6DgguELEDEIMBEMcBEK8BOgoIABCABBCHAhAUOgYIABAeEAc6CAgAEB4QBxAKOgQIABANOggIABAeEAgQBzoECAAQHjoGCAAQHhAIUABYuYEBYL2tAWgEcAB4AIABvwGIAdMjkgEEMC4yOZgBAKABAQ&sclient=gws-wiz

PS ... Separated the ammo for the weapon / guns for a better safety. Make sure the weapons are unloaded. In Canada ( where I live ) , they are illegal. Just my opinion.

yea that google link is the code i started with and I have been slowly modifying to do what I want.

This is my first time ever looking at code before so im trying to learn here. So even the most basic stuff eludes me right now.

:+1:

Thank you @johnwasser that looks way cleaner

Hello

It could be as simple as this t1030673.ino - Wokwi Arduino and ESP32 Simulator

@guix yea thats clean looking too thanks, I would just change the builtin LED to a pin to activate a 5v relay on my access control box and would work perfect, thanks