Push button staying on for 3 seconds after unpressed

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);
  }
  
}

P.S. quick side note this also happens when you use the example code here: https://docs.arduino.cc/built-in-examples/basics/DigitalReadSerial
(I don't have any 10kohm resistors so I didn't use them).

This is not the same as:

if (button1 == 1){


pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);

1 Like

As you don't have a problem with the IDE, your topic has been moved to a more suitable category on the forum.

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.

Try using any resistor... or put some in series to add their resistance.

As @Grumpy_Mike has covered:

1 Like

Hello pagemonkey

Welcome to the worldbest Arduino forum ever.

In general, you should not use magic numbers. The I/O pins love to have a functional name.

Have a nice day and enjoy coding in C++.

Confusingly, c has two "equals". (or more precisely, our language around "equals" is inexact)

A = B - even if (A=B) COPIES B to A

= is an ASSIGNMENT operator

so if A is 3 and B is 2 before this line
if (A=B)
A will become 2 and B will be 2
therefore the IF will always be true.

A == B is a COMPARISON OPERATOR and will return "TRUE" if A is equal to B

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.