3 buttons, 2 LEDS assignment

hey guys,

im new to arduino and for a schoolproject a mate and I got an assignment. we had to make some sort of 'Safe', there are 3 buttons on the arduino and also a green and a red LED. The red LED should be on all the time. if a specific code was entered by pressing on the buttons (something like 331) the red LED should go off and the green LED should turn on. seems like an easy thing to do, but after spending hours and hours of work we just cant figure it out. So i ask you fellas to help us out, please keep it simple since we are new and try to answer in simple English so we understand. anyway, thanks for the ones that help!

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin1 = 2;   // the number of the pushbutton pin
const int buttonPin2 = 3; 
const int buttonPin3 = 4; 

const int ledPin =  7;      // the number of the LED pin
const int ledPin2 =  6; 

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

//int for code check
int firstCode;
int secondCode;
int thirdCode;

//int for checking the number of clicks
int numberClicks = 0;

// variables will change:
// variable for reading the pushbutton status
int buttonStateOne = 0;
int buttonStateTwo = 0;
int buttonStateThree = 0;
int lastButtonState = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT); 
  pinMode(buttonPin2, INPUT);     
  pinMode(buttonPin3, INPUT);   
// initialize serial
Serial.begin(9600);    
}

void loop()
{
  // read the state of the pushbutton value:
  buttonStateOne = digitalRead(buttonPin1);
  buttonStateTwo = digitalRead(buttonPin2);
  buttonStateThree = digitalRead(buttonPin3);
   
   
   if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW && numberClicks == 0)
   {
     firstCode = 1;
numberClicks = 1;
Serial.println(TEST)
   }
   if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW && numberClicks == 0)
   {
     firstCode = 2;
numberClicks = 1;
   }
   if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH && numberClicks == 0)
   {
     firstCode = 3;
numberClicks = 1;
   }
     if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW && numberClicks == 1)
   {
     secondCode = 1;
numberClicks = 2;
   }
   if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW && numberClicks == 1)
   {
     secondCode = 2;
numberClicks = 2;
   }
   if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH && numberClicks == 1)
   {
     secondCode = 3;
numberClicks = 2;
   }
     if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW && numberClicks == 2)
   {
     thirdCode = 1;
numberClicks = 3;
   }
   if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW&& numberClicks == 2)
   {
     thirdCode = 2;
numberClicks = 3;
   }
   if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH&& numberClicks == 2)
   {
     thirdCode = 3;
numberClicks = 3;
   }
   if (firstCode == 3 && secondCode == 3 && thirdCode == 1)
   {
     digitalWrite(ledPin,HIGH);
     digitalWrite(ledPin2,LOW);
   }
   else
   {
     digitalWrite(ledPin,LOW);
     digitalWrite(ledPin2,HIGH);
   }
   
}

You have a call to Serial.begin() in setup. Do you know why you put that there?

It enables the Arduino to communicate with the PC. Specifically, the Serial Monitor application that is launched by pressing the rightmost icon on the top row.

All that you are missing is the code to make the Arduino actually print stuff to the serial monitor. That would be one or more calls to Serial.print() and/or Serial.println().

You have not enabled the internal pullup resistors. This means that you have external resistors wired with the switches. Right?

You can't use one variable to keep track of the previous state of three buttons. You aren't using this variable anyway.

Serial.println(TEST)

Tell me this compiled. I know it didn't.

ok i dont perfectly understand it.
how do i use this variable then? and how do i keep track of the previous state of three buttons?

how do i keep track of the previous state of three buttons?

The same way you keep track of the current state of three buttons - use three variables.

how do i use this variable then?

int currState;
int prevState = LOW;
int somePin = 2;
void loop()
{
  currState = digitalRead(somePin);
  if(currState != prevState)
  {
    // The switch state changed. Did it change
    // from pressed to released or from released
    // to pressed?
    if(currState == HIGH)
    {
       // State is now HIGH and was LOW
       // Depending on how the switch is
       // wired, this may mean pressed now
       // or it may mean released now.
       Serial.println("Switch is pressed"); // Change to released if it doesn't match how YOUR switch behaves
    }
  }
  prevState = currState;
}

Asking again: How are your switches wired?


this is how we build it up.

Depending on how your switches are actually oriented, that looks good.

So, running the code in reply #3 then, for each pin (2, 3, and 4) should show consistent results (except that deboucing is not implemented, yet. One issue at a time).

hmm but when we use ur code we get this error:
core.a(main.cpp.o): In function main': C:\Users\Casper\Desktop\arduino-0023\hardware\arduino\cores\arduino/main.cpp:7: undefined reference to setup'

So, define setup(). That was meant to demonstrate what loop() should look like, not be a complete sketch.

Ok thanks thats working but its giving the message alot of times if i press it.

Ok thanks thats working but its giving the message alot of times if i press it.

