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