Mock Security Pad issue

Hi there, im new to Arduino, and even newer to this forum.
Im having some issues, im trying my best to write a short program for a set out of buttons and two LED's.
The idea is that when the code is entered correctly (Correct Buttons Hit) the light changes from Red (ledOne) to Green (ledTwo).
at the moment it doesnt matter what the password is set to (pass[]) whatever button is hit the light changes, then after a while if you leave say a minute, the lights dont even change properly at all.
Here's the code:

int ledOne = 13;  // Red LED
int ledTwo = 12;  // Green LED
int x = 0; // Set x to 0
byte pass[] = {1,2}; // Pass Code To Enter
byte attempt[2]; // Entered Code; What people entered
byte buttons[] = {2,3,4,5,6,7,8,9,10,11}; // Set Buttons 
byte state[10]; // Set digitalRead states array in preperation
int time = 0; // set time measure "millis()"
int y = 0; // Another Variable set to 0

void setup() {
  for (x=0; x<10; x++){
    pinMode(buttons[x], INPUT); // For all the buttons set them to INPUT
  }
  pinMode(ledOne, OUTPUT);//set LEDS TO OUTPUT
  pinMode(ledTwo, OUTPUT);
  digitalWrite(ledOne, HIGH); // Turn Red Light On
}

void loop(){
  for (x=0; x<10; x++){// for each number 0-9
    state[x] = digitalRead(buttons[x]);// Set each digital read from 2-11 in to state[0-9]
  }
  for (x=0; x<10; x++){
    if (state[x] == HIGH){//see if any states are HIGH
      attempt[y] = state[x];//If they are, set Attempt[0] to whichever state it was
      time = millis(); // make time variable equal how long pgrams been running
    }
  }
  if (attempt[0] == pass[0] /*&& pass[1] == attempt[1]*/){// if attempt[0] is equal to pass[0], greenOn()
    greenOn();
  }
  if ((millis() - time) > 10000){//if time between last button press and now is 5 seconds set all code entries to 0
    for (x=0; x<10; x++){
      attempt[0] = 0;
    }
  }
  if (attempt[0] != pass[0]){// if attempt[0] is not pass[0] turn off green return to red
    greenOff();
  }
}

void greenOn(){
  digitalWrite(ledTwo, HIGH);
  digitalWrite(ledOne, LOW);
}

void greenOff(){
  digitalWrite(ledTwo, LOW);
  digitalWrite(ledOne, HIGH);
}

Any help will be appreciated, try to solve it in as much a simple way as possible, im still learning (I guess if i wasn't learning i wouldn't have this issue :P)

Glitchy4ButtonCOde.ino (1.2 KB)

You are not using the internal pullup resistors, so you need to have external resistors with the switches, to prevent floating pins. The test for HIGH says that the resistors should be pull-down resistors.

How ARE the switches wired?

They're wired with pull down resistors on the bread board as in.:
5V in one pin of button, on the other pin there is a resistor connected to ground and wire going from pin to digital input pin

Also, im pretty sure its a programming issue, as the light lights up, but not when i press the specified button instead whenever i press a button at all

byte attempt[2]; // Entered Code; What people entered
    for (x=0; x<10; x++){
      attempt[0] = 0;
    }

That's probably not good.

int time = 0; // set time measure "millis()"
      time = millis(); // make time variable equal how long pgrams been running

Nor is that. Look at the documentation for the millis() function again.

      attempt[y] = state[x];//If they are, set Attempt[0] to whichever state it was

You are storing HIGH in attempt[0]. Is that what you want? I would think that you would want to store the pin number or the array position, not the state of the pin at that position in the array.

Since HIGH is one, if any switch is pressed, the value will match pass[0].

I believe you, my intelligent friend, have solved at least one of my problems, possibly the biggest one. :slight_smile: