I'm new to Arduino and I am working on a project for class. I would like each LED to be controlled by a separate button. (3 LEDs/ 3 Buttons).
I've tried everything, but no matter what I do all 3 LEDs are controlled by one button. If anyone could please help I would appreciate it so much! (using the Arduino Uno)
Code here:
// constants won't change. They're used here to set pin numbers:
const int button1Pin = 2; // the number of the pushbutton pin
const int led1Pin = 11; // the number of the LED pin
const int button2Pin = 3; // the number of the pushbutton pin
const int led2Pin = 12; // the number of the LED pin
const int button3Pin = 4; // the number of the pushbutton pin
const int led3Pin = 13; // the number of the LED pin
// variables will change:
//BUTTON 1 AND LED 1 HERE ------------------------------------
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(led1Pin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button1Pin, INPUT);
//LED 2------------------------------------------------------
pinMode(led2Pin, OUTPUT);
pinMode(button2Pin, INPUT);
//LED 3-------------------------------------------------------
pinMode(led3Pin, OUTPUT);
pinMode(button3Pin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(button1Pin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// LED 1---------------------------------------------------------------------------------
//turn LED on:
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
//LED 2--------------------------------------------------------------------------------------
if (buttonState == HIGH)
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, LOW);
digitalWrite(led1Pin, LOW);
//LED 3--------------------------------------------------------------------------------------
if (buttonState == HIGH)
digitalWrite(led3Pin, HIGH);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
}
}
You have one button state controlling all of the three LEDS
after the first LED control put
buttonState = digitalRead(Button2Pin)
after the Second LED control put
buttonState = digitalRead(Button3Pin);
// constants won't change. They're used here to set pin numbers:
const byte button1Pin = 2; // the number of the pushbutton pin
const byte led1Pin = 11; // the number of the LED pin
const byte button2Pin = 3; // the number of the pushbutton pin
const byte led2Pin = 12; // the number of the LED pin
const byte button3Pin = 4; // the number of the pushbutton pin
const byte led3Pin = 13; // the number of the LED pin
// variables will change:
//BUTTON 1 AND LED 1 HERE ------------------------------------
byte buttonState1 = 0; // variable for reading the pushbutton status
byte buttonState2 = 0; // variable for reading the pushbutton status
byte buttonState3 = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(led1Pin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button1Pin, INPUT_PULLUP);
//LED 2------------------------------------------------------
pinMode(led2Pin, OUTPUT);
pinMode(button2Pin, INPUT_PULLUP);
//LED 3-------------------------------------------------------
pinMode(led3Pin, OUTPUT);
pinMode(button3Pin, INPUT_PULLUP);
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(button1Pin);
buttonState2 = digitalRead(button2Pin);
buttonState3 = digitalRead(button3Pin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// LED 1---------------------------------------------------------------------------------
//turn LED on:
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
//LED 2--------------------------------------------------------------------------------------
if (buttonState2 == HIGH)
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, LOW);
digitalWrite(led1Pin, LOW);
//LED 3--------------------------------------------------------------------------------------
if (buttonState3 == HIGH)
digitalWrite(led3Pin, HIGH);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
}
}
Thank you for your response. I appreciate your help.
However a problem still persists where after I run the code, all of the LEDs stay on and eventually turn off and back on (I think it's following the loop part of the code). If I press any button only the first LED (red) brightens slightly. Do you have any advice?
I'm gonna attach a picture so you can see what I mean. (I know the breadboard looks chaotic LOL this is the biggest one my school has)
if( . . . )
{
//do something
//do another thing
//do one last thing
}
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == HIGH)
{
// LED 1---------------------------------------------------------------------------------
//turn LED on:
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
}
// constants won't change. They're used here to set pin numbers:
const byte button1Pin = 2; // the number of the pushbutton pin
const byte led1Pin = 11; // the number of the LED pin
const byte button2Pin = 3; // the number of the pushbutton pin
const byte led2Pin = 12; // the number of the LED pin
const byte button3Pin = 4; // the number of the pushbutton pin
const byte led3Pin = 13; // the number of the LED pin
// variables will change:
//BUTTON 1 AND LED 1 HERE ------------------------------------
byte buttonState1 = 0; // variable for reading the pushbutton status
byte buttonState2 = 0; // variable for reading the pushbutton status
byte buttonState3 = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(led1Pin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button1Pin, INPUT_PULLUP);
//LED 2------------------------------------
pinMode(led2Pin, OUTPUT);
pinMode(button2Pin, INPUT_PULLUP);
//LED 3----------------------------------
pinMode(led3Pin, OUTPUT);
pinMode(button3Pin, INPUT_PULLUP);
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(button1Pin);
buttonState2 = digitalRead(button2Pin);
buttonState3 = digitalRead(button3Pin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == HIGH)
{
// LED 1----------------------------------------
//turn LED on:
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
}
//LED 2---------------------------------------
if (buttonState2 == HIGH)
{
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, LOW);
digitalWrite(led1Pin, LOW);
}
//LED 3-------------------------------------
if (buttonState3 == HIGH)
{
digitalWrite(led3Pin, HIGH);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
}
Oh! I fixed it, it was just missing a } at the end.
Do you have any advice if they are all still lighting up for every button? They all slightly light up more when a button is pressed.
Updated code:
// constants won't change. They're used here to set pin numbers:
const byte button1Pin = 2; // the number of the pushbutton pin
const byte led1Pin = 11; // the number of the LED pin
const byte button2Pin = 3; // the number of the pushbutton pin
const byte led2Pin = 12; // the number of the LED pin
const byte button3Pin = 4; // the number of the pushbutton pin
const byte led3Pin = 13; // the number of the LED pin
// variables will change:
//BUTTON 1 AND LED 1 HERE ------------------------------------
byte buttonState1 = 0; // variable for reading the pushbutton status
byte buttonState2 = 0; // variable for reading the pushbutton status
byte buttonState3 = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(led1Pin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button1Pin, INPUT_PULLUP);
//LED 2------------------------------------
pinMode(led2Pin, OUTPUT);
pinMode(button2Pin, INPUT_PULLUP);
//LED 3----------------------------------
pinMode(led3Pin, OUTPUT);
pinMode(button3Pin, INPUT_PULLUP);
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(button1Pin);
buttonState2 = digitalRead(button2Pin);
buttonState3 = digitalRead(button3Pin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == HIGH)
{
// LED 1----------------------------------------
//turn LED on:
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
}
//LED 2---------------------------------------
if (buttonState2 == HIGH)
{
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, LOW);
digitalWrite(led1Pin, LOW);
}
//LED 3-------------------------------------
if (buttonState3 == HIGH)
{
digitalWrite(led3Pin, HIGH);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
}
}
Hello
Try and check out this modification to your code:
// constants won't change. They're used here to set pin numbers:
const byte button1Pin = 2; // the number of the pushbutton pin
const byte led1Pin = 11; // the number of the LED pin
const byte button2Pin = 3; // the number of the pushbutton pin
const byte led2Pin = 12; // the number of the LED pin
const byte button3Pin = 4; // the number of the pushbutton pin
const byte led3Pin = 13; // the number of the LED pin
void setup() {
// initialize the LED pin as an output:
pinMode(led1Pin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button1Pin, INPUT_PULLUP);
//LED 2------------------------------------
pinMode(led2Pin, OUTPUT);
pinMode(button2Pin, INPUT_PULLUP);
//LED 3----------------------------------
pinMode(led3Pin, OUTPUT);
pinMode(button3Pin, INPUT_PULLUP);
}
void loop() {
// switch LED with related button:
digitalWrite(led1Pin, !digitalRead(button1Pin));
digitalWrite(led2Pin, !digitalRead(button2Pin));
digitalWrite(led3Pin, !digitalRead(button3Pin));
}
Have nice day and enjoy coding in C++.
p.s.
This is a nice task to start to use OOP.
The object is the LED with related button and the method takes care about the action to be done.
Thank you so much for your reply! I'm trying it out, and it's a lot better. The only thing is that all of the pushbuttons are controlling the red LED (pin 11).
Hmm.. I'm gonna have to check this out. My class has only covered the basic examples Arduino provides inside the program, so this is all new to me ahaha
Hi,
Can you try this bit of code please?
All it does is cycle each LED ON then OFF, there should only be ONE LED on at a time.
// constants won't change. They're used here to set pin numbers:
const byte button1Pin = 2; // the number of the pushbutton pin
const byte led1Pin = 11; // the number of the LED pin
const byte button2Pin = 3; // the number of the pushbutton pin
const byte led2Pin = 12; // the number of the LED pin
const byte button3Pin = 4; // the number of the pushbutton pin
const byte led3Pin = 13; // the number of the LED pin
void setup()
{
// initialize the LED pin as an output:
pinMode(led1Pin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button1Pin, INPUT_PULLUP);
//LED 2------------------------------------
pinMode(led2Pin, OUTPUT);
pinMode(button2Pin, INPUT_PULLUP);
//LED 3----------------------------------
pinMode(led3Pin, OUTPUT);
pinMode(button3Pin, INPUT_PULLUP);
}
void loop()
{
// cycle each LED in turn
digitalWrite(led1Pin, HIGH);
delay(500);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, HIGH);
delay(500);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, HIGH);
delay(500);
digitalWrite(led3Pin, LOW);
}
How have you got your buttons wired now.
Can you please post a circuit diagram and picture of your project.?
Is it still the same as in post #11?
If so you are using pull down resistors and the code is for internal pull_up resistors.
Yes this code is a lot better, just have to figure out how to get it to the other LEDs.
Yes it is still the same. I'm not sure how to create one, but it is following this diagram with an extra LED and button. I can try to place the LEDs and buttons in a different layout if that would be beneficial though.
Also, in the code, is it possible to switch "pull_up" to "pull_down"?
Yes it was flashing with the other code posted. Each button seemed to be connected to the first LED (pin11). If any button was pressed, it would temporarily stop the LED from flashing but would come back.
The code you just sent stopped the flashing though. The first LED is off, but if any button is pressed it turns on. The other LEDs aren't working yet.