Hi,
I'm trying to build a library for 3-state switch. I am using an ON-OFF-ON toggle switch and three LEDs/220-ohm resistors to test my code. It works fine when I use the following script code:
int Way1=2;
int Way2=3;
int LED1=12;
int LED2=13;
int LED3=11;
void setup() {
Serial.begin(9600);
pinMode(Way1,INPUT_PULLUP);
pinMode(Way2,INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
}
void loop() {
//read the pushbutton value into a variable
int stateA = digitalRead(Way1);
int stateB = digitalRead(Way2);
int stateC = LOW;
//print out the value of the pushbutton
//Serial.println(stateA);
// Keep in mind the pullup means the pushbutton's
// logic is inverted. It goes HIGH when it's open,
// and LOW when it's pressed. Turn on pin LED2 when the
// button's pressed, and off when it's not:
if (stateA == LOW) {
digitalWrite(LED1, !stateA);
digitalWrite(LED2, stateA);
digitalWrite(LED3, stateA);
Serial.println("A");
delay(1000);
} else if (stateB == LOW) {
digitalWrite(LED1, stateB);
digitalWrite(LED2, !stateB);
digitalWrite(LED3, stateB);
Serial.println("B");
delay(1000);
} else if (stateA == HIGH && stateB ==HIGH) {
digitalWrite(LED1, stateC);
digitalWrite(LED2, stateC);
digitalWrite(LED3, !stateC);
Serial.println("C");
delay(1000);
}
}
However, when I try to use the attached library and test script, I'm having issues. The LEDs light up erratically for some reason and I'm not sure what is going on? If anyone has any ideas, I'd highly appreciate it. Thanks.
dcparduino
test4.ino (1.15 KB)
Three_way.zip (831 Bytes)