Hey, I've got a DPDT switch of the on-off-on type.
I have a wire soldered to each pin on the switch, they are arranged like this:
5v------ o | o -----gnd
dig 11-- o | o --dig 12
gnd----- o | o -----gnd
Running a simple script:
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor
This example code is in the public domain.
*/
void setup() {
Serial.begin(9600);
pinMode(11, INPUT);
pinMode(12, INPUT);
}
void loop() {
int sensorValueTw = digitalRead(12);
int sensorValueTh = digitalRead(13);
Serial.print("PIN 11 = ");
Serial.print(sensorValueTw, DEC);
Serial.print(" | PIN 12 = ");
Serial.println(sensorValueTh, DEC);
}
When the switch is in the middle, pin 11 flucuates between 0 and 1, moving switch to left makes it permanently 1 and moving switch to right has absolutely no effect, pin 12 remains a constant 0.
5v------ o | o -----gnd
dig 11-- o | o --dig 12
gnd----- o | o -----gnd <<< change this gnd to 5v
Add external pull-down resistors to pins 11 and 12 and ground. Otherwise you will have floating input pin conditions when the switch is in it's center off position.
I would suggest that is because you don't have any software debouncing logic in your code. That may or may not be a problem for your specific application, but is solvable if required.
No, no debouncing, I've got 11 digital inputs (9 pushbuttons + 3way switch), might be able to implement it on the 3-way as I don't see it being used as frequently.