Hi! I'm quite new to Arduino and have been having some programming problems lately for a personal project. I have two pushbuttons and 5 LEDs and I want to be able to turn ON or OFF the LEDs based on the number of pushes saved in the code.
What I want my code to say is:
Push button1 ++;
Push Button2 --;
If pushes = 1, LED1 On and others off
and so on until 5. If I reach 5 or more pushes the 5 LEDs are ON, if I get to 0 all LEDs are OFF. I don't know if I explained myself quite well haha.
I have checked simpler codes to see if my circuit was working right and it does, and I tried tearing the code a part to see what parts are wrong but I can't seem to figure it out and I'm running out of options and examples to use. Here is what I have written until now:
const int button1 = 2; // increasing button
const int button2 = 3; // decreasing button
const int led1 = 11;
const int led2 = 10;
const int led3 = 9;
const int led4 = 6;
const int led5 = 5; // defining LEDs
int buttonPushCounter = 0; //pushcounter to know number of pushes after each loop
int buttonState = 0;
int lastButtonState = 0; //what was the last thing done with the buttons
int buttonState1 = 0; //to check what has button1 done
int buttonState2 = 0; //to check what has button2 done
// unsigned long currentMillis = 0; // put these to make it work with millis() after making it work with delay
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT); //all the LEDS
pinMode(button1, INPUT);
pinMode(button2, INPUT); //the 2 buttons
// currentMillis= millis(); //put these to make it work with millis() after making it work with delay
Serial.begin(9600); //to debug
}
void loop() {
buttonState1 = digitalRead(button1); // read state of button1
buttonState2 = digitalRead(button2); // read state of button2
if (buttonState != lastButtonState) { //check if button1 and button 2 have been pressed
if (buttonState1 == HIGH && buttonState2 == LOW)
{
buttonPushCounter ++; //if button1 has been pressed but not button2 +1 in counter
}
else if (buttonState2 == HIGH && buttonState1 == LOW)
{
buttonPushCounter --; //if button2 has been pressed but not button1 -1 in counter
}
else
{
delay(10); //if something else do nothing
}
}
lastButtonState = buttonState; // save button state so I can now amount of leds to turn on
controlled (); // run control LED void to know what LEDS to turn on
delay (100);// currentMillis= millis();
}
void controlled() { //void with code for on or off leds
if ( buttonPushCounter == 1 ) //if 1 was the pushcounter result turn LED1 ON
{
digitalWrite(led5, LOW);
digitalWrite(led4, LOW);
digitalWrite(led3, LOW);
digitalWrite(led2, LOW);
digitalWrite(led1, HIGH);
}
else if (buttonPushCounter == 2) //if 2 was the pushcounter result turn LED1 and LED2 ON
{
digitalWrite(led5, LOW);
digitalWrite(led4, LOW);
digitalWrite(led3, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led1, HIGH);
}
else if (buttonPushCounter == 3) //if 3 was the pushcounter result turn LED1 , LED2 and LED3 ON
{
digitalWrite(led5, LOW);
digitalWrite(led4, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led1, HIGH);
}
else if (buttonPushCounter == 4) //if 4 was the pushcounter result turn LED1, LED2 , LED3 and LED4 ON
{
digitalWrite(led5, LOW);
digitalWrite(led4, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led1, HIGH);
}
else if (buttonPushCounter >= 5) //if 5 or more was the pushcounter result turn all LEDs ON
{
digitalWrite(led5, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led1, HIGH);
}
else //if the pushcounter result is 0 turn all LEDs off
{
digitalWrite(led5, LOW);
digitalWrite(led4, LOW);
digitalWrite(led3, LOW);
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
}
} //end of code
I have also been trying to use millis() instead of delay because I have read it is better to use millis() than delay, but have been quite unable to get it to work on the code.
All help will be appreciated!!