combination password

Guys I've written this program, and the result isn't as I expected it to be. It keeps giving me access denied a few seconds after I push the first button. Thanks in advance for any help

int ledEnter = 11;
int ledWrong = 12;
int ledGood = 13;

int switch1 = 2;
int switch2 = 3;
int switch3 = 4;
int switch4 = 5;
int switch5 = 6;
int switch6 = 7;
int switch7 = 8;
int switch8 = 9;
int switch9 = 10;

boolean bs1 = LOW;
boolean bs2 = LOW;
boolean bs3 = LOW;
boolean bs4 = LOW;
boolean bs5 = LOW;
boolean bs6 = LOW;
boolean bs7 = LOW;
boolean bs8 = LOW;
boolean bs9 = LOW;

int wait = 100;
int num1;
int num2;
int num3;
int num4;
int c1 = 1;
int c2 = 1;
int c3 = 1;
int c4 = 1;
int choice = 0;
int count = 1;

void setup()
{
Serial.begin(9600);
pinMode(ledWrong, OUTPUT);
pinMode(ledGood, OUTPUT);
pinMode(ledEnter, OUTPUT);

pinMode(switch1, INPUT);
pinMode(switch2, INPUT);
pinMode(switch3, INPUT);
pinMode(switch4, INPUT);
pinMode(switch5, INPUT);
pinMode(switch6, INPUT);
pinMode(switch7, INPUT);
pinMode(switch8, INPUT);
pinMode(switch9, INPUT);

}
void loop()
{
num1 = -5;
num2 = -5;
num3 = -5;
num4 = -5;
Serial.available () >0;

Serial.println (" Enter Code");
Serial.println("");
Serial.println("");
while (num1 == -5)
{
bs1 = digitalRead (switch1);
if (bs1 == HIGH)
{
num1 = 1;
bs2 = digitalRead (switch2);
if (bs2 == HIGH)
{
num1 = 2;
}
bs3 = digitalRead (switch3);
if (bs3 == HIGH)
{
num1 = 3;
}
bs4 = digitalRead (switch4);
if (bs4 == HIGH)
{
num1 = 4;
}
bs5 = digitalRead (switch5);
if (bs5 == HIGH)
{
num1 = 5;
}
bs6 = digitalRead (switch6);
if (bs6 == HIGH)
{
num1 = 6;
}
bs7 = digitalRead (switch7);
if (bs7 == HIGH)
{
num1 = 7;
}
bs8 = digitalRead (switch8);
if (bs8 == HIGH)
{
num1 = 8;
}
bs9 = digitalRead (switch9);
if (bs9 == HIGH)
{
num1 = 9;
}
if (num1 != -5)
{
digitalWrite(ledEnter, HIGH);
Serial.println ("Code is Being Entered");
}
}

while (num2 == -5)
{
bs1 = digitalRead (switch1);
if (bs1 == HIGH)
{
num2 = 1;
bs2 = digitalRead (switch2);
if (bs2 == HIGH)
{
num2 = 2;
}
bs3 = digitalRead (switch3);
if (bs3 == HIGH)
{
num2 = 3;
}
bs4 = digitalRead (switch4);
if (bs4 == HIGH)
{
num2 = 4;
}
bs5 = digitalRead (switch5);
if (bs5 == HIGH)
{
num2 = 5;
}
bs6 = digitalRead (switch6);
if (bs6 == HIGH)
{
num2 = 6;
}
bs7 = digitalRead (switch7);
if (bs7 == HIGH)
{
num2 = 7;
}
bs8 = digitalRead (switch8);
if (bs8 == HIGH)
{
num2 = 8;
}
bs9 = digitalRead (switch9);
if (bs9 == HIGH)
{
num2 = 9;
}
}

while (num3 == -5)
{
bs1 = digitalRead (switch1);
if (bs1 == HIGH)
{
num3 = 1;
bs2 = digitalRead (switch2);
if (bs2 == HIGH)
{
num3 = 2;
}
bs3 = digitalRead (switch3);
if (bs3 == HIGH)
{
num3 = 3;
}
bs4 = digitalRead (switch4);
if (bs4 == HIGH)
{
num3 = 4;
}
bs5 = digitalRead (switch5);
if (bs5 == HIGH)
{
num3 = 5;
}
bs6 = digitalRead (switch6);
if (bs6 == HIGH)
{
num3 = 6;
}
bs7 = digitalRead (switch7);
if (bs7 == HIGH)
{
num3 = 7;
}
bs8 = digitalRead (switch8);
if (bs8 == HIGH)
{
num3 = 8;
}
bs9 = digitalRead (switch9);
if (bs9 == HIGH)
{
num3 = 9;
}
}
while (num4 == -5)
{
bs1 = digitalRead (switch1);
if (bs1 == HIGH)
{
num4 = 1;
bs2 = digitalRead (switch2);
if (bs2 == HIGH)
{
num4 = 2;
}
bs3 = digitalRead (switch3);
if (bs3 == HIGH)
{
num4 = 3;
}
bs4 = digitalRead (switch4);
if (bs4 == HIGH)
{
num4 = 4;
}
bs5 = digitalRead (switch5);
if (bs5 == HIGH)
{
num4 = 5;
}
bs6 = digitalRead (switch6);
if (bs6 == HIGH)
{
num4 = 6;
}
bs7 = digitalRead (switch7);
if (bs7 == HIGH)
{
num4 = 7;
}
bs8 = digitalRead (switch8);
if (bs8 == HIGH)
{
num4 = 8;
}
bs9 = digitalRead (switch9);
if (bs9 == HIGH)
{
num4 = 9;
}
}
if ((num1 == c1) && (num2 == c2) && (num3 == c3) && (num4 == c4))
{
digitalWrite (ledGood, HIGH);
Serial.println ("Access Granted");
delay (5000);
}
else
{
digitalWrite (ledWrong,HIGH);
Serial.println("Access Denied");
delay (5000);
}
}
}

}
}

}

 Serial.available () >0;

