Hey, I just startet with programming Arduino and I am very confused because I'm not even able to make the Serial Monitor print that I am tapping my button.
Instead of writing that as long as I am pushing it, it tells that for about 13s after I stopped.
This is my code:
It looks like you are testing whether it is connected to 5V to determine that it is pressed but is there a resistor in place to keep it at GND (LOW) when it is not pressed or is it floating at an unknown voltage, maybe HIGH, maybe LOW, maybe changing ?
Consider using INPUT_PULLUP in pinMode() for the button pin to turn on the built in pullup resistor, wire the button to take the pin LOW when the button is pressed and test for LOW to know when the button is pressed
millis() gives you the time since the sketch started.
So if you press the button after 13 seconds, you will get the value that you see and it will increase. If you don't release the button, that time will increase.
See further @UKHeliBob's answer about the INPUT_PULLUP and the wiring of the button; you can do a search on the web for 'floating inputs'.
Hey, thanks for your answer.
My button is connected to 5V on one side and to A0 on the other.
If I understood your advice right I should put a resistor in between but both the regular 220Ω resistor and the INPUT_PULLUP resistor didn't solve the problem.
@sterretje it starts with
S1 pushed
99
(I didn't have enough space on that screenshot)
and i just quickly tapped on it
You mean it stays on a random voltage lvl when i leave the button?
That is why the program still recognizes an input for a while.
And the INPUT_PULLUP resistor in contrast immediatly pulls it back down.
If a button used as an input is not held at a specific voltage level then it is said to be "floating" and will pick up electrical noise from its environment so will be at an unknown level