Well, I've had my UNO r3 for only 2 days and even though I can envision all kinds of ambitious projects with it, I'm already struggling with simple operations.
I've got a bunch of click switches lying around and figured I'd use one to turn on an LED before I go connecting motors and such, but I'm not seeing the results I expect. Visual attached.
The click switch has three prongs: common, normally open and normally closed. I wired the C to the ground and the NO to Pin 5. Then I wired the LED to the ground rail and Pin 9. Set pin 9 to output, Pin 5 to input and run.
The issues I'm seeing are twofold: WHEN the switch works, it's opposite of what I want. Clicking turns the LED off, while leaving it untouched keeps it on. Oh, and if I take out the leads to the switch altogether, the LED seems to turn on and off by moving my hand near it.
I use a basic (modified) sketch to test it. I'm sure I'm doing something wrong and I'm hoping someone will see it immediately. Or is it possible, what with the light going on an off when my hand gets close or jiggles the board), that I've got a bad UNO or breadboard?
Sketch:
int pushButton = 5;
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
pinMode(9, OUTPUT);
}
void loop() {
int buttonState = digitalRead(pushButton);
Serial.println(buttonState);
if(buttonState==1){
digitalWrite(9, HIGH);
}else{
digitalWrite(9, LOW);
}
delay(300);
}