Components and supplies
Resistor 1k ohm
Arduino UNO
LED (generic)
Jumper wires (generic)
Tactile Switch, Top Actuated
Breadboard (generic)
Apps and platforms
Arduino IDE
Project description
Code
Two LEDs and Two Push Buttons with opposite effect
arduino
Two LEDs and Two Push Buttons with opposite effect
Downloadable files
Breadboard Diagram
Make connections as shown in the figure.
Breadboard Diagram
Comments
Only logged in users can leave comments
nori77
2 years ago
so it will work if you do that's const int BUTTON1 = 2; const int BUTTON2 = 4; const int LED1 = 8; const int LED2 = 12; int BUTTONstate1 = 0; int BUTTONstate2 = 0; void setup() { pinMode(BUTTON1, INPUT); pinMode(BUTTON2, INPUT); pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); } void loop() { BUTTONstate1 = digitalRead(BUTTON1); if (BUTTONstate1 == HIGH) { digitalWrite(LED1, HIGH); } else{ digitalWrite(LED1, LOW); } BUTTONstate2 = digitalRead(BUTTON2); if (BUTTONstate2 == LOW) { digitalWrite(LED2, LOW); } else{ digitalWrite(LED2, HIGH); } }
Anonymous user
2 years ago
I tried on proteus but only 1 push button can work at a time..How to fix that?
projjalsahoo2017
2 years ago
the code is little bit incorrect, the correct code will be : const int BUTTON1 = 2; const int BUTTON2 = 4; const int LED1 = 8; const int LED2 = 12; int BUTTONstate1 = 0; int BUTTONstate2 = 0; void setup() { pinMode(BUTTON1, INPUT); pinMode(BUTTON2, INPUT); pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); } void loop() { BUTTONstate1 = digitalRead(BUTTON1); if (BUTTONstate1 == HIGH) { digitalWrite(LED1, HIGH); } else{ digitalWrite(LED1, LOW); } BUTTONstate2 = digitalRead(BUTTON2); if (BUTTONstate2 == HIGH) { digitalWrite(LED2, HIGH); } else{ digitalWrite(LED2, LOW); } }
Anonymous user
2 years ago
This is a great project. Used if for my kids toys and they love it. But i was wondering if you could help me change it so both leds start in off and then on when button is pushed and off when pressed again with each button controlling each led. I'm new and trying to figure this out. Thanks
Anonymous user
5 years ago
You just switch the high and low states of the leds. So that the led is getting voltage when the button is not pressed and does not when the button is pressed.
Anonymous user
2 years ago
so it will work if you do that's const int BUTTON1 = 2; const int BUTTON2 = 4; const int LED1 = 8; const int LED2 = 12; int BUTTONstate1 = 0; int BUTTONstate2 = 0; void setup() { pinMode(BUTTON1, INPUT); pinMode(BUTTON2, INPUT); pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); } void loop() { BUTTONstate1 = digitalRead(BUTTON1); if (BUTTONstate1 == HIGH) { digitalWrite(LED1, HIGH); } else{ digitalWrite(LED1, LOW); } BUTTONstate2 = digitalRead(BUTTON2); if (BUTTONstate2 == LOW) { digitalWrite(LED2, LOW); } else{ digitalWrite(LED2, HIGH); } }
Anonymous user
2 years ago
the code is little bit incorrect, the correct code will be : const int BUTTON1 = 2; const int BUTTON2 = 4; const int LED1 = 8; const int LED2 = 12; int BUTTONstate1 = 0; int BUTTONstate2 = 0; void setup() { pinMode(BUTTON1, INPUT); pinMode(BUTTON2, INPUT); pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); } void loop() { BUTTONstate1 = digitalRead(BUTTON1); if (BUTTONstate1 == HIGH) { digitalWrite(LED1, HIGH); } else{ digitalWrite(LED1, LOW); } BUTTONstate2 = digitalRead(BUTTON2); if (BUTTONstate2 == HIGH) { digitalWrite(LED2, HIGH); } else{ digitalWrite(LED2, LOW); } }
faris03irs
3 years ago
//what about three led and two tactila switches const int BUTTON1 = 2; const int BUTTON2 = 4; const int LED1 = 7; const int LED2 = 8; const int LED3 = 12; int BUTTONstate1 = 0; int BUTTONstate2 = 0; void setup() { pinMode(BUTTON1, INPUT); pinMode(BUTTON2, INPUT); pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); } void loop() { BUTTONstate1 = digitalRead(BUTTON1); if (BUTTONstate1 == HIGH) { digitalWrite(LED1, HIGH); } else{ digitalWrite(LED1, LOW); } BUTTONstate2 = digitalRead(BUTTON2); if (BUTTONstate2 == HIGH) { digitalWrite(LED2, HIGH); } else{ digitalWrite(LED2, LOW); } BUTTONstate1 = digitalRead(BUTTON1); if (BUTTONstate1 == HIGH); { digitalWrite(LED3, HIGH); } else{ digitalWrite(LED3, LOW); } }
Anonymous user
3 years ago
This so bad i really do not like not very nice very nice(borat accent). I put in computer and arduiny go bang bang.
Anonymous user
3 years ago
When the room is cleaned, the button will be pressed for 5 seconds and I am trying to print the last date the button was pressed on the LCD screen. I couldn't write any code. Can you help with this issue?
Anonymous user
4 years ago
Hello. Thank you for this simple and helpful project. I had one question. I am working on a project similar to this one. I want to connect 2 active low push buttons and 2 LEDs (green and red). When PB1 is pressed the led should flash at 40Hz and when pressed again it should turn off. Same for the red but at 100Hz. When I wrote the code I have to keep my figer on the PB for it to the led to flash. May I get some help please?
Anonymous user
4 years ago
idk mine isnt working for some reason
maxim10373
4 years ago
Only One Switch can work at a time.how to change the code ?
oldandlearning
5 years ago
Hello great project I like the simplicity of it I’m new to Arduino and having trouble with adding Debounce to a four button sketch (4 buttons 4 LEDs) Can you add Debounce to this sketch and how would I do it Thank you for sharing your projects and thank you in advance
BP420A
2 years ago
This is a great project. Used if for my kids toys and they love it. But i was wondering if you could help me change it so both leds start in off and then on when button is pushed and off when pressed again with each button controlling each led. I'm new and trying to figure this out. Thanks