I'm testing individual components and code as I go for the project i'm working on. I have the code and the circuit designed but i'm not getting anything to happen. The code is
digitalRead(SwitchPositionPinA);
digitalRead(SwitchPositionPinB);
These do nothing in setup move them to loop() but, you place them in the if() statement.
Hiya Locke Lamora! Welcome to the arduino.cc forum! As you can tell, there is a lot of information here.
I’m looking at your fritzy, and it looks like you are trying to flip one switch and have one LED go on, and the other one go off, or something like that, right?
Your programs needs to work like this:
Is the switch in position A?
if yes: turn LED A on and turn LED B off
if no: turn LED A off and LED B on
– that’s all! … you don’t need no stinkin’ DPDT! Any switch will work. I like small slide switches, like this one:
It’s got 3 contacts, but you can cut one of the outer pins off. That makes it into a simple SPST switch that is either open or closed. Connect one of the pins to ground and the other to pin 4. Hook up the LEDs the way you have them in your fritzy. Run this code:
I changed the code and the LEDS and its working! This will be for a nightlight for my daughter. I'm trying to troubleshoot things piece by piece as i'm new to all of this. The switch in position 1 will turn on the nightlight feature, the switch is position 2 will turn it on as just a lamp, and position 0 will turn everything off. I am using Neopixels. Thank you all for your help. I really appreciate it.
LockeLamora:
I changed the code and the LEDS and its working! This will be for a nightlight for my daughter. I'm trying to troubleshoot things piece by piece as i'm new to all of this. The switch in position 1 will turn on the nightlight feature, the switch is position 2 will turn it on as just a lamp, and position 0 will turn everything off. I am using Neopixels. Thank you all for your help. I really appreciate it.
The only way to do that with a three-position switch like that is to bias the centre pin to mid-rail and
use analogRead(), not digitalRead().
I would consider just using a single push-button switch and cycling the states in software - then you
are not limited to 3 states. Its no co-incidence that non-push-button switches are uncommon in
modern devices, once you have software reading a switch you can do much more with a simpler and
cheaper piece of hardware. For instance 2 push buttons can be a volume control, as fade-up and fade-down.
Left side of switch package shows ON/OFF/ON operation.
OP decribed application as: ( Leds ON/Leds OFF/Leds DIM)
Schematic posted by DLoyd shows app only requires SPDT switch.