Multiple Toggle-Switch State Change Detection

G'day, brand new to the forum here, i'm sure someone else has asked for help on this topic, but i cant find a search function for the forums.

Basically i have 2 toggle switches and want the serial monitor to show when switch 1 and 2 is turned on/off. I have used code similar to the State Change Detection in Digital examples, but still having some problems.

If anyone can help, it would be much appreciated!

DDD

Code:

/* 3 input toggle switches with LED status indicator

Testing: As this is in bugshooting stage, a simpler problem of 2 toggle switches is being used.

Problem: Serial monitor shows only switch 1 turning on and off

*/ const int switch1 = 6; const int switch2 = 5; const int switch3 = 4;

//We need 3 sets of status and last status int button1State = 0; int lastButton1State = 0; int button2State = 0; int lastButton2State = 0; int button3State = 0; int lastButton3State = 0;

void setup() { pinMode(switch1, INPUT); pinMode(switch2, INPUT); pinMode(switch3, INPUT); Serial.begin(9600); }

void loop(){ button1State = digitalRead(switch1); button2State = digitalRead(switch2);

//If switch changed position if (button1State != lastButton1State) { //If switch WAS low, and got turned high if (button1State == HIGH) { Serial.println("1 is on"); } //Switch was high and was turned low else { Serial.println("1 is off"); } } //Same deal as for switch 1 if (button2State != lastButton2State) { if (button2State == HIGH) { Serial.println("2 is on"); } else{ Serial.println("2 is off"); } } lastButton1State = button1State; lastButton2State = button2State; delay(500); }

Hello,

your program works as designed. The main source of potential problems I see is the way you wired your switches? Did you use either an external pull-up or an external pull-down resistor? If not, the concerned pin will be floating when your switch is open and deliver random values.

If you just want to use a switch with resistor, you can activate the internal pull-up resistor and have your switch close the connection to ground. You save yourself the hassles of an external resistor but the logic will be reversed, meaning HIGH = switch open and LOW = switch closed.

Korman

@DDD, The search function is in the right upper corner

Not quite sure what you mean by external pull up/pull down resistor. I use a resistor to limit current.

5Vdc------------------ Sorry for the crude circuit diagram. Not | | sure how to upload the photo. switch switch | | 6s 5s | | LED LED | | -ve--------------------

Thanks,

DDD

Replace the LED with a 1kOhm resistor and try again. Then it should work reliably - at least it did for me.

Korman

Ok, so now when one switch is closed, the status of the other switch is unstable...it outputs highs and lows.

DDD

so now when one switch is closed, the status of the other switch is unstable.

That’s because you haven’t wired it up right. It sounds like your inputs are floating, that is not connected to anything. An input always has to be connected to a 5V source or ground either directly or through a resistor.