I am new to this, and this is my first time trying to code something myself, so please excuse any bad form in the programming.
I am simply trying to get three LED's to cycle which one is on based off of a button press. The code that I have will only turn on the first LED, and that is it. I can't figure out why, and after browsing the forums for a bit, I give up and am asking for help. I am using an Arduino UNO for this.
My code:
/*
* Button_Press
*
* On a button press, will change
* from one pin having high output
* to another
*
*/
// Pins used, and what they're used for
// out1 is first, followed by 2, then 3
const int out1 = 9;
const int out2 = 10;
const int out3 = 11;
const int in1 = 2;
void setup()
{
//setting pins up for input and output
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
pinMode(out3, OUTPUT);
pinMode(in1, INPUT);
//turning all ouputs off
digitalWrite(out1, LOW);
digitalWrite(out2, LOW);
digitalWrite(out3, LOW);
}
void loop()
{
int pressCount = 0; // used to count how many times button was pressed
int buttonState; // Holds the button state. LOW if pushed, HIGH if not
buttonState = digitalRead(in1); // Reading input from button
// when button is pushed, pressCount increases
if (buttonState == LOW)
{
pressCount++;
// When presscount reaches 4, resets to 0
if (pressCount == 4)
pressCount = 0;
}
if (pressCount == 1)
{
digitalWrite(out1, HIGH); // Turns on the first LED
digitalWrite(out2, LOW); // Makes sure the other 2 are off
digitalWrite(out3, LOW);
}
else if (pressCount == 2)
{
digitalWrite(out2, HIGH); // Turns on the first LED
digitalWrite(out1, LOW); // Makes sure the other 2 are off
digitalWrite(out3, LOW);
}
else if (pressCount == 3)
{
digitalWrite(out3, HIGH); // Turns on the first LED
digitalWrite(out1, LOW); // Makes sure the other 2 are off
digitalWrite(out2, LOW);
}
}
Pictures of the breadboard setup I am using:
Sorry about the link to pictures.
I am using 30 ohm resistors with the LED's, and a 10k ohm resistor as a pull-up with the button.
If any other information is needed to help, please let me know