I have a project that is using four SPST switches, I have a code running where when one is flipped to the on position it prints "on" to the Serial port, but as of now it outputs on continuously until it is turned off. What I need it to do is print "on" once when it is toggled, and "on" again when it goes off. I can't use interrupts because I have four switches that need to do this, and only two interrupt ports. How can I do this? I am not too experienced with Arduino programming but I am not a beginner either. Thanks in advance for your help!
Look at the solution I gave this guy, for he too had the same issue.
post
Thanks HazardsMinds, that works, but it still prints "on" about 10 times, while this is better than an infinite amount of times, it still is not good enough. Here is my code, I am using one switch for this test, but eventually I will use all four.
int old;
void setup(){
Serial.begin(9600);
}
void loop(){
boolean button;
button = digitalRead(8);
if(button == HIGH && button! = old){
Serial.println("Switch Toggled");
}
old = button;
}
From: http://arduino.cc/en/Reference/DigitalRead
If the pin isn't connected to anything, digitalRead() can return either HIGH or LOW (and this can change randomly).
So you probably need a pull up or pull down resistor to avoid the indeterminate state?
Well pin #8 is connected to a toggle switch (on/off) and it is wired correctly I believe, I don't think floating pins is the problem
Well if your certain its not a floating issue, then it could be a bouncing issue. The switch may need to be debounced then you check the states.
A pullup (or pulldown) resistor is definitely needed with an SPST switch. Otherwise, when the switch is open, the pin will be floating. This is a perfect application for the AVR internal pullup resistor, which can be turned on as follows:
pinMode(8, INPUT_PULLUP);
Debouncing is also mandatory.
hrhtml99:
Well pin #8 is connected to a toggle switch (on/off) and it is wired correctly I believe, I don't think floating pins is the problem
hrhtml99:
Thanks HazardsMinds, that works, but it still prints "on" about 10 times...
It's bouncing. Ten bounces are pretty typical.
I vote for the floating hypothesis. Bouncing 10 times with a Serial.print() isn't likely in my experience.
Connect the switch between pin 8 and the ground, and enable the internal pull-up.
The prints are buffered, so the prints themselves won't take long. Admittedly 10 such prints are pushing it a bit (the buffer would fill up).
Thank you Nick, Jack, John, Tan and Hazards, from what I gather the problem is either a bouncing issue or a floating pin issue. My current setup has the switch wired to 5v, pin 8 and ground via a 10k ohm resistor. Do you recommend I remove that and use a pull up resistor like Jack suggested? I will have a chance to work on this later today, so I want to be prepared and know what the best fix will be. As of now, I will include a debouncer and a pull up resistor on pin 8. Thank you all for your help and input.
I read that a hardware debounce can be used by attaching a ~ 1uf capacitor along with the pull up resistor. Is there any reason to use a software debounce over a hardware one?
hrhtml99:
Thank you Nick, Jack, John, Tan and Hazards, from what I gather the problem is either a bouncing issue or a floating pin issue. My current setup has the switch wired to 5v, pin 8 and ground via a 10k ohm resistor. Do you recommend I remove that and use a pull up resistor like Jack suggested? I will have a chance to work on this later today, so I want to be prepared and know what the best fix will be. As of now, I will include a debouncer and a pull up resistor on pin 8. Thank you all for your help and input.
Your software is essentially doing a debounce. (button != old)
hrhtml99:
Thank you Nick, Jack, John, Tan and Hazards, from what I gather the problem is either a bouncing issue or a floating pin issue.
Both need to be addressed.
My current setup has the switch wired to 5v, pin 8 and ground via a 10k ohm resistor. Do you recommend I remove that and use a pull up resistor like Jack suggested?
10K is fine for a pullup but it's hard to visualize the circuit from your description. I use the internal pullups when possible just to reduce the number of external components. See Nick's page for the proper wiring. If the internal pullup is used, then the wiring is simple: One side of the switch goes to the pin on the Arduino and the other side goes to ground.