DPDT switch not working

Hello,

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

int SwitchPositionPinA  = 4;
int SwitchPositionPinB = 2;

byte switchState = 0;


void setup() {
  Serial.begin(9600);
  
  digitalRead(SwitchPositionPinA);
  digitalRead(SwitchPositionPinB);
  pinMode(SwitchPositionPinA, INPUT);
  pinMode(SwitchPositionPinB, INPUT);
  pinMode(11, OUTPUT);
  pinMode(9, OUTPUT);


}

void loop() {
  if(SwitchPositionPinA == HIGH) switchState = 1; 
  if(SwitchPositionPinB == HIGH) switchState = 2;
  else switchState = 0; //off
  

  switch(switchState)
{
  case 0:
  digitalWrite(11, LOW);
  digitalWrite(9, LOW);
  Serial.println("off");
  break;  
  
  case 1:  
  digitalWrite(11, HIGH);
  Serial.println("on1");
  break;

  case 2: 
  digitalWrite(9, HIGH);
  Serial.println("on2");
  break;

 
  
}
}

I've also drawn up the schematic on fritzing. I was pretty sure I did this right but cannot locate the issue. This is the switch i'm using: Gardner Bender 10 amps Toggle O-Ring Switch Silver 1 pk - Ace Hardware any help would be appreciated.

My apologies, I didn’t know that I couldn’t upload a .fzz file. I’ve uploaded a .png instead.

What schematic?

FYI

.

Can you take a picture of the circuit?

.

Thete's no DPDT switches in that schematic.

I found the DPDT slide switch in the fritzing diagram picture.

What is the serial port telling you?

digitalRead(SwitchPositionPinA);
digitalRead(SwitchPositionPinB);
These do nothing in setup move them to loop() but, you place them in the if() statement.

As in:
if(digitalRead(SwitchPositionPinA) == HIGH) switchState = 1;
if(digitalRead(SwitchPositionPinB) == HIGH) switchState = 2;

edit
are there two switches?
if one switch you do not need to monitor both contacts.

edit
What are you trying to do?

.

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! :grin: 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:

#define switch4 4
#define led9 9
#define led11 11

void setup() {
  pinMode(switch4, INPUT_PULLUP);
  pinMode(led11, OUTPUT);
  pinMode(led9, OUTPUT);
}

void loop() {
  if(digitalRead(switch4)) {
    digitalWrite(led11, HIGH);
    digitalWrite(led9, LOW);
  } else {
    digitalWrite(led11, LOW);
    digitalWrite(led9, HIGH);
  }
}

ChrisTenone:
Hook up the LEDs the way you have them in your fritzy.

Except in opposite polarity.
(In the Fritzing, he has the LEDs connected backwards. They're not quite as bright like that. :wink:

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.

There are THREE positions to this switch?! I think that you had better explain the switch.

Must be a rocker with a null middle position

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.

Picture of the switch from the link provided by LockeLamora:

Possible connection scheme:

Position PinA PinB
   1     LOW  HIGH
   2     HIGH HIGH
   3     HIGH LOW

But it also begs the question - why are you using a microprocessor when the switch alone can switch the LEDs in the desired sequence

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.

Maybe DP is also on the Arduino power line, with both throws joined so it’s either on/off.