Trying to make button security

Hi,

I'm trying to make a button tap "security" code (like 1234, not programming code). I already wrote my code (arduino code) but i can't seem to get it to work.

Here's my code:

const int button1Pin = 7;  
const int button2Pin = 6;
const int button3Pin = 5;
const int button4Check = 4;  
int ledPin1 =  12;    
int ledPin2 =  11;

void setup() {
  // put your setup code here, to run once:
  // Set up the pushbutton pins to be an input:
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
  pinMode(button4Check, INPUT);

  // Set up the LED pin to be an output:
  pinMode(ledPin1, OUTPUT);   
  pinMode(ledPin2, OUTPUT);

  Serial.begin(9600);
  Serial.println("Little Test");

  // Setting up variables:
  
}

void loop() {
  //first button
  int x = 0; //count
  int r = 0; //store
  
  //second button
  int y = 0;
  int s = 0;
  
  //third button
  int z = 0;
  int t = 0;
  
  // put your main code here, to run repeatedly:
  int button1State, button2State, button3State, button4State;  // variables to hold the pushbutton states

  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
  button3State = digitalRead(button3Pin);
  button4State = digitalRead(button4Check);

  

  if ((button1State == LOW)  
     && !                                               
     (button2State, button3State, button4State == LOW)) {
      delay(1000);
      int r = ++r;
      Serial.println("r=r+1");

      }


     
     
  

  
  if ((button2State == LOW)  
     && !                                               
     (button1State, button3State, button4State == LOW)) {
      delay(1000);
      int s = ++s;
      Serial.println("s=s+1");
     
  }


  if ((button3State == LOW)  
     && !                                               
     (button2State, button1State, button4State == LOW)) {
      delay(1000);
      int t = ++t;
      Serial.println("t=t+1");
     
  }
  if ((button4State == LOW)  
     && !                                               
     (button2State, button3State, button1State == LOW)) {
      delay(1000);
      Serial.println("button 4");

      if ((r == 2)
         &&
         (s == 1)
         &&
         (t == 4)){
          digitalWrite(ledPin1, HIGH);
         }
    
      else {
        digitalWrite(ledPin2, HIGH);
      }
     }

}

What i'm trying to do is get a green LED to light up if i type in the good code. I use the serial monitor to test if my inputs are detected.

Results Serial:

Little Test
r=r+1
r=r+1
s=s+1
t=t+1
t=t+1
t=t+1
t=t+1

But whichever button i press, the same (red, ledPin2) LED keeps turning on...

I hope you guys can help me...

Thijs

firstly, update your code using variables that make sense, it's too difficult to understand what button1state is or what r is or what s is. I think while your doing that you'll find the problem.

also, your if statement says if something happens turn the LED off, else turn the LED on.

I'd recommend changing that to if something happens, turn the LED on, else turn it off.

What is this supposed to do?

  if ((button1State == LOW)
      && !
      (button2State, button3State, button4State == LOW))

If it needs to check for button1State low and the others HIGH, it can look something like below.

  if (button1State == LOW &&
      button2State == HIGH &&
      button3State == HIGH &&
      button4State == HIGH)
  {
    ...
    ...
  }

Also, how are your buttons wired? Are you using pull-up resistors or pull-down resistors; this will prevent floating inputs and hence unpredictable results. I think (but am not sure) that you wired the buttons between pin and ground; if so, use pinMode(yourpin, INPUT_PULLUP) to make use of the internal pull-up resistor.

Thank's for your responses, i tested if the buttons gave strange outputs with the serial monitor (If there was any, there would be text on the serial monitor). This is how my wires are wired. I'll update my variables to something that makes a bit more sense to the rest of the world ;) About the LED's, the if statement states that if something is right turn the led on, if it's not right turn the other led on, right?

      if ((r == 2)
      &&
      (s == 1)
      &&
      (t == 4)){
        digitalWrite(ledPin1, HIGH)
      }
   
      else {
        digitalWrite(ledPin2, HIGH);
      }

But do you know what (besides the variables) might have caused the problem, 'cause i cant seem to figure it out...

There are a few more things wrong with your code.

void loop() {
  //first button
  int x = 0; //count
  int r = 0; //store

  //second button
  int y = 0;
  int s = 0;

  //third button
  int z = 0;
  int t = 0;

  ...
  ...

As you might know, loop is called constantly. Each time it is called, it will execute the above and all your variables will be set to 0 again. If you want to prevent that, either make those variables global or make the static.
GLOBAL

  //first button
  int x = 0; //count
  int r = 0; //store

  //second button
  int y = 0;
  int s = 0;

  //third button
  int z = 0;
  int t = 0;

void loop()
{
  ...
  ...
}

STATIC

void loop() {
  //first button
  static int x = 0; //count
  static int r = 0; //store

  //second button
  static int y = 0;
  static int s = 0;

  //third button
  static int z = 0;
  static int t = 0;

  ...
  ...

The second problem is probably in the blocks like below

void loop() {
  //first button
  int x = 0; //count
/*>>*/  int r = 0; //store

  ...
  ...

  if ((button1State == LOW)
      && !
      (button2State, button3State, button4State == LOW)) {
    delay(1000);
/*>>*/    int r = ++r;
    Serial.println("r=r+1");

  }
  ...
  ...
}

Those two variables are NOT the same variables. The second one only exists inside the if block; the first one is NOT used inside the if block and will not change.

Read up on scope of variables. You probably want

  if ((button1State == LOW)
      && !
      (button2State, button3State, button4State == LOW)) {
    delay(1000);
    r = ++r;
    Serial.println("r=r+1");

Dankjewel ( ;) ) Sterretje,

That loop thing was pretty stupid :confused: but i would never have noticed those int's. My code is working now! Thank you very much!

Graag gedaan ( :D ) (for non-dutch: pleasure)