Hi, i'm new to electronics and a project of mines requires me to use a single SPDT 3 position switch in such a way that i get an output signal that is high, medium, or low. Can someone guide me as to how the circuit and arduino code should be to implement.
high medium low for what?
If you have a 3 position spdt switch then you have a, spdt center off switch.
Hi,
Welcome to the forum.
Can you post a picture or link to spec/data/seller of your switch please.
Does your SPDT have a center position, and how many terminals.
Tom...
The SPDT switch has is off at center position and has 3 terminals. I want to use this 3 position switch as a means of input, particularly as an AUX switch for a DIY transmitter which allows me to switch between 3 modes
Center contact to ground, and both outer contacts of the switch to two digital pins.
Enable the internal pull up resistors for those pins in void setup().
If both pins read HIGH (or if no pins read LOW), then the switch must be in the center position.
if(pinOne == LOW)....//one outer position
else if(pinTwo == LOW)...//other outer position
else...//middle position
Leo..
ok i'll give that a try... Thanks alot :)... also is it possible to achieve this using only one digital or analog pin
Wawa:
Center contact to ground, and both outer contacts of the switch to two digital pins.
Enable the internal pull up resistors for those pins in void setup().If both pins read HIGH (or if no pins read LOW), then the switch must be in the center position.
if(pinOne == LOW)....//one outer position
else if(pinTwo == LOW)...//other outer position
else...//middle position
Leo..
Voltage divider with e.g. two 10k resistors between 5volt and ground.
Center tap to an analogue pin.
Center pin of the switch also to the analogue pin.
Other pins of the switch to +5volt and ground.
Pos1 will read 0
Pos2 will read ~512
Pos3 will read 1023
Leo..
Note that the solution proposed by Wawa (reply#4) is very simple to debounce (if needed). Simple debouncing is a benefit to using two digital pins.
if ((pinOne == LOW) && (pinTwo == HIGH))
{
// one outer position
}
else if ((pinTwo == LOW) && (pinOne == HIGH))
{
// other outer position
}
else
{
// middle position
}
One digital pin? Hmmm...
Position 1: switch connects Arduino digital input to Vcc
Position 3: switch connects Arduino digital input to ground
Position 2: switch connects Arduino digital input to ground via 100K
#define MIDDLE 2
...
pinMode(n, INPUT);
int v = digitalRead(n);
if (v == LOW) {
pinMode(n, INPUT_PULLUP);
if (digitalRead(n) == HIGH) v = MIDDLE;
}
Note that the solution proposed by Wawa (reply#4) is very simple to debounce (if needed). Simple debouncing is a benefit to using two digital pins.
@DLoyd,
I thought the very definition of "debounce" is to eliminate switch bounce which is a function of TIME (typically XX mS). The debounce code you posted is not time dependent since we can dismiss the code execution time as negligible and thus the code makes one pass checking the given conditions and that's it. Hardware debouncing on the other hand, (pullup resistor and bypass cap) is time dependent where time "t" = R*C.
Can you help me understand how that code can perform debouncing of a switch bounce that has a real and measurable (with a DOS) width in mS yet the code is NOT waiting for the bounce to subside. I am a hardware person so I find the code puzzling. Perhaps you could enlighten me.
if ((pinOne == LOW) && (pinTwo == HIGH))
{
// one outer position
}
else if ((pinTwo == LOW) && (pinOne == HIGH))
{
// other outer position
}
else
{
// middle position
}
@raschemmel,
Yes, normally debounce uses an ignore time interval. However, the unique thing about a SPDT switch (or Form-C relay contacts (NO/COM/NC terminals) is that the action is "break before make". Thus, when reading both terminals, there can never be the condition where both are shorted. When wired as per reply#4, there will never be the case where both inputs go LOW at the same time, therefore the logic (when looking at both inputs) is inherently bounce free.
If the user overdoses on caffeine and is able to operate the switch through the 3 positions at 10 times per second, then you could consider the debounce time to be 100ms. Actually in this case, the middle condition would be true for 100ms.
Got it.
I went to give you karma and noticed you were only 1 more than my karma. Now you are +2 ;D
(and it took me 4 times as many posts to get there ;D ) Must be my winning personality.. ;D
+1 to both, pre-election giveaway.
.
@ LarryD,
I owe you a Bloody Mary.
Uh-oh ... might have to hit my invisible -1
What I had mentioned (and code) would work well for a standard SPDT switch and relay contacts, but the OP's switch can rest in the middle position. Yeah, there would be bounce from any end position and the middle, but not from end to end.
+1 to rashemmel for being correct. Also to Larry.
I didn't want to say anything but after you answered answered my question I was wondering how a mechanical switch could not have contact bounce with a moving part.