I'm a beginner having trouble with a password activated LED. Help appreciated!

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)

Without going through your logic in too much detail, I notice you aren't debouncing the buttons. Is there a chance your buttons are bouncing and causing false out of sequence readings?

 if (Button1state == HIGH) {

   Button1pressed = 1;
 }

 if (Button2state == HIGH) {

   Button2pressed = 1;
 }

 if (Button3state == HIGH) {

   Button3pressed = 1;
 }

 if (Button4state == HIGH) {

   Button4pressed = 1;
 }

these lines of code convert a simple button press into a permanent value. the buttons aren't connected to the leds themselves in anyway.

There are 10k ohm resistors connected to pins 2, 3,4, and 5 that go to ground, eliminating noise. Plus i am using a simulator.

@ FroggierSnow7:

Holy.... man thats hard to follow.. Lets see. So basically you are trying to make an led sequence that is activated by a password right. I assume you must have 2 sequence to say the least. 1 for correct input and one for the wrong input?

now for simplicity sake. why not use a library. The tried and tested one like bounce2. Those are one of the best.

second use an array.

save the value of your password.

3rd. use function

if you could please dont ever use delay.. and I mean never.... avoid it like its the plague.

I don't think that debouncing is the issue here; there are delays if a button is pressed.

How are your buttons wired? Are you using pulldown resistors? If not, your inputs are floating and can have any value.

The easiest way to wire buttons is to connect them between pin and ground and enable internal pullup resistors; e.g.

  pinMode(Button1, INPUT_PULLUP);

Note that the logic will now be reversed; when a button is pressed, the signal will be low.

At least three if statements has a single = instead of ==.