I'm using a Mega2560 board. Based on a tutorial, I connected this circuit, using a pull-down resistor. It works.
Then, as I'm trying to wrap my head around pull-down/up resistors, I am seeing this schematic:
I noted that I do not have the 10K resistor between the button and input pin (white wire). Is this extra 10K resistor necessary?
Also, I thought maybe the 2560 includes a built-in resistor. I looked at the datasheet, but could find no reference to a resistor that made sense.
No it doesn't. Some people add this for safety in case you make a mistake and antecedently make this pin an output.
The use of pull down resistors is not the best way to wire an input. You can do it all without any resistors at all by using the internal pull up resistors built into each Arduino.
No not at all, I think you are missing the point here.
When a normal push to close button is used then HIGH is the un-pushed state and LOW is the pushed state. You just use these states in your code.
Many beginners might find this confusing but it is the way most people use it. If you are uncomfortable with this then add to the top of your sketch
#define PUSHED LOW
#define UN-PUSHED HIGH
and then use things like
if(digitalRead(buttonPin) == PUSHED) { // do the stuff when pushed
Unfortunately, the "tutorials" on the Arduino site generally show switches to +5 V and pull-down resistors. They are badly written in many ways.
One reason to wire the switches to ground, is that you really do not want the 5 V supply wire going out to any part of an assembly that it does not need to as there may be some risk of wires becoming damaged and shorting it to ground.
Why not?
It will compile and it will work. I don't think the compiler is advanced enough to have feelings.
Yes that is what it is. CLOSED can replace PUSHED.
Y the way there is a tradition that goes if all the letters in a variable name, that variable is in effect a constant. You can totally ignore that if you like but it makes the code simpler to understand at a glance, and is considered good practice.
Sorry, it will like UN_PUSHED but not UN-PUSHED. Typo, apologies.
C:\Users\serretje\AppData\Local\Temp\arduino_modified_sketch_250350\sketch_jul26a.ino:1:11: warning: ISO C++11 requires whitespace after the macro name
#define UN-PUSHED HIGH
^