Toggle Switch help

Hey guys i have a real problem this time i recntly got this switch from radioshack(Switch) and ive been trying to use it so when its HIGH the led is on and when its LOW the led is off here's my code for it.

//Switch LED TEST
const int ledPin = 4;
const int switchPin = 3;
int switchState = 0;

void setup() {
pinMode(ledPin,OUTPUT);
pinMode(switchPin,INPUT);
Serial.begin(9600);
}

void loop() {
switchState = digitalRead(switchPin);
if(switchState = LOW){digitalWrite(ledPin, LOW);}
if(switchState = HIGH){digitalWrite(ledPin, HIGH);}
Serial.print(switchState);
}

In my opinion i don't see anything wrong with this code and when i use it on my arduino uno the led is just constantly on and the serial port is saying the the switchstate is HIGH...Any help would be appreciated.

Thank You

these are not tests, they are assignments :

if(switchState = LOW){digitalWrite(ledPin, LOW);} if(switchState = HIGH){digitalWrite(ledPin, HIGH);}

you must use "==" instead of "="

This is an unfortunate choice made when the C language was designed many years ago, many other languages avoided the pitfall by using a different symbol to '=' for assignment (':=' was one, read as "becomes equal")

One wonders how many millions of hours have been wasted over the years hunting down this sort of bug...

Hi i have changed the code like you said but now the led only turns on and the serial monitor goes to 1 if the switch is balanced in the middle; and if the switch is all the way on serial monitor goes back to 0 and led is off...Any help would be appreciated.

I recommend you think about getting a multimeter. This would be a lot easier to troubleshoot if you could "Ohm-out" the switch, and if you could check the voltage on the Arduino's input pin to see if it's high or low.

Are there any markings on the switch? A 3-termnal SPST switch is unusual. I assume one of the terminals is unused (not connected to anything). The switch could be wired wrong.

Can you show us a schematic?

Do you have a pull-up or pull-down resistor? (Alternatively, you can [u]enable the internal pull-up resistor[/u] by writing a high to your input-pin after configuring it as an input.) If you don't have a pull-up or pull-down, the input will "float" to an undefined state when the switch is off (open) and there is no connection to the input.

Well i fixed it...But the answer to it sort of confuses me at first i assumed the middle pin on the switch marked acc was for the light on the top of the switch but then i pulled that out and then it worked just for some reason when it was off the led was on and vice versa but i switched that and now it works perfectly also with the led on top now i'm just wondering what does the acc prong do if it all works normally including the led on top with just the positive and negative... Thank you for all your suggestion

It's an automotive switch. Start asking people who hang around cars. They will be able to tell you right away. "Acc" stands for "Accessory". It is supposed to be connected to the device that is powered from the switch (like fog lights, or whatever).

DVDdoug: Do you have a pull-up or pull-down resistor? (Alternatively, you can enable the internal pull-up resistor by writing a high to your input-pin after configuring it as an input.)

Old school... pinMode(x, INPUT_PULLUP)'s much simpler.