Hello!
I have recently started programming with arduino. It's very fun!
I have built multiple projects with Arduino, but there is one project that will not work completely.
A password activated LED.
Basically, if you press Buttons 1, 2, 3, 4 in order, an LED will flash, and then reset all values to zero so you can try again.
However, if you get this code wrong, another LED will flash, which also resets the values to zero.
Here's my code, about 100 lines and 2000 bytes.
const int LED = 3;
const int Error = 2;
const int Button1 = 4;
const int Button2 = 5;
const int Button3 = 6;
const int Button4 = 7;
int Button1pressed = 0;
int Button2pressed = 0;
int Button3pressed = 0;
int Button4pressed = 0;
int Password = 0;
int SafetyCheck = 0;
int SafetyCheck2 = 0;
int SafetyCheck3 = 0;
void setup() {
// put your setup code here, to run once:
pinMode(Button1, INPUT);
pinMode(Button2, INPUT);
pinMode(Button3, INPUT);
pinMode(Button4, INPUT);
pinMode(LED, OUTPUT);
pinMode(Error, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int Button1state = digitalRead(Button1);
int Button2state = digitalRead(Button2);
int Button3state = digitalRead(Button3);
int Button4state = digitalRead(Button4);
if (Button1state == HIGH) {
Button1pressed = 1;
}
if (Button2state == HIGH) {
Button2pressed = 1;
}
if (Button3state == HIGH) {
Button3pressed = 1;
}
if (Button4state == HIGH) {
Button4pressed = 1;
}
if (Button1pressed = 1) {
delay (100);
Password = 1;
delay (100);
Button1pressed = 0;
}
if (Button2pressed == 1) {
if (Password == 1) {
SafetyCheck = 1;
delay(100);
Password = 2;
delay (100);
Button2pressed = 0;
}
if (Password != 1 && SafetyCheck != 1) {
digitalWrite (Error, HIGH);
delay (250);
digitalWrite (Error, LOW);
delay (250);
digitalWrite (Error, HIGH);
delay (250);
digitalWrite (Error, LOW);
delay (250);
Button1pressed = 0;
Button2pressed = 0;
Button3pressed = 0;
Button4pressed = 0;
Password = 0;
SafetyCheck = 0;
SafetyCheck2 = 0;
SafetyCheck3 = 0;
}
}
if (Button3pressed = 1) {
if (Password = 2 && SafetyCheck == 1 && SafetyCheck3 != 1 && SafetyCheck2 !=1) {
SafetyCheck2 = 1;
delay(100);
Password = 3;
delay(100);
Button3pressed = 0;
}
if (Password != 2 && SafetyCheck2 != 1 ) {
digitalWrite (Error, HIGH);
delay (250);
digitalWrite (Error, LOW);
delay (250);
digitalWrite (Error, HIGH);
delay (250);
digitalWrite (Error, LOW);
delay (250);
Button1pressed = 0;
Button2pressed = 0;
Button3pressed = 0;
Button4pressed = 0;
Password = 0;
SafetyCheck = 0;
SafetyCheck2 = 0;
SafetyCheck3 = 0;
}
}
if (Button4pressed == 1) {
if (Password == 3 && SafetyCheck == 1 && SafetyCheck3 != 1 && SafetyCheck2 ==1) {
SafetyCheck3 =1;
delay (100);
Password = 4;
delay (100);
Button4pressed = 0;
}
if (Password != 3) {
digitalWrite (Error, HIGH);
delay (250);
digitalWrite (Error, LOW);
delay (250);
digitalWrite (Error, HIGH);
delay (250);
digitalWrite (Error, LOW);
delay (250);
Button1pressed = 0;
Button2pressed = 0;
Button3pressed = 0;
Button4pressed = 0;
Password = 0;
SafetyCheck = 0;
SafetyCheck2 = 0;
SafetyCheck3 = 0;
}
}
if (Password == 4 && SafetyCheck == 1 && SafetyCheck3 == 1 && SafetyCheck2 ==1) {
digitalWrite (LED, HIGH);
delay (250);
digitalWrite (LED, LOW);
delay (250);
digitalWrite (LED, HIGH);
delay (250);
digitalWrite (LED, LOW);
delay (250);
Button1pressed = 0;
Button2pressed = 0;
Button3pressed = 0;
Button4pressed = 0;
Password = 0;
}
}
If this is too much for you, an .ino is attached, along with a proteus project file so you can run this example on Proteus.
What happens is the program ignores the ButtonPressed Values and goes straight to the Error sequence.
I am only a beginner, so the issue may be staring me in the face and i am not going to notice it. If you could explain what is wrong, i would be very grateful.
File.zip (15.4 KB)
sketch_aug11c.ino (3.47 KB)