Reputation: 11
This is my code I wrote for when I press the button the Led turns on , when I let go of the button then it switches off. It's always on when I press the button and always on when I let go off the button. Why is it always on?
int Input_Pin = 13;
int Out_Pin = 2;
int Button_read;
void setup()
{
pinMode(Input_Pin, INPUT);
pinMode(Out_Pin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
Button_read = digitalRead(Input_Pin);
if (Button_read == 1) {
delay(1000);
digitalWrite(Out_Pin, LOW);
Serial.println("off");
}
if (Button_read == 0) {
delay(1000);
digitalWrite(Out_Pin, HIGH);
Serial.println("On");
}
}
Upvotes: 1
Views: 158
Reputation: 41
I think I found an issue in your diagram...
Basically, your pin 13 is always gonna be HIGH
because the 5V has a direct connection through it. There is nothing wrong with your code Allow me to demonstrate
The fixed circuit for this would be (The red is wrong and the blue is the fixed connection):
Upvotes: 2