0

I’m pretty new to arduino. I need help concerning a project I’m making. I’m using a SPDT slide switch to switch between what code(method) should be executed.i.e when I shift the switch to the left, XXX method should be executed and when I move the switch to the right, YYYY method should be executed. The center pin of the switch is connected to arduino +5v while the left and right leg of the switch are connected like how a tactile switch is interfaces with the arduino. Please can anyone help me with the code, or at least how to program the arduino? Thank youHere is the circuit connection

0

Your switch wiring for D12 is wrong. You connect your inputs directly to ground and that can never change, regardless of the position of the switch.

You effectively have:

schematic

simulate this circuit – Schematic created using CircuitLab

However you are over complicating things. A SPDT switch is nothing more than two buttons, only one of which can be pressed at one time. If one is pressed the other must be released.

You can completely ignore half the switch and treat the other half as a button: if the "button" is pressed then the switch is in one position. If the "button" is released then it's in the other position.

Unless it's a "center off" switch, in which case it has 3 positions, not 2, and the middle one has nothing connected.

If it is a simple 2-position SPDT switch then just disconnect the D12 wire and remove the right-and resistor in your drawing. Then read D13 to determine the position: HIGH is one position and LOW is the other.

If it's a 3-position "center off" switch then move the D12 wire 4 holes to the left so that it connects to the switch-resistor junction, not ground. Then you will need to read both D12 and D13 - which ever one is HIGH is the one that is activated, and the switch is in that position.

Then it's a simple "if / else" (2 position) or "if / elseif / else" (3 position center-off) structure to decide which code to run.

| improve this answer | |
  • “Your switch wiring for D12 is wrong. You connect your inputs directly to ground and that can never change, regardless of the position of the switch.” I don’t understand how it is wrong, each side of the switch is basically like a push button, and that’s the exact same wiring as a push button. I do agree with “However you are over complicating things. A SPDT switch is nothing more than two buttons, only one of which can be pressed at one time. If one is pressed the other must be released”. It’s a 2 position switch. – Brandy Apr 17 at 13:24
  • The method I first used did work but once I move the switch to one position, it won’t go back to its former position(as in not physically). I think maybe there’s a problem with the code. – Brandy Apr 17 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.