Go Down

Topic: 2 LED 2 Buttons (Read 536 times) previous topic - next topic

gfvesley

I am trying to make a simple circuit with an Arduino UNO, 2 LEDs and 2 buttons.  I want to turn on circuit and have green LED on, push red button and have green LED go off and red LED come on, push red button and have red LED go off and green button come on.

green button ground and Pin 3
red button ground and Pin 2
green LED Pin 13 and ground
red LED ground and +resistor Pin 11

Both LEDs on
push green button both LED off.  push again nothing.
both LEDs on, push red button,both LEDs blink,  several pushes both go off,
both LEDs off, push red button, LEDs blink,  several pushes both LEDs come on.

Cans anyone help?

Thanks

Code: [Select]


 //2 led 2 button
int redbuttonPIN = 2;    //red button
int greenbuttonPIN = 3;  //green button
int redledPIN = 11;      //red led
int greenledPIN = 13;    //green led
int state = HIGH;        //current state of output pin
int reading;             //current reading from the input pin
int reading2;         
int previous = LOW;      //previous reading from the input pin

// following variables are long because time in milli sec
long time = 0;              //last time the output pin was toggled
long debounce = 200;   // debounce time increases if output flickers

void setup() {
  pinMode(redbuttonPIN, INPUT);     //red button is an input
  pinMode(redledPIN, OUTPUT);       //red led is output
  pinMode(greenbuttonPIN, INPUT);   //green button in input
  pinMode(greenledPIN, OUTPUT);     //green led is output

  pinMode(redbuttonPIN, INPUT);
  digitalWrite(redbuttonPIN, HIGH);   //enable pull resistor

  pinMode(greenbuttonPIN, INPUT);
  digitalWrite(greenbuttonPIN, HIGH);  //enable pull resistor
}

void loop() {
  reading = digitalRead(redbuttonPIN);  //reading red button
  reading2 = digitalRead(greenbuttonPIN);  //reading green button

  //if the input went from low to high
  //ignore noise from circuit, toggle the output pin and remember
  //time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state = HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();
  }

  digitalWrite(redledPIN, state);  //chaging redled pin to on or off

  previous = reading;
  if (reading2 == HIGH && previous == LOW && millis() - time > debounce){
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

   time = millis();
  }
  digitalWrite(greenledPIN, state);  //changing ledg pin state to on or off

  previous = reading2;
}
 
 





LesserMole

Before looking at the code, let's get this sorted out:

green LED Pin 13 and ground
If by that you mean an external LED over and above the built-in one, it also needs a resistor, just like your red one on pin 11.






gfvesley

Doesn't pin 13 have a 1K resistor built in?  The green LED in on and has not burned out.

LesserMole

Doesn't pin 13 have a 1K resistor built in? 
If you look top-centre of the Uno R3 schematic, you will see there's a resistor in series with the built-in LED but that won't help an external LED.


gfvesley

www.varesano.net/blog/fabio/serial-communication-arduino-and-processing-simple-examples and-arduino-based-gamepad-int  page 5/17 "...pin 13 has a 1k resistor connected in serries, so it is safe to directly connect the LED to it."  among other references.  The addition of a 1k resistor to the +side of LED and pin 13 reduces the intensity of the LED, but does not affect the operation of the circuit.

LesserMole

#5
Feb 18, 2018, 05:12 am Last Edit: Feb 18, 2018, 09:10 am by LesserMole
For reasons best known to himself, one Paul__RB oops sorry I mean Paul__B has taken this into a PM chain, so in the interests of clarity here's his main point:

Quote
No board since the Arduino Diecimila has had a resistor in series with pin 13.  Earlier boards did, and in fact this was a design fault that rendered the pin unusable
I take that to mean I'm right in what I said about the Uno R3, where the resistor protects only the built-in led. Here's a snip from the Uno R3 schematic.





PaulRB

For reasons best known to himself, one Paul__RB has taken this into a PM chain
Huh???

LesserMole

Ooops typo there, Paul__B not Paul__RB, but I didn't say PaulRB either....




PaulRB

Oh, ok. Is there a Paul__RB on the forum, out of curiosity?

wilykat

I remember having issues with pin 13 on my old Uno R2 because of the inline resistor and LED. Some functions attached to pin 13 didn't behave right.  I did eventually mod my R2 so the D13 LED would match R3 schematic by lifting unused op amp pins, connecting op amp input to D13, cutting trace from D13 to LED resistor, connecting LED resistor to op amp output, and making sure the D13 pin header is directly connected to the chip. Everything I did afterward behaved correctly.

Go Up