I'm currently working on a simple project that revolves toggling 3 different LEDs on and off using 3 different toggles.
I have the wiring set up and coding completed, but for some reason, all 3 LEDs remained toggled on all the time.
I would recommend simplifying your schematic by removing your pulldown resistors at each button. Instead, you can use UNO's built-in pullup resistors on each button switch by using PinMode() function with mode option: INPUT_PULLUP.
To learn how to toggle LED, take a look at the Arduino Tutorials "Debounce" example code.
Thanks for the advice, Ill look into that in a bit.
Here's the codes I have for the set up:
int ledPin = 13;
int buttonPin = 12;
int ledPin2 = 10;
int buttonPin2 = 11;
int ledPin3 = 8;
int buttonPin3 = 9;
boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state
boolean ledState = LOW;//storage for the current state of the LED (off/on)
void setup(){
pinMode(buttonPin, INPUT);//this time we will set the pin as INPUT
pinMode(ledPin, OUTPUT);
pinMode(buttonPin2, INPUT);//this time we will set the pin as INPUT
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin3, INPUT);//this time we will set the pin as INPUT
pinMode(ledPin3, OUTPUT);
Serial.begin(9600);//initialize Serial connection
}
void loop(){
currentState = digitalRead(buttonPin);
if (currentState == HIGH && lastState == LOW){//if button has just been pressed
Serial.println("pressed");
delay(1);//crude form of button debouncing
}
currentState = digitalRead(buttonPin2);
if (currentState == HIGH && lastState == LOW){//if button has just been pressed
Serial.println("pressed");
delay(1);//crude form of button debouncing
}
currentState = digitalRead(buttonPin3);
if (currentState == HIGH && lastState == LOW){//if button has just been pressed
Serial.println("pressed");
delay(1);//crude form of button debouncing
}
//toggle the state of the LED
if (ledState == HIGH){
digitalWrite(ledPin, LOW);
ledState = LOW;
} else {
digitalWrite(ledPin, HIGH);
ledState = HIGH;
}
if (ledState == HIGH){
digitalWrite(ledPin2, LOW);
ledState = LOW;
} else {
digitalWrite(ledPin2, HIGH);
ledState = HIGH;
}
if (ledState == HIGH){
digitalWrite(ledPin3, LOW);
ledState = LOW;
} else {
digitalWrite(ledPin3, HIGH);
ledState = HIGH;
}
lastState = currentState;
}
I am rather new to programming so I do apologise if I made some obvious mistakes.
Please use code blocks when posting code. This reduces scrolling clutter for other readers here.
Your using the same variable "currentState" for each button. You will need to provide a unique "currentState" variable for each button ( e.g., currentStateButton1, currentStateButton2, etc.). Then use a conditional statement to update each button and associated LED if any state changes are detected.
Anyway, It's not working still, probably cuz Im coding it wrong somehow.
Here's the updated program:
//Button Toggle LED
int ledPin = 13;
int buttonPin = 12;
int ledPin2 = 10;
int buttonPin2 = 11;
int ledPin3 = 8;
int buttonPin3 = 9;
boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state
boolean ledState = LOW;//storage for the current state of the LED (off/on)
boolean currentState2 = LOW;//stroage for current button state
boolean lastState2 = LOW;//storage for last button state
boolean ledState2 = LOW;//storage for the current state of the LED (off/on)
boolean currentState3 = LOW;//stroage for current button state
boolean lastState3 = LOW;//storage for last button state
boolean ledState3 = LOW;//storage for the current state of the LED (off/on)
void setup(){
pinMode(buttonPin, INPUT);//this time we will set the pin as INPUT
pinMode(ledPin, OUTPUT);
pinMode(buttonPin2, INPUT);//this time we will set the pin as INPUT
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin3, INPUT);//this time we will set the pin as INPUT
pinMode(ledPin3, OUTPUT);
Serial.begin(9600);//initialize Serial connection
}
void loop(){
currentState = digitalRead(buttonPin);
if (currentState == HIGH && lastState == LOW){//if button has just been pressed
Serial.println("pressed");
delay(1);//crude form of button debouncing
}
currentState2 = digitalRead(buttonPin2);
if (currentState2 == HIGH && lastState2 == LOW){//if button has just been pressed
Serial.println("pressed");
delay(1);//crude form of button debouncing
}
currentState3 = digitalRead(buttonPin3);
if (currentState3 == HIGH && lastState3 == LOW){//if button has just been pressed
Serial.println("pressed");
delay(1);//crude form of button debouncing
}
//toggle the state of the LED
if (ledState == HIGH){
digitalWrite(ledPin, LOW);
ledState = LOW;
} else {
digitalWrite(ledPin, HIGH);
ledState = HIGH;
}
if (ledState2 == HIGH){
digitalWrite(ledPin2, LOW);
ledState2 = LOW;
} else {
digitalWrite(ledPin2, HIGH);
ledState2 = HIGH;
}
if (ledState3 == HIGH){
digitalWrite(ledPin3, LOW);
ledState3 = LOW;
} else {
digitalWrite(ledPin3, HIGH);
ledState3 = HIGH;
}
lastState = currentState;
lastState2 = currentState2;
lastState3 = currentState3;
}
but for some reason, all 3 LEDs remained toggled on all the time.
The code which toggles the led is not coupled to the button press state and will toggle the led independently of what the button is doing.
Try add LarryD's toggle code in the button block and remove the other toggle code
currentState = digitalRead(buttonPin);
if (currentState == HIGH && lastState == LOW){//if button has just been pressed
Serial.println("pressed");
digitalWrite(ledPin, !digitalRead(ledPin));
delay(1);//crude form of button debouncing
}
You really should store your pin numbers and states in arrays so that there will be only one block of logic in loop() instead of one for each LED.
So for your pins you would have:
int ledPins[] = {13,10,8};
int buttonPins[] = {12,11,9};
To loop through the pins you would do like this:
for(int i = 0; i < 3; i++)
{
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT);
}
Then in loop() you use the same technique to read the button pins and turn the LEDs on and off.
You do not need to store the state of each led, only the state of the buttons. Use the code in reply #4 to toggle the LEDs (when you are toggling you don't care if it is currently on or off, just set it to the opposite of what it is).