Two button lock error

Hi!

I'm trying to make a lock where the user needs to press two buttons in a sequence to get a door unlocked, but every time I upload the code it keeps writing 1 and 2 (The key's sat to the buttons) automatically! I cannot see where the problem is.
The Buttons are each connected to a port and to 5V. While the LED's are separated on a breadboard.

//
const int button1 = 22;
const int button2 = 23;
const int greenled = 6;
const int RedLed = 8;
const int Thelock = 11;
int code = 0; 
int codesize = 4;    // Number of the Code Digits
//Resets the count
int keypress = 0;     //keypress counter
int locked = 0;
int unlock_code = 1212;   // the code

void setup()
{

  Serial.begin(9600);

  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(Thelock, OUTPUT);
  pinMode(RedLed, OUTPUT);
  pinMode(greenled, OUTPUT);
}

void loop() {
//If the button is pressed write 1 
  if (locked == 0)
  {
    if (digitalRead(button1) == HIGH) //Button 1
    {

      code = (code + 1) * 2;
      keypress = keypress + 1;
      Serial.println("1");
      delay(90000);
      
    }
//If the button is pressed write 2
    if (digitalRead(button2) == HIGH) //Button 2
    {
      code = (code + 1) * 3;
      keypress = keypress + 1;
        Serial.println("2");
      delay(90000);
 }
 // if the code entered matches the secret code then give access
    if (keypress == codesize)
    {
      if (unlock_code == code)
      { locked = 1;
        digitalWrite(greenled, HIGH);
        Serial.println("Acess Granted");
        delay(150);
        digitalWrite(greenled, LOW);
        code = 0;
        locked = 0;
        keypress = 0;
      }
      else
      //if the code entered does not match with the secret code then write acess denied
      {
        digitalWrite(RedLed, HIGH);
        delay(50);
        digitalWrite(RedLed, LOW);
        code = 0;
        locked = 0;
        keypress = 0;
        delay(250);
        Serial.println("Acess Denied");
      }
    }
  }
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Do you have pulldown resistors on your button pins?

Yes, both of the buttons have a 10k resistor attached to them leading to the port!

That's not how a pulldown resistor works. Please see:

Even better, connect your buttons to GND instead, use the internal pullup resistors in the microcontroller, and invert the button logic in your code:

Alright! I got it to function properly, now my main issue is everytime I press the code correctly is still gives me an "Access Denied"

/* invert
 * <http://www.arduino.cc/en/Tutorial/Invert>
 *
 * turns on and off a light emitting diode(LED) connected to digital  
 * pin 13, when pressing a pushbutton attached to pin 7. It illustrates the
 * concept of Active-Low, which consists in connecting buttons using a
 * 1K to 10K pull-up resistor.
 *
 */
int G = 13; // choose the pin for the LED
int button1 = 2;
int button2 = 3;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status
int code = 0;
int codesize = 4;
int keypress = 0;
int unlock_code = 1212;
int locked = 0;

void setup() {
  Serial.begin(9600);
  pinMode(G, OUTPUT);  // declare LED as output
pinMode(button2, INPUT_PULLUP);
  pinMode(button1, INPUT_PULLUP);// declare pushbutton as input
}

void loop(){
 if (locked == 0)
  {
    if (digitalRead(button1) == LOW) //Button 1
    {

      code = (code + 1) * 2;
      keypress = keypress + 1;
      Serial.println("1");
      delay(900);
      
    }
//If the button is pressed write 2
    if (digitalRead(button2) == LOW) //Button 2
    {
      code = (code + 1) * 3;
      keypress = keypress + 1;
        Serial.println("2");
      delay(900);
 }
 // if the code entered matches the secret code then give access
    if (keypress == codesize)
    {
      if (unlock_code == code)
      { locked = 1;
        digitalWrite(G, HIGH);
        Serial.println("Acess Granted");
        delay(150);
        digitalWrite(G, LOW);
        code = 0;
        locked = 0;
        keypress = 0;
      }
      else
      //if the code entered does not match with the secret code then write acess denied
      {
        code = 0;
        locked = 0;
        keypress = 0;
       digitalWrite(G, HIGH);
        Serial.println("Acess Denied");
        delay(5000);
        digitalWrite(G, LOW);
      }
    }
  }
}
int unlock_code = 1212;
if (digitalRead(button1) == LOW) //Button 1
    {

      code = (code + 1) * 2;
      keypress = keypress + 1;
      Serial.println("1");
      delay(900);
      
    }
//If the button is pressed write 2
    if (digitalRead(button2) == LOW) //Button 2
    {
      code = (code + 1) * 3;
      keypress = keypress + 1;
        Serial.println("2");
      delay(900);
 }
if (unlock_code == code)

What button combination are you typing to reach unlock_code value 1212 ?
Calculate the code value after each button press and see if it matches...

Gatheringrook1:
Alright! I got it to function properly, now my main issue is everytime I press the code correctly is still gives me an "Access Denied"

/* invert
  • http://www.arduino.cc/en/Tutorial/Invert
  • turns on and off a light emitting diode(LED) connected to digital
  • pin 13, when pressing a pushbutton attached to pin 7. It illustrates the
  • concept of Active-Low, which consists in connecting buttons using a
  • 1K to 10K pull-up resistor.

*/
int G = 13; // choose the pin for the LED
int button1 = 2;
int button2 = 3;  // choose the input pin (for a pushbutton)
int val = 0;    // variable for reading the pin status
int code = 0;
int codesize = 4;
int keypress = 0;
int unlock_code = 1212;
int locked = 0;

void setup() {
  Serial.begin(9600);
  pinMode(G, OUTPUT);  // declare LED as output
pinMode(button2, INPUT_PULLUP);
  pinMode(button1, INPUT_PULLUP);// declare pushbutton as input
}

void loop(){
if (locked == 0)
  {
    if (digitalRead(button1) == LOW) //Button 1
    {

code = (code + 1) * 2;
      keypress = keypress + 1;
      Serial.println("1");
      delay(900);
     
    }
//If the button is pressed write 2
    if (digitalRead(button2) == LOW) //Button 2
    {
      code = (code + 1) * 3;
      keypress = keypress + 1;
        Serial.println("2");
      delay(900);
}
// if the code entered matches the secret code then give access
    if (keypress == codesize)
    {
      if (unlock_code == code)
      { locked = 1;
        digitalWrite(G, HIGH);
        Serial.println("Acess Granted");
        delay(150);
        digitalWrite(G, LOW);
        code = 0;
        locked = 0;
        keypress = 0;
      }
      else
      //if the code entered does not match with the secret code then write acess denied
      {
        code = 0;
        locked = 0;
        keypress = 0;
      digitalWrite(G, HIGH);
        Serial.println("Acess Denied");
        delay(5000);
        digitalWrite(G, LOW);
      }
    }
  }
}

I dunno if you changed you wiring from supplying 5V to the inputs, which is okay electrically but....

Your input pins are set to INPUT_PULLUP mode. The button should be connected to the pin on one side and ground on the other so that pushing the button grounds the pin. The button down pulls the INPUT_PULLUP pin to ground.

If you are feeding the INPUT_PULLUP pin 5V it will never register a button press.

They 900 ms delays.... I think you need to learn how to work with time not just to get rid of those but because you can include how long between presses (like more than X and less than Y long between), yes you can time a pattern with a recording sketch or mode and use that to make a match template, you will only need 1 button too.

A little unblocking and you can read pins while the button code is waiting.
That won't work with delay() blocking execution.

This function is made to run in loop(). See how it works, use it to get rid of delay() calls.

void nonBlockingButtonRead()
{
  static unsigned long startWait = 0;  // on creation only, static variables keep their values
  static unsigned long intervalWait = 0;

  if ( intervalWait > 0 )  // this timer only runs when it is set up to run
  {
    if ( millis() - startWait >= intervalWait )  // is time up yet?
    {
      intervalWait = 0;  // time is up, turn te timer off
    }
    else
    {
      return;  // time is not up, don't run the rest of the function until it is
    }
  }

    if (digitalRead(button1) == LOW) //Button 1
    {

      code = (code + 1) * 2;  // your code never resets the value of 'code'. every press makes it bigger.

      keypress = keypress + 1;
      Serial.println("1");
//      delay(900);
      intervalWait = 900;  // set the wait time, the timer will run next time the function runs
      startWait = millis();  // set the start time for the wait   
    }

//If the button is pressed write 2
    if (digitalRead(button2) == LOW) //Button 2
    {
      code = (code + 1) * 3;  // woot

      keypress = keypress + 1;
        Serial.println("2");
//      delay(900);
      intervalWait = 900;  // set the wait time, the timer will run next time the function runs
      startWait = millis();  // set the start time for the wait  
     }
}