ON/OFF system using 2 pushbuttons 2 LEDs

Hi,

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?

Thanks

https://create.arduino.cc/projecthub/diegohco/latch-or-self-power-off-arduino-101441?f=1

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).

Hard to tell from the image, your LEDs may be backwards.

I've moved it as you said but it still doesn't work. Also, I've rotated the LEDs. Any example I can use instead?

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.

Edit: Added explanation.

Assume you are using pin D8 and D9 for the switches and D10 and D11 for LEDs.

If not, show us ‘your’ current sketch.
Use code tags.

.

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.

2button2LEDs.zip (306 KB)

Could you please attach your code.

My code:

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.

pinMode(ledPinGreen, OUTPUT);
pinMode(ledPinRed, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}

// the loop function runs over and over again forever
void loop() {

buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == HIGH)
{
digitalWrite(ledPinRed, HIGH);

}

buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == HIGH)
{
digitalWrite(ledPinGreen, HIGH);

}

delay(75);
}

int buttonPin1 = 2;    //
int buttonPin2 = 3;    //

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.

Done!

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.

pinMode(ledPinGreen, OUTPUT);
pinMode(ledPinRed, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}

// the loop function runs over and over again forever
void loop() {

buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == HIGH)
{
if(digitalRead(ledPinGreen)== HIGH)
{
digitalWrite(ledPinRed, HIGH);
digitalWrite(ledPinGreen, LOW);
}
else
{
digitalWrite(ledPinRed, HIGH);
}
}

buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == HIGH)
{
if(digitalRead(ledPinRed)== HIGH)
{
digitalWrite(ledPinRed, LOW);
digitalWrite(ledPinGreen, HIGH);
}
else
{
digitalWrite(ledPinGreen, HIGH);
}
}

delay(75);
}

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;
  }

}

I hope It will give You an Idea in Youre code.

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;
}

}