Hi!
I just started off with my first Arduino Sketch which, based on the voltage on 1 input pin, regulates 3 output pins connected to LEDs.
The program works just fine and includes a pushbutton which I use to decide the value of the input pin; when I click it the LEDs light up and when I do not press the button no light appears (rightfully so).
What confuses me however is why, once I press the button, the LEDs that are supposed to be on only when the button is pressed keep on being light up forever even if I disconnect the button from the breadboard or even when I disconnect the cable connecting the input pin to the breadboard.
How is it possible that although no voltage is present in the input, the output is still the same as if there was voltage in the input?
Thank you very much!
int switchState = 0;
void setup() {
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
}
void loop() {
switchState = digitalRead(2);
if (switchState == LOW) {
digitalWrite(3, LOW); // green LED
digitalWrite(4, LOW); // red LED
digitalWrite(5, LOW); // red LED
}
else {
digitalWrite(3, HIGH); // green LED
digitalWrite(4, HIGH); // red LED
digitalWrite(5, HIGH); // red LED
}
}
P.S. I know I am not supposed to disconnect parts of the circuit while running but since I am starting out I am trying to learn by disconnecting and connecting components and seeing what happens.
