First ARDUINO project.

Hi, A friend and I got our very first project with an ARDUINO. The problem is that we have no experience in working with an ARDUINO :cold_sweat:. We have to make a simple 3 digit code wich will let a green LED glow. The digits are entered by clicking buttons on the board. The first button = 1, the second = 2 and the third = 3. We choice our code to be 312. So after pressing these in a row the green LED wil glow. When the code pressed is wrong for example 123 the red LED should glow. We have to finish this by monday and our teacher is not reachable in this time, so if we could get any help on this it would be appreciated. Thank you, Oolino

Ps: Sorry for my terrible english hope you can still make sense out of my story.

There is usually only one button on an Arduino, and that is the reset button.

1) Go to the main arduino website and click on 'learning'. You will find examples for how to read a single button, how to make a single led blink. 2) Then learn how to use "if(a==5)" and "if( (a==5) && (b==4) )" and the likes. This will let you check for a code sequence. 3) It may be useful to write down "pseudo-code" that you understand to describe what you want the thing to do. Process it in your head and see if it works. 3) PLAY with it.

Remember to ALWAYS use a resistor with an LED.

Thanks alot for your advice ;) The problem is that we dont know how the red light can burn only when the three digits are entered and not just anytime.

How are you programming the buttons? Is there 3 buttons, each represent the 1st digit, 2nd digit, and 3rd? If so, do you plan on pressing button 1 3 times in order for the Arduino to recognize it as 3? Or, are you doing it over the Serial Monitor and have to type in your number?

If you are doing either of those, the way to program the logic behind it is very different. If Serial, you can tell the program to wait until it receives 3 digits. For the buttons method, you could basically have the buttons all start off as null, until they are pressed at least once. You could do that by assigning them all values of 0 until the program recognizes that the button has been pressed. However, by doing it this way, you could never use 0 as a digit, but instead only 1-9. Unless of course, you did some tricky setup which is possible.

If this is a learning exercise, it isn't too good for us to give you the answer, so don't expect the code to be completed for you. What you are trying to do isn't complex, as it is fundamental basics of programming. However, you will never become a strong coder until you easily understand those basic concepts.

You haven't told us anything about the buttons, so help won't be easy.

oke so there are just three buttons next to each other each representing a digit the first one = 1 the second = 2 and the third = 3. only if these are clicked in the correct sequince the green LED will shine. And i know that ofcourse we shoud not get all the code from you guys but whe where just looking for a start off. We just arent fammiliar with the code a ARDUINO uses and what its capable off. And thats why we need a little help ;). Thanks for the advice though! :D

That makes more sense. There are multiple ways you can do this, but because you are beginners, I'm going to point you in the easiest way I know for this.

1. You will need to make a loop function, and run 3 different digitalRead(pin#) lines.
2. You will want to assign an integer value to each of these reads, such as int Pin1 = digitalRead(pin#);
3. You will need to create a big, or multiple 'if' statements to figure out their order. This is the part you will need to code as I won't give it all away. However, consider this direction:
``````if((Pin1 == LOW) && (Pin2 == LOW) && (Pin3 == HIGH))
{
FirstPin = 3;
}
``````

Then eventually you can run an 'if' statement to see if your First/Second/Third are equal to your password.

The rest you should be able to find in the examples. You just have to try.

Alright! Thanks i get it now. I will be working on this tomorrow thanks a lot!

Ok what whe have so far is this:

``````/ 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;

// 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);
}

void loop()
{
// read the state of the pushbutton value:

if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW)
{
firstCode = 1;
}
if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW)
{
firstCode = 2;
}
if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH)
{
firstCode = 3;
}
if( buttonStateOne != lastButtonState && buttonStateTwo != lastButtonState && buttonStateThree != lastButtonState)
{
if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW)
{
secondCode = 1;
}
if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW)
{
secondCode = 2;
}
if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH)
{
secondCode = 3;
}
if( buttonStateOne != lastButtonState && buttonStateTwo != lastButtonState && buttonStateThree != lastButtonState)
{
if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW)
{
thirdCode = 1;
}
if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW)
{
thirdCode = 2;
}
if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH)
{
thirdCode = 3;
}
}
}
if (firstCode == 3 && secondCode == 3 && thirdCode == 1)
{
digitalWrite(ledPin,HIGH);
digitalWrite(ledPin2,LOW);
}
else
{
digitalWrite(ledPin,LOW);
digitalWrite(ledPin2,HIGH);
}

}
``````

But it doesnt do anything its annoying that we cant see whats the problem any of you can?

A friend and I are making a sequence passcode with an ARDUINO. If you click the buttons on our board in the correct sequence the green LED should lit. We made a piece of code wich we thought would be correct but nothing is happening. Does anyone has an idea whats the problem? Ok what whe have so far is this:

``````/ 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;

// 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);
}

void loop()
{
// read the state of the pushbutton value:

if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW)
{
firstCode = 1;
}
if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW)
{
firstCode = 2;
}
if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH)
{
firstCode = 3;
}
if( buttonStateOne != lastButtonState && buttonStateTwo != lastButtonState && buttonStateThree != lastButtonState)
{
if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW)
{
secondCode = 1;
}
if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW)
{
secondCode = 2;
}
if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH)
{
secondCode = 3;
}
if( buttonStateOne != lastButtonState && buttonStateTwo != lastButtonState && buttonStateThree != lastButtonState)
{
if (buttonStateOne == HIGH && buttonStateTwo == LOW && buttonStateThree == LOW)
{
thirdCode = 1;
}
if (buttonStateOne == LOW && buttonStateTwo == HIGH && buttonStateThree == LOW)
{
thirdCode = 2;
}
if (buttonStateOne == LOW && buttonStateTwo == LOW && buttonStateThree == HIGH)
{
thirdCode = 3;
}
}
}
if (firstCode == 3 && secondCode == 3 && thirdCode == 1)
{
digitalWrite(ledPin,HIGH);
digitalWrite(ledPin2,LOW);
}
else
{
digitalWrite(ledPin,LOW);
digitalWrite(ledPin2,HIGH);
}

}
``````

By the way, the red light (pin2) should always be lid unless the code is correct, and it is so thats not the problem. Thanks, Oolino

We can't see what your program is doing, so you need to add some serial debug prints.

Uhmm sorry what are those.

You scatter Serial.println through your code, to allow you to track the the state of variables, and see which way your program flow is going.

So if i add Serial.println(thirdCode) i should the value of the variable throughout the code.

But don't forget a Serial.begin in setup

and it would help if each one is preceded by something like Serial.println("step xxx"); so we know where we are in the code!

Also, to save you the headache in case you haven't done it before:

1. Insert a library to the code: Sketch > Import Library > SoftwareSerial
2. In the void setup(): Serial.begin(9600) --- You can increase the 9600, but that is quite fast for your needs
3. In the code we are talking about doing a Serial.print command to help identify where your error is:
``````//First section of the code
Serial.println("We successfully began the sequence!");

//Second part of code
Serial.println("Ok, and made it to stage 2");

//Third and so on.
``````

That way as you try to run the sketch, the program will spit out those words and you can see exactly where it stopped. This is a great way to troubleshoot your code.

@DaBeej, Why do they need SoftwareSerial?

Whoops, you are right. I just woke up and my head is in another project. OP - Scratch my #1 point! Good catch AWOL.