Although this circuit should be easy to implement, I cannot achieved properly. I want to use two pushbutton as toggle which one activates the system (light green LED) and the other pushbutton turns off the system (light red LED). Could you tell me where to find an appropriate approach?
I've followed this website: https://www.instructables.com/id/Arduino-2-Button-2-Led/ with their code. Find attached my circuit. The shorter leg from the LED is inline with the resistor. The problem could be the resistors' values?
Examine the pinout of the switch in the image below.
Move the green wires on terminal 1 of the switches to terminal 4 of the switches (terminal 4 will then have the resistor and the new moved green wire).
Let’s test the LED installation:
The Green wire going to the Arduino and the 1k resistor (for the top LED), move the wire end that is on the Arduino pin from that pin to the Red (+5v) rail on the breadboard.
The LED should then go on.
Red rail (+5v) ——— 1k ——— LED Anode rounded side ——— LED Cathode flat side ——— GND (Blue rail)
If not on, turn LED around to your original setup, LED should then go on.
I've followed this website: https://www.instructables.com/id/Arduino-2-Button-2-Led/ with their code. Find attached my circuit. The shorter leg from the LED is inline with the resistor. The problem could be the resistors' values?
Now you added that after I posted my reply, making my reply look rubbish.
About as rubbish as most instructable articles.
Thank you very much. >:(
Read the rules, it specifically tells you not to do this.
Larryd, I've moved the green wire as you said but nothing. I attach an image and the project with Fritzing. I've follow all the instructions but it does not work.
int ledPinRed = 0; //
int ledPinGreen = 1; //
int buttonPin1 = 2; //
int buttonPin2 = 3; //
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
Then why have you connected the switches to A2 and A3?
These are analogue input pins, to do what your code say you should wire them up to pins 2 & 3 on the other side of the board!
It works Grumpy_Mike! Thank you. However, I expected that when one LEDs is ON (green) the other LED (red) turns OFF, and viceversa (simulating a Start/Stop system).
However, I expected that when one LEDs is ON (green) the other LED (red) turns OFF, and viceversa
So write that in your code.
Every time you turn an LED on, with a digitalWrite HIGH turn the other one off with a digitalWrite LOW on the very next line.
int ledPinRed = 0; //
int ledPinGreen = 1; //
int buttonPin1 = 2; //
int buttonPin2 = 3; //
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
Maybe this will help You. You will be able to do what You want with only one button.
Problem is with mechanical button that is not ideal. Button is creating short unwanted flickering on sticks, and You will notice fast on and off.
Adding delay for button is preventing arduino to see that flickering. I call it "masking button for short time"
This is code what I use in my project.
int pinswich = 2;
int ledpin = 13;
int state = LOW;
int a = LOW;
void setup(){
Serial.begin(9600);
Serial.setTimeout(10);
pinMode(ledpin, OUTPUT);
pinMode(pinswich, INPUT_PULLUP);
digitalWrite(ledpin, LOW);
}
void loop(){
while (digitalRead(pinswich) == HIGH)
{
a = HIGH;
delay(10);
}
if ((a == HIGH) && (state == HIGH))
{
digitalWrite(ledpin, HIGH);
Serial.println("on");
state = LOW;
a = LOW;
}
else if ((a == HIGH) && (state == LOW))
{
digitalWrite(ledpin, LOW);
Serial.println("of");
state = HIGH;
a = LOW;
}
}
Check this code. Using one button to turn on and off whole system.
int pinswich = 2;
int ledpin = 13;
int state = LOW;
int a = LOW;
void setup(){
Serial.begin(9600);
Serial.setTimeout(10);
pinMode(ledpin, OUTPUT);
pinMode(pinswich, INPUT_PULLUP);
digitalWrite(ledpin, LOW);
}
void loop(){
while (digitalRead(pinswich) == HIGH)
{
a = HIGH;
delay(10);
}
if ((a == HIGH) && (state == HIGH))
{
digitalWrite(ledpin, HIGH);
// Using next digital pin for desire external electronics digitalWrite(systempin, HIGH);
// Also You can add next digitalWrite to turn off red led in Your circuit.
Serial.println("system turned on");
state = LOW;
a = LOW;
}
else if ((a == HIGH) && (state == LOW))
{
digitalWrite(ledpin, LOW);
// And turn off green led and turn on red led. Its so simple.
Serial.println("turn system of");
state = HIGH;
a = LOW;
}
}