Hey all,
I am pretty new to Arduino and I am trying to make a button turn a LED on and off. I tried doing this and it did not work with the LED so I started checking my output from the button on the serial monitor. When doing this I found that the pushbutton stayed on for 3 seconds after it had been released, and I tried making it go back to zero as you can see below (however it does not work).
Any help would be appreciated. Thanks!
void setup() {
// put your setup code here, to run once:
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int button1 = digitalRead(6);
Serial.println(button1);
if (button1 = 1){
button1 = 0;
delay(10);
}
}
This is all down to how you wired up your buttions. Can you show a diagram of how you wired it up please.
That would do it. You just can not ignore components like this. It sounds like these were pull down resistors, and leaving them out means you have a floating input, which will leave you these symptoms.
The alternative is to use the internal pull up resistors, as brought into play using the pinMode instructions that @LarryD showed you. Then connect your button between the input pin and ground. Note when you do this a button press will read as a 0 and a no press will read as 1, so you will have to change your code to reflect this.