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)