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.
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;
}
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'
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 :).