Go Down

Topic: ON/OFF system using 2 pushbuttons 2 LEDs (Read 307 times) previous topic - next topic

REM14

Nov 09, 2018, 04:17 pm Last Edit: Nov 09, 2018, 04:56 pm by REM14
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


larryd

#2
Nov 09, 2018, 05:34 pm Last Edit: Nov 09, 2018, 05:35 pm by larryd


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



No technical PMs.
The last thing you did is where you should start looking.

larryd

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


No technical PMs.
The last thing you did is where you should start looking.

REM14

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?

larryd

#5
Nov 09, 2018, 06:00 pm Last Edit: Nov 09, 2018, 06:09 pm by larryd
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.


No technical PMs.
The last thing you did is where you should start looking.

larryd

#6
Nov 09, 2018, 06:12 pm Last Edit: Nov 09, 2018, 07:03 pm by larryd
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.


.
No technical PMs.
The last thing you did is where you should start looking.

Grumpy_Mike

#7
Nov 09, 2018, 07:06 pm Last Edit: Nov 09, 2018, 07:07 pm by Grumpy_Mike
Quote
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.

REM14

#8
Today at 08:52 am Last Edit: Today at 08:54 am by REM14
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.

Rintin

Could you please attach your code.

REM14

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

Grumpy_Mike

Code: [Select]
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!

REM14

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

Grumpy_Mike

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

REM14

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

Go Up