What do you think this is doing?

    }   
  }
      }
     
          }
              }
           
                  }

Properly formatted code would be so much easier to read (and write). If you can't be bothered writing code properly, at least use Tools + Auto Format before incorrectly posting your code.

Please start here

jusspit I don't see logic to prevent a single keypress from being registered 4 times in rapid succession. You might only hold the key down for 1 millisecond but in that time your Arduino has registered dozens or hundreds of duplicate keypresses. You need to make sure that after a key is pressed, you ignore repeated presses for a few milliseconds. Something between 25 and 250 milliseconds is a reasonable place to start.

Also, are you using pulldown resistors? Without a pulldown resistor, a floating pin may occasionally read HIGH when no button was actually pressed.

Also, you may find it easier to write/debug if you use arrays and loops. Something like

char code[4] = { 1, 1, 1, 1 }; // your code here
char keypress[4]; // place to hold user input
byte button[9] = { 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // your pins here

void setup()
{
for(byte i=0; i < 10; i++) // for each button pin,
  { pinMode(button[i], INPUT); } // set it to INPUT
}

void loop()
{
for(byte i=0; i < 10; i++) // check each button
  {
  if(digitalRead(button[i])) // if this button is pressed,
    {
    for(byte x=0; x < 3; x++) // move every keystroke over in the array to make room for the new
      { keypress[x] = keypress[x+1]; }
    keypress[3] = button[i]; // insert the new keypress onto the end of our array
    if(!strcmp(code, keypress)) // compare the entered string, with the string containing the correct code
      {
      // correct code entered!
      }
    delay(250); // prevent the same keypress being processed more than once
    }
  }
}
    if(!strcmp(code, keypress)) // compare the entered string, with the string containing the correct code

Wrong. Neither code nor keypress are strings, so passing them to a function that expects strings is a mistake.

memcmp() to the rescue.

byte button[9] = { 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // your pins here
....
for(byte i=0; i < 10; i++)

Oops.

PaulS:

    if(!strcmp(code, keypress)) // compare the entered string, with the string containing the correct code

Wrong. Neither code nor keypress are strings, so passing them to a function that expects strings is a mistake.

Ah, I forgot the NULL terminators. I believe strcmp would work if both strings had null terminators, but you're right, memcmp is appropriate.

AWOL:

byte button[9] = { 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // your pins here

....
for(byte i=0; i < 10; i++)



Oops.

Maybe I, uh, left that there as an exercise for the reader?

I found the mistakes I had and arranged them and finally my project is working. This is the arranged code. Sorry for no comments but I haven't got time to write comments.

int ledEnter = 11;
int ledWrong = 12;
int ledGood = 13;

int switch1 = 2;
int switch2 = 3;
int switch3 = 4;
int switch4 = 5;
int switch5 = 6;
int switch6 = 7;
int switch7 = 8;
int switch8 = 9;
int switch9 = 10;

boolean bs1 = LOW;
boolean bs2 = LOW;
boolean bs3 = LOW;
boolean bs4 = LOW;
boolean bs5 = LOW;
boolean bs6 = LOW;
boolean bs7 = LOW;
boolean bs8 = LOW;
boolean bs9 = LOW;

int wait = 100;
int num1 = -5;
int num2 = -5;
int num3 = -5;
int num4 = -5;
int c1 = 1;
int c2 = 9;
int c3 = 9;
int c4 = 6;
int count = 1;

void setup()
{
(Serial.available() > 0);

Serial.begin(9600);
pinMode(ledWrong, OUTPUT);
pinMode(ledGood, OUTPUT);
pinMode(ledEnter, OUTPUT);

pinMode(switch1, INPUT);
pinMode(switch2, INPUT);
pinMode(switch3, INPUT);
pinMode(switch4, INPUT);
pinMode(switch5, INPUT);
pinMode(switch6, INPUT);
pinMode(switch7, INPUT);
pinMode(switch8, INPUT);
pinMode(switch9, INPUT);
Serial.println (" Enter Code");
Serial.println("");
Serial.println("");

}
void loop()
{
delay (100);
while ((num1 == -5) || (num2 == -5) || (num3 == -5) || (num4 == -5))
{
while (num1 == -5)
{
bs1 = digitalRead (switch1);
if (bs1 == HIGH)
{
num1 = 1;
}
bs2 = digitalRead (switch2);

if (bs2 == HIGH)
{
num1 = 2;
}
bs3 = digitalRead (switch3);
if (bs3 == HIGH)
{
num1 = 3;
}
bs4 = digitalRead (switch4);
if (bs4 == HIGH)
{
num1 = 4;
}
bs5 = digitalRead (switch5);
if (bs5 == HIGH)
{
num1 = 5;
}
bs6 = digitalRead (switch6);
if (bs6 == HIGH)
{
num1 = 6;
}
bs7 = digitalRead (switch7);
if (bs7 == HIGH)
{
num1 = 7;
}
bs8 = digitalRead (switch8);
if (bs8 == HIGH)
{
num1 = 8;
}
bs9 = digitalRead (switch9);
if (bs9 == HIGH)
{
num1 = 9;
}
if (num1 != -5)
{
digitalWrite(ledEnter, HIGH);
Serial.print ("Code is Being Entered : ");
Serial.print ("* ");
}
delay (200);
}

bs1 = LOW;
bs2 = LOW;
bs3 = LOW;
bs4 = LOW;
bs5 = LOW;
bs6 = LOW;
bs7 = LOW;
bs8 = LOW;
bs9 = LOW;

while (num2 == -5)
{
bs1 = digitalRead (switch1);
if (bs1 == HIGH)
{
num2 = 1;
}
bs2 = digitalRead (switch2);
if (bs2 == HIGH)
{
num2 = 2;
}
bs3 = digitalRead (switch3);
if (bs3 == HIGH)
{
num2 = 3;
}
bs4 = digitalRead (switch4);
if (bs4 == HIGH)
{
num2 = 4;
}
bs5 = digitalRead (switch5);
if (bs5 == HIGH)
{
num2 = 5;
}
bs6 = digitalRead (switch6);
if (bs6 == HIGH)
{
num2 = 6;
}
bs7 = digitalRead (switch7);
if (bs7 == HIGH)
{
num2 = 7;
}
bs8 = digitalRead (switch8);
if (bs8 == HIGH)
{
num2 = 8;
}
bs9 = digitalRead (switch9);
if (bs9 == HIGH)
{
num2 = 9;
}
if (num2 != -5)
{
Serial.print("* ");
}
delay (200);
}

bs1 = LOW;
bs2 = LOW;
bs3 = LOW;
bs4 = LOW;
bs5 = LOW;
bs6 = LOW;
bs7 = LOW;
bs8 = LOW;
bs9 = LOW;

while (num3 == -5)
{
bs1 = digitalRead (switch1);
if (bs1 == HIGH)
{
num3 = 1;
}
bs2 = digitalRead (switch2);
if (bs2 == HIGH)
{
num3 = 2;
}
bs3 = digitalRead (switch3);
if (bs3 == HIGH)
{
num3 = 3;
}
bs4 = digitalRead (switch4);
if (bs4 == HIGH)
{
num3 = 4;
}
bs5 = digitalRead (switch5);
if (bs5 == HIGH)
{
num3 = 5;
}
bs6 = digitalRead (switch6);
if (bs6 == HIGH)
{
num3 = 6;
}
bs7 = digitalRead (switch7);
if (bs7 == HIGH)
{
num3 = 7;
}
bs8 = digitalRead (switch8);
if (bs8 == HIGH)
{
num3 = 8;
}
bs9 = digitalRead (switch9);
if (bs9 == HIGH)
{
num3 = 9;
}
if (num3 != -5)
{
Serial.print("* ");
}
delay (200);
}

bs1 = LOW;
bs2 = LOW;
bs3 = LOW;
bs4 = LOW;
bs5 = LOW;
bs6 = LOW;
bs7 = LOW;
bs8 = LOW;
bs9 = LOW;

while (num4 == -5)
{
bs1 = digitalRead (switch1);
if (bs1 == HIGH)
{
num4 = 1;
}
bs2 = digitalRead (switch2);
if (bs2 == HIGH)
{
num4 = 2;
}
bs3 = digitalRead (switch3);
if (bs3 == HIGH)
{
num4 = 3;
}
bs4 = digitalRead (switch4);
if (bs4 == HIGH)
{
num4 = 4;
}
bs5 = digitalRead (switch5);
if (bs5 == HIGH)
{
num4 = 5;
}
bs6 = digitalRead (switch6);
if (bs6 == HIGH)
{
num4 = 6;
}
bs7 = digitalRead (switch7);
if (bs7 == HIGH)
{
num4 = 7;
}
bs8 = digitalRead (switch8);
if (bs8 == HIGH)
{
num4 = 8;
}
bs9 = digitalRead (switch9);
if (bs9 == HIGH)
{
num4 = 9;
}
if (num4 != -5)
{
Serial.println("* ");
}

delay (200);
}
bs1 = LOW;
bs2 = LOW;
bs3 = LOW;
bs4 = LOW;
bs5 = LOW;
bs6 = LOW;
bs7 = LOW;
bs8 = LOW;
bs9 = LOW;
digitalWrite (ledEnter, LOW);
}
if ((num1 == c1) && (num2 == c2) && (num3 == c3) && (num4 == c4))
{
digitalWrite (ledGood, HIGH);
Serial.println ("Access Granted");
delay (5000);
num1 = -5;
num2 = -5;
num3 = -5;
num4 = -5;
}
else if ((num1 != c1) || (num2 != c2) || (num3 != c3) || (num4 != c4))
{
digitalWrite (ledWrong, HIGH);
Serial.println("Access Denied");
delay (5000);
}

}

Sorry for no comments but I haven't got time to write comments.

Instead, you have time to stare at that code next month, going "What was I thinking?"?

jusspit:
This is the arranged code. Sorry for no comments but I haven't got time to write comments.

Comments are for that horrible bastard that in two years will be looking at your code and scratching his head.

Which will be you.
Do your future self a favour.

I haven't got time to write comments

You should make the time to learn about arrays though.

I know arrays but not with arduino. Please appreciate that I had the board for a week and managed to write that program alone.

Could you maybe find the time to at least acquaint yourself with tags?

No, i prefer to use that time to experiment

Please nicely.

No, i prefer to use that time to experiment

If you don't, getting booted out is in your future. Your choice.