Project: I am trying to create a response to a manual 3-way switch movement. This means, when the nob is on the top pol, i get some signal and when the nob is on the bottom pol, i receive a different signal, while no signal (or maybe another one) when the nob is in the middle position.
Question: How do i connect and use a 3 way switch?
My Idea: (See attachement for connections) and i programmed my arduino Uno with this
int pinA = 2;
int pinB = 4;
void setup(){
Serial.begin(9600);
pinMode(pinA, INPUT);
pinMode(pinB, INPUT);
}
void loop(){
if (digitalRead(pinA) == HIGH){
Serial.println("pinA is connected");
}
elseif (digitalRead(pinB) == HIGH){
Serial.println("PinB is connected");
}
else{
Serial.println("No pin is connected");
}
}
Issue: I however don't get any response from this code.
I'm not sure if you are understanding the function of that switch in your schematic. It is actually a 2-way switch. It will connect the center pin to either the right or the left pin. The center pin can be called the "common" pin because it is common to both switch settings. (It is also, more properly called the pole due to the way switch contacts are named, but that is a different topic.) You do have it wired properly though.
In your pinMode statements you only declare the pin as an input. That allows the input pin to float around giving unreliable results. The input pin needs to be "pulled" to the opposite power rail that the common pin is set to. In this case, because you have the common pin tied to ground you want the input pins pulled up to the +V pin. This pulling is commonly done with resistors. In the case of the AVR chip on the UNO (you are using an UNO as your diagram suggests, no?) there are internal pull up resistors. There are a couple ways of activating them, but the modern way is by using the INPUT_PULLUP keyword. Thus your two lines in setup() that read:
(The AVRs used on the UNO and similar Arduino boards only have internal pull up resistors. But there are some processors in the Arduino compatible (meaning supported by the Arduino IDE) that also have pull down resistors, activated with INPUT_PULLDOWN. I don't remember off the top of my head which ones though.)