3

I'm working on modifying this example from the Arduino website to read the state of 3 toggle (SPDT) switches. If I have one toggle switch hooked up, my sketch works as expected...the sketch will log the state of the switch in the serial debugger every-time the state changes, and only when the state changes.

However, if I add in two more switches, the serial debugger will continuously output my log messages, without detecting if the state has changed.

Here's the code from my sketch:

const int switchOnePin = 2; // digital in 2 (pin the switch one is attached to)
const int switchTwoPin = 3; // digital in 3 (pin the switch two is attached to)
const int switchThreePin = 4; // digital in 4 (pin the switch three is attached to)
int switchOneState = 0; // current state of the switch
int lastSwitchOneState = 0; // previous state of the switch
int switchTwoState = 0; 
int lastSwitchTwoState = 0; 
int switchThreeState = 0; 
int lastSwitchThreeState = 0; 
void setup() {
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
 int switchOneState = 0; // current state of the switch
 int lastSwitchOneState = 0; // previous state of the switch switch pins as an input
 pinMode(switchOnePin, INPUT);
 pinMode(switchTwoPin, INPUT);
 pinMode(switchThreePin, INPUT);
}
void loop() {
 // read the switch input pins:
 switchOneState = digitalRead(switchOnePin);
 delay(50);
 switchTwoState = digitalRead(switchTwoPin);
 delay(50);
 switchThreeState = digitalRead(switchThreePin);
 // compare the switchState to its previous state
 if (switchOneState != lastSwitchOneState) {
 // if the state has changed, increment the counter
 if (switchOneState == HIGH) {
 // if the current state is HIGH then the button
 // went from off to on:
 Serial.println("Switch one is on");
 }
 else {
 // if the current state is LOW then the button
 // went from on to off:
 Serial.println("Switch one is off");
 }
 if (switchTwoState != lastSwitchTwoState) {
 if (switchTwoState == HIGH) {
 Serial.println("Switch two is on");
 }
 else {
 Serial.println("Switch two is off");
 }
 if (switchThreeState != lastSwitchThreeState) {
 if (switchThreeState == HIGH) {
 Serial.println("Switch three is on");
 }
 else {
 Serial.println("Switch thre is off");
 }
 }
}
 // Delay a little bit to avoid bouncing
 delay(50); 
}
// save the current state as the last state,
//for next time through the loop
lastSwitchOneState = switchOnePin;
lastSwitchTwoState = switchTwoPin;
lastSwitchThreeState = switchThreePin;
}

And here's what my circuit looks like:

enter image description here Does anyone have any insight into what I may be doing wrong? I'm not quite sure if it's a circuit or a sketch issue (or both).

EDIT I tried adding a delay of 50ms between each digital input read, but that still did not seem to help the problem.

asked Aug 9, 2015 at 16:41
8
  • Not sure this is the entirety of the problem, or if it's just a "transcription" error when you made your circuit diagram, but you've got both sides of your switches connected to +5V. One side should be connected to ground, and the other to +5V. Commented Aug 9, 2015 at 17:55
  • @AndrewMadsen, yes, that is just a transcription error...thank you for catching that! Commented Aug 9, 2015 at 18:03
  • A bit off topic: why aren't you using the internal pullup resistors? You could simply connect the input to GND (for logical zero) or leave them open (for logical one, courtesy of the pullup resistor). Commented Aug 9, 2015 at 18:37
  • @IgorStoppa, I just wasn't aware they could work that way to be honest (still a beginner with Arduino) Commented Aug 9, 2015 at 18:40
  • 2
    I think your {} bracketing got a bit messed up, do you intend to only check the state of switch two if switch one changed? Commented Aug 9, 2015 at 19:22

2 Answers 2

5

The problem was two pronged---both the copy/paste error that @Gerben pointed out, as well as the incorrect if-else statements that @BrettAM pointed out. Below is the working code:

const int switchOnePin = 2; // digital in 2 (pin the switch one is attached to)
const int switchTwoPin = 3; // digital in 3 (pin the switch two is attached to)
const int switchThreePin = 4; // digital in 4 (pin the switch three is attached to)
int switchOneState = 0; // current state of the switch
int lastSwitchOneState = 0; // previous state of the switch
int switchTwoState = 0;
int lastSwitchTwoState = 0;
int switchThreeState = 0;
int lastSwitchThreeState = 0;
void setup() {
 //initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
 int switchOneState = 0; // current state of the switch
 int lastSwitchOneState = 0; // previous state of the switch switch pins as an input
 pinMode(switchOnePin, INPUT);
 pinMode(switchTwoPin, INPUT);
 pinMode(switchThreePin, INPUT);
}
void loop() {
 // read the switch input pins:
 switchOneState = digitalRead(switchOnePin);
 switchTwoState = digitalRead(switchTwoPin);
 switchThreeState = digitalRead(switchThreePin);
 // compare the switchState to its previous state
 if (switchOneState != lastSwitchOneState) {
 // if the state has changed, increment the counter
 if (switchOneState == HIGH) {
 // if the current state is HIGH then the button
 // went from off to on:
 Serial.println("Switch one is on");
 } else {
 // if the current state is LOW then the button
 // went from on to off:
 Serial.println("Switch one is off");
 }
 }
 if (switchTwoState != lastSwitchTwoState) {
 if (switchTwoState == HIGH) {
 Serial.println("Switch two is on");
 } else {
 Serial.println("Switch two is off");
 }
 }
 if (switchThreeState != lastSwitchThreeState) {
 if (switchThreeState == HIGH) {
 Serial.println("Switch three is on");
 } else {
 Serial.println("Switch thre is off");
 }
 }
 // Delay a little bit to avoid bouncing
 delay(50);
 // save the current state as the last state,
 //for next time through the loop
 lastSwitchOneState = switchOneState;
 lastSwitchTwoState = switchTwoState;
 lastSwitchThreeState = switchThreeState;
}
answered Aug 9, 2015 at 20:13
4
lastSwitchOneState = switchOnePin;
lastSwitchTwoState = switchTwoPin;
lastSwitchThreeState = switchThreePin;

should be

lastSwitchOneState = switchOneState;
lastSwitchTwoState = switchTwoState;
lastSwitchThreeState = switchThreeState;
answered Aug 9, 2015 at 19:25
1
  • 1
    That's definitely part of the problem; so I did mark it up. Thank you! Still only able to read the result of switch one though. Commented Aug 9, 2015 at 19:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.