Many thanks for the nice answers!
However, I wonder why the LED came on without pressing the button.
I will definitely avoid saying, "see reply #"... ![]()
OMG, that is funny.
Please follow the link to the tutorial I posted...
Reread response #2, please.
When learning to code, you need to stretch a bit and think that if something must be in two states, code must exist to provision both states.
Schematics help us to help you.
You have a "floating input", so two problems actually....
aarg
louie_1
1m
You have a "floating input", so two problems actually....
When a digital pin, say D1, is left unconnected, it is called "floating: because the digital state of the input is undefined, maybe 0 (false) or maybe 1 (true.)
A similar sketch exists in the examples section,
here.
https://create.arduino.cc/projecthub/reverendfuzzy/simple-on-off-pushbutton-f637a7
Now, You have a complete example to study.
Further reading:
https://learn.sparkfun.com/tutorials/logic-levels/all
Yes, the tutorial says,
"If you disconnect the digital I/O pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resistor in the circuit".
As aarg says, you need a pre-defined input condition for digital inputs.
Explained reasonably well here:
https://www.seeedstudio.com/blog/2020/02/21/pull-up-resistor-vs-pull-down-differences-arduino-guide/
It is most often the little things that annoy us, you're get there soon. Adafruit has basic tutorials for many boards here.
Is there any point posting links if you don’t read them?
Thank you now I get it!
My mistake was, I didn't connect the button to ground properly.
So that I can get my mistakes across better next time, can someone recommend a free circuit diagram program (in the web browser)?
You may wish to do some Arduino simulations here:
https://wokwi.com/
For non-Arduino wiring, this is rather useful:
http://falstad.com/circuit/
Or, if you have Win-10/11 and a touch screen, Snipping Tool + Paint will suffice:
Or use Google to find something you like.
If that is the only change you made, you still have a flaw, the missing pull up resistor.
I added that too.
