Hi, i have stumbled upon the following problem today in my programning.
I needed a 2 buttons to act as a lever(switch) and make leds blink, and found this code: http://www.arduino.cc/en/Tutorial/Switch, comparing the previous and current button states to turn and led on.
Then for the second button i just copied the code and just changed the variables as so:
int inPin = 2;
int inPin2 = 3;
int outPin2 = 12;
int outPin = 13;
int state = HIGH;
int state2 = HIGH;
int reading;
int reading2;
int previous = LOW;
int previous2 = LOW;
long time = 0;
long time2 = 0;
long debounce2 = 200;
long debounce = 200;
void setup()
{
pinMode(inPin, INPUT);
pinMode(inPin2, INPUT);
pinMode(outPin, OUTPUT);
pinMode(outPin2, OUTPUT);
}
void loop()
{
reading = digitalRead(inPin);
reading = digitalRead(inPin2);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce2) {
if (state2 == HIGH)
state = LOW;
else
state = HIGH;
time = millis()
}
digitalWrite(outPin, state);
delay(450);
digitalWrite(outPin, LOW);
digitalWrite(outPin2, state2);
delay(450);
digitalWrite(outPin2, LOW);
delay(450);
previous = reading;
previous2 = reading2;
In turn the first initial button and led still blinked and stopped when the button was pressed, yet the second button didnt work as a switch and behaved like a normal button, corresponding led only blinking when the button was pressed. Can anyone help me either develop this code to work or explain to me what I am doing wrong. Thanks in advance