DPDT Switch?

A simple question, but I seem to be having trouble getting a DPDT switch working with the arduino. I'm trying to get one of two LEDs to turn on depending on the position of the switch, and I couldn't say exactly what I'm doing wrong. Assuming I've got the power leads of the switch connected to the 5v and the ground on the arduino, and each of the signal leads to two digital pins, should the following code not turn on the LEDs depending on the switch position?

int enablePin = 3; int ledAPin = 4; int ledBPin = 6; int input1Pin = 8; int input2Pin = 9;

void setup() // run once, when the sketch starts {

pinMode(ledAPin, OUTPUT); // sets the digital pin as output pinMode(ledBPin, OUTPUT); // sets the digital pin as output pinMode(input1Pin, INPUT); // declare switch 1 as input pinMode(input2Pin, INPUT); // declare switch 2 as input }

void loop() // run over and over again { if (digitalRead(input1Pin) == HIGH) // check if the input is HIGH { digitalWrite(ledAPin, HIGH); } else { digitalWrite(ledAPin, LOW); }

if (digitalRead(input2Pin) == HIGH) { digitalWrite(ledBPin, HIGH); } else { digitalWrite(ledBPin, LOW); } }

Could you show us a picture of the setup? You didn't mention any resistors...

/me

As PA Skins implies, you need to wire the switch correctly and use pull-up or pull-down resistors for the sketch to work. BTW, you can implement the same logic with less code as follows:

int ledAPin = 4;                            
int ledBPin = 6;                    
int input1Pin = 8;                
int input2Pin = 9;              


void setup()                       // run once, when the sketch starts
{

  pinMode(ledAPin, OUTPUT);      // sets the digital pin as output
  pinMode(ledBPin, OUTPUT);      // sets the digital pin as output
  pinMode(input1Pin, INPUT);       // declare switch 1 as input
  pinMode(input2Pin, INPUT);       // declare switch 2 as input
}

void loop()                        // run over and over again
{       
  digitalWrite(ledAPin, digitalRead(input1Pin));  
  digitalWrite(ledBPin, digitalRead(input2Pin));
}

If you need to invert the output (led on when switch level is LOW, you can put an exclamation point in front of the digitalRead (this is the C logical negation operator, it inverts the logic)

digitalWrite(ledAPin, !digitalRead(input1Pin));

Thanks for the code, where might I put the resistors? My physics teacher suggested putting a 1k resistor between the switch and the pin-ins, would that make sense? I'll try and get a picture up soon.

My physics teacher suggested putting a 1k resistor between the switch and the pin-ins, would that make sense?

No you tell him he shouldn't be teaching physics if he suggests that.

Pull up resistors from input lines to supply, about 3K. LEDs should have a series resistor, about 300R.

As I understood it from reading the description, he's got the DPDT switch wired up to 5V and Ground, with the wiper connected to the Arduino's input pin. So, he doesn't need any pull-up resistor(s).

Could you post a schematic, showing us exactly how the switch is wired up?