Trying to write a library for a 3-state switch

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)

It doesn't even compile for me:

C:\Users\per\Desktop\test4\test4.ino: In function 'void loop()':

test4:21: error: 'class Three_way' has no member named 'Position'

   Serial.println(myLEDswitch.Position());

                              ^

test4:22: error: 'class Three_way' has no member named 'Position'

   Serial.println(myLEDswitch.Position());

                              ^

test4:23: error: 'class Three_way' has no member named 'Position'

   Serial.println(myLEDswitch.Position());

                              ^

test4:25: error: 'class Three_way' has no member named '_stateA'

 if (myLEDswitch._stateA) {

                 ^

test4:26: error: 'class Three_way' has no member named 'Position'

     int a=myLEDswitch.Position();

                       ^

C:\Users\per\Desktop\test4\test4.ino:26:9: warning: unused variable 'a' [-Wunused-variable]

     int a=myLEDswitch.Position();

         ^

test4:35: error: 'class Three_way' has no member named '_stateB'

   } else if (myLEDswitch._stateB) {

                          ^

test4:36: error: 'class Three_way' has no member named 'Position'

     int b=myLEDswitch.Position();

                       ^

C:\Users\per\Desktop\test4\test4.ino:36:9: warning: unused variable 'b' [-Wunused-variable]

     int b=myLEDswitch.Position();

         ^

test4:45: error: 'class Three_way' has no member named '_stateC'

   } else if (myLEDswitch._stateC) {

                          ^

test4:46: error: 'class Three_way' has no member named 'Position'

     int c=myLEDswitch.Position();

                       ^

C:\Users\per\Desktop\test4\test4.ino:46:9: warning: unused variable 'c' [-Wunused-variable]

     int c=myLEDswitch.Position();

         ^

Using library Three_way in folder: E:\Stuff\misc\electronics\arduino\libraries\Three_way (legacy)
exit status 1
'class Three_way' has no member named 'Position'

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

Sorry wrong test file. The correct one is attached. Thanks.

dcparduino

test3.ino (1020 Bytes)