I just got my arduino starter pack yesterday, and i'm just testing this out, a very simple idea, a light switch.
int switchstate = 1;
int ledPin = 13;
int speaker = 7;
int listener = 8;
void setup() {
pinMode(speaker, OUTPUT);
pinMode(listener, INPUT);
digitalWrite(speaker, HIGH);
}
void loop() {
switchstate = digitalRead(listener);
if (switchstate == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
So I have a wire going out of speaker and into listener, with a switch on the wire. Flick the switch on, and the voltage from speaker should go through the wire into listener, listener returns HIGH, light comes on.
But for some reason, the light remains on forever, regardless of switch position. I can even remove the cables and it still stays on!
I specifically said to search for pull-up because the Arduino (ATmega chip) has built in pull-up resistors. Re-read the pinmode() reference page to understand how to turn them on.