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.
2 Answers 2
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;
}
lastSwitchOneState = switchOnePin;
lastSwitchTwoState = switchTwoPin;
lastSwitchThreeState = switchThreePin;
should be
lastSwitchOneState = switchOneState;
lastSwitchTwoState = switchTwoState;
lastSwitchThreeState = switchThreeState;
-
1That'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.narner– narner2015年08月09日 19:57:55 +00:00Commented Aug 9, 2015 at 19:57
{}
bracketing got a bit messed up, do you intend to only check the state of switch two if switch one changed?