How many is "alot"? Is there a debouncing issue? There really shouldn't be, as the Serial.print() takes time, effectively debouncing the switch.

Try adding:

Serial.print("Current state: ");
Serial.println(currState);
Serial.print("Previous state: ");
Serial.println(prevState);
delay(250);

before

   prevState = currState;

Also, add:

    else
    {
       Serial.println("Switch is released");
    }

after the if statement.

o yes thats working but how do we add this in our code? do we have to paste it everywhere?

We fixed it! Thanks for the help!

// constants won't change. They're used here to 
// set pin numbers:
int currState;
int prevState = LOW;
const int buttonPin1 = 2;   // the number of the pushbutton pin
const int buttonPin2 = 3; 
const int buttonPin3 = 4; 

const int ledPin =  7;      // the number of the LED pin
const int ledPin2 =  6; 

//int for code check
int firstCode;
int secondCode;
int thirdCode;

//Debounce
int debounceTijd = 100;

//int for checking the number of clicks
int numberClicks = 0;

// variables will change:
// variable for reading the pushbutton status
int buttonStateOne = 0;
int buttonStateTwo = 0;
int buttonStateThree = 0;
int lastButtonState = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT); 
  pinMode(buttonPin2, INPUT);     
  pinMode(buttonPin3, INPUT);   
// initialize serial
Serial.begin(9600);    
}

void loop()
{
  // read the state of the pushbutton value:
  buttonStateOne = digitalRead(buttonPin1);
  buttonStateTwo = digitalRead(buttonPin2);
  buttonStateThree = digitalRead(buttonPin3);
   
   
   if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW && numberClicks == 0)
   {
     firstCode = 1;
numberClicks = 1;
 Serial.println("Switch is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateOne);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateOne = prevState;
     
   }
   if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW && numberClicks == 0)
   { 
     Serial.println("Switch is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateTwo);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateTwo = prevState;
     firstCode = 2;
numberClicks = 1;
    
   }
   if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH && numberClicks == 0)
   {
  Serial.println("Switch is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateThree);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateThree = prevState;
     firstCode = 3;
numberClicks = 1;
     
   }
     if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW && numberClicks == 1)
   {
      Serial.println("Switch2 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateOne);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateOne = prevState;
     secondCode = 1;
numberClicks = 2;
     
   }
   if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW && numberClicks == 1)
   {
      Serial.println("Switch2 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateTwo);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateTwo = prevState;
     secondCode = 2;
numberClicks = 2;
     
   }
   if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH && numberClicks == 1)
   {
      Serial.println("Switch2 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateThree);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateThree = prevState;
     secondCode = 3;
numberClicks = 2;
     
   }
     if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW && numberClicks == 2)
   {
 Serial.println("Switch3 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateOne);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateOne = prevState;
     thirdCode = 1;
numberClicks = 3;
     
   }
   if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW&& numberClicks == 2)
   {
     Serial.println("Switch3 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateTwo);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateTwo = prevState;
     thirdCode = 2;
numberClicks = 3;
     
   }
   if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH&& numberClicks == 2)
   {
    Serial.println("Switch3 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateThree);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateThree = prevState;
     thirdCode = 3;
numberClicks = 3;
     
   }
   if (firstCode == 3 && secondCode == 3 && thirdCode == 1)
   {
     digitalWrite(ledPin,HIGH);
     digitalWrite(ledPin2,LOW);
     delay (3000)
   }
   else if (numberClicks == 3)
   {
     digitalWrite(ledPin2,LOW);
     delay(500);
     digitalWrite(ledPin2,HIGH);
     delay(500);
     digitalWrite(ledPin2,LOW);
     delay(500);
     digitalWrite(ledPin2,HIGH);
     delay(500);
     digitalWrite(ledPin2,LOW);
     delay(500);
     digitalWrite(ledPin2,HIGH);
     delay(500);

     numberClicks = 0;
     firstCode = 0;
     secondCode = 0;
     thirdCode = 0;
     buttonStateOne = 0;
     buttonStateTwo = 0;
     buttonStateThree = 0;
   }
   else
   {
     digitalWrite(ledPin,LOW);
     digitalWrite(ledPin2,HIGH);
   }
   
   
}

This one is the 2014 assignment version:

// constants won't change. They're used here to 
// set pin numbers:
int currState;
int prevState = LOW;
const int buttonPin1 = 2;   // the number of the pushbutton pin
const int buttonPin2 = 3; 
const int buttonPin3 = 4; 

const int ledGroen =  7;      // the number of the LED pin
const int ledRood =  6; 

//int for code check
int firstCode;
int secondCode;
int thirdCode;
int fourthCode;

//Debounce
int debounceTijd = 100;

//int for checking the number of clicks
int numberClicks = 0;

