Pages: [1] 2   Go Down
Author Topic: SPST Switch Help. Thank you! Arduino UNO  (Read 1515 times)
0 Members and 1 Guest are viewing this topic.
Vermont
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

I enjoy electrical engineering and robotics engineering. I have a Arduino Mega 2560 based rover with ultrasonic, infrared and gyroscopic cap

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3755
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at the solution I gave this guy, for he too had the same issue.
post
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Vermont
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
}
Logged

I enjoy electrical engineering and robotics engineering. I have a Arduino Mega 2560 based rover with ultrasonic, infrared and gyroscopic cap

Offline Offline
Full Member
***
Karma: 5
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Vermont
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

I enjoy electrical engineering and robotics engineering. I have a Arduino Mega 2560 based rover with ultrasonic, infrared and gyroscopic cap

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3755
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4089
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
pinMode(8, INPUT_PULLUP);

Debouncing is also mandatory.
Logged

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19035
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19035
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

It's bouncing. Ten bounces are pretty typical.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19035
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Logged


Offline Offline
God Member
*****
Karma: 25
Posts: 529
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19035
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Vermont
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I enjoy electrical engineering and robotics engineering. I have a Arduino Mega 2560 based rover with ultrasonic, infrared and gyroscopic cap

Vermont
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

I enjoy electrical engineering and robotics engineering. I have a Arduino Mega 2560 based rover with ultrasonic, infrared and gyroscopic cap

Offline Offline
Full Member
***
Karma: 5
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: