Go Down

Topic: SPST Switch Help. Thank you! Arduino UNO (Read 1 time) previous topic - next topic

hrhtml99

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!
I enjoy electrical engineering and robotics engineering. I have a Arduino Mega 2560 based rover with ultrasonic, infrared and gyroscopic cap

HazardsMind

Look at the solution I gave this guy, for he too had the same issue.
post
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

hrhtml99

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.

Code: [Select]

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;
}
I enjoy electrical engineering and robotics engineering. I have a Arduino Mega 2560 based rover with ultrasonic, infrared and gyroscopic cap

JohnHoward

From: http://arduino.cc/en/Reference/DigitalRead

Quote
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?

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
I enjoy electrical engineering and robotics engineering. I have a Arduino Mega 2560 based rover with ultrasonic, infrared and gyroscopic cap

HazardsMind

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.
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Jack Christensen

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:

Code: [Select]
pinMode(8, INPUT_PULLUP);

Debouncing is also mandatory.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Nick Gammon


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


http://www.gammon.com.au/switches

Nick Gammon


Thanks HazardsMinds, that works, but it still prints "on" about 10 times...


It's bouncing. Ten bounces are pretty typical.

Nick Gammon


TanHadron

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.

Nick Gammon

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).

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.
I enjoy electrical engineering and robotics engineering. I have a Arduino Mega 2560 based rover with ultrasonic, infrared and gyroscopic cap

hrhtml99



http://www.gammon.com.au/switches



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?
I enjoy electrical engineering and robotics engineering. I have a Arduino Mega 2560 based rover with ultrasonic, infrared and gyroscopic cap

JohnHoward


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)

Go Up