// variables will change:
// variable for reading the pushbutton status
int buttonStateOne = 0;
int buttonStateTwo = 0;
int buttonStateThree = 0;
int lastButtonState = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledGroen, OUTPUT);
  pinMode(ledRood, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT); 
  pinMode(buttonPin2, INPUT);     
  pinMode(buttonPin3, INPUT);   
// initialize serial
Serial.begin(9600);    
}

void loop()
{
  // read the state of the pushbutton value:
  buttonStateOne = digitalRead(buttonPin1);
  buttonStateTwo = digitalRead(buttonPin2);
  buttonStateThree = digitalRead(buttonPin3);
   
   
   if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW && numberClicks == 0)
   {
     firstCode = 1;
numberClicks = 1;
 Serial.println("Switch is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateOne);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateOne = prevState;
     
   }
   if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW && numberClicks == 0)
   { 
     Serial.println("Switch is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateTwo);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateTwo = prevState;
     firstCode = 2;
numberClicks = 1;
    
   }
   if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH && numberClicks == 0)
   {
  Serial.println("Switch is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateThree);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateThree = prevState;
     firstCode = 3;
numberClicks = 1;
     
   }
     if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW && numberClicks == 1)
   {
      Serial.println("Switch2 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateOne);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateOne = prevState;
     secondCode = 1;
numberClicks = 2;
     
   }
   if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW && numberClicks == 1)
   {
      Serial.println("Switch2 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateTwo);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateTwo = prevState;
     secondCode = 2;
numberClicks = 2;
     
   }
   if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH && numberClicks == 1)
   {
      Serial.println("Switch2 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateThree);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateThree = prevState;
     secondCode = 3;
numberClicks = 2;
     
   }
     if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW && numberClicks == 2)
   {
 Serial.println("Switch3 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateOne);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateOne = prevState;
     thirdCode = 1;
numberClicks = 3;
     
   }
   if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW&& numberClicks == 2)
   {
     Serial.println("Switch3 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateTwo);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateTwo = prevState;
     thirdCode = 2;
numberClicks = 3;
     
   }
   if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH&& numberClicks == 2)
   {
    Serial.println("Switch3 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateThree);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateThree = prevState;
     thirdCode = 3;
numberClicks = 3;
     
   }
   //TESTETSTTETETETSTETETSTETETSTETSTETTETSETTETSETEST
   if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW&& numberClicks == 3)
   {
    Serial.println("Switch2 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateTwo);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateTwo = prevState;
     fourthCode = 2;
numberClicks = 4;
     
   }
   
  if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW&& numberClicks == 3)
   {
    Serial.println("Switch2 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateTwo);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateTwo = prevState;
     fourthCode = 1;
numberClicks = 4;
     
   }   
   
    if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH&& numberClicks == 3)
   {
    Serial.println("Switch2 is pressed");
    Serial.print("Current state: ");
    Serial.println(buttonStateTwo);
    Serial.print("Previous state: ");
    Serial.println(prevState);
    delay(250);
    buttonStateTwo = prevState;
     fourthCode = 3;
numberClicks = 4;
     
   } 
   
   
   
   if (firstCode == 3 && secondCode == 3 && thirdCode == 1 && fourthCode == 1)
   {
     digitalWrite(ledGroen,HIGH);
     digitalWrite(ledRood,LOW);
     delay (3000);
     digitalWrite(ledGroen,LOW);
     
     numberClicks = 0;
     firstCode = 0;
     secondCode = 0;
     thirdCode = 0;
     fourthCode = 0;
     buttonStateOne = 0;
     buttonStateTwo = 0;
     buttonStateThree = 0;
   }
   else if (numberClicks == 4)
   {
     digitalWrite(ledRood,LOW);
     delay(500);
     digitalWrite(ledRood,HIGH);
     delay(500);
     digitalWrite(ledRood,LOW);
     delay(500);
     digitalWrite(ledRood,HIGH);
     delay(500);
     digitalWrite(ledRood,LOW);
     delay(500);
     digitalWrite(ledRood,HIGH);
     delay(500);

     numberClicks = 0;
     firstCode = 0;
     secondCode = 0;
     thirdCode = 0;
     fourthCode = 0;
     buttonStateOne = 0;
     buttonStateTwo = 0;
     buttonStateThree = 0;
   }
   else
   {
     digitalWrite(ledGroen,LOW);
     digitalWrite(ledRood,HIGH);
   }
   
   
}

It's a nice and basic way of handling this. If you want to continue on this assignment, maybe also read up on Finite-state machine - Wikipedia
Now it just keeps waiting for 4 presses, and then shows if it's right or not (and if someone pressed two buttons, you'll have to complete his password attempt before you can start your own).

Maybe this is the way it was requested. But if i see how these things work in reality, any sequence of the right combination is accepted, no matter what came before it. And Finite state machines is how this is usually implemented :).