I looked around in the forums but there is still something I don't understand.
I have some background with programming but I'm a complete beginner with electronics.
Doing various tutorials so far I've used the simple little 4-pins button (the one in the starter kit) in various ways to control some basic on/off stuff. Every time I was required to put a 10k resistor in the circuit. I have a very basic understanding of Ohm's law, and I assumed that 10k Ohms were required for the same safety reasons they are used in LEDs. But apparently this is not the case, because I found other exercises in which the button is directly connected to the 5V and GND jumpers without any resistor. I haven't been able to find any explanation for this difference.
Does the simple button require a resistor or not? What might be the reason why it is there in some exercises and not in others?
(if you lack context, consider that these are extremely basic exercises like "turn on the onboard led when button is down", "print to console when button is down" and stuff like that.)
In the exercises so far I see HIGH and LOW states declared as "int". But digging further it looks like they are just placeholder names for 1 and 0. It'd seem more appropriate (and cheaper memory-wise) to have them as bools, but maybe there's a reason they are always declared as int?
That's a good question because they are pre-defined as type bool! I would have expected re-defining them to cause an error, or at least a warning...
It's OK to copy a boolean value into an int (int X = HIGH;) but you should have a good reason for doing so. This is called "casting" or "typecasting".
You can also copy an int value to a type bool but the result will be 1 for all non-zero values.
Resistors are used for many different purposes and sometimes the value is not that critical....
On a pushbutton or switch they are used as "pull-up" or "pull-down" resistors. (The ATmega chip actually has built-in pull-up resistors, about 20K I think, that can be optionally enabled.)
Unconnected inputs (like when the switch/button is off) are "floating" and undefined... The voltage is unpredictable and they can read high or low.
A pull-up resistor will pull the input high when the switch is off. The input has very-high impedance so almost no current flows, and almost any resistor value will work (1K to 10K is common). The button/switch is tied to ground so when the button is pressed the input forced to ground (low) and current flows through the resistor.
LEDs are a bit "odd" because (like all diodes) they are non-linear, which means their resistance changes with the voltage. Ohm's Law is a law of nature and it's always true but we can't use it directly on the LED because we don't know it's resistance...
The resistor and LED form a Voltage Divider where one resistor is replaced by the LED. If we are applying 5V and the LED is spec'd at 2V we know there will be 3V across the resistor.
We also know that the same current flows through the LED & series resistor so knowing we'll have 3V across the resistor, we can use Ohm's Law to calculate the required resistance for the current we want.
So HIGH/LOW for all intents and purposes may as well be declared as bools. I was just fearing there could be some reason not immediately apparent for having those ints, but apparently there is not. I will proceed to make it a habit of declaring them as bools regardless of what the tutorials use. Overall it doesn't change much, but it seems more appropriate and consistent with what those values actually are.
Regarding the resistors... I thank you all for the help you offered, but I'm realizing I still need to learn so much more about electronics to fully understand what was explained
Anyway this new Arduino world looks amazing and I can't wait to learn more. Thank you all again, see you around!
You do not declare or define them in code. And as such, it does not matter what they are. What "exercises" would these be?
Well, you did not find a button "directly connected to the 5V and GND", it connects to one or the other but best practice by far is to connect it to an input and ground and use a pull-up either in the Arduino itself or externally to 5 V.
Alright I'll try to explain myself better. I tried to log variables with HIGH/LOW values and they always print either 0 or 1. I then tried to declare those exact variables as bools and it still logged only 0 or 1 (as expected) and there weren't any errors that I could notice.
So why would I declare a, say, "ledState" variable, as an int? Couldn't I just declare it as bool and save 1 byte?
Of course my concern is not so much saving that 1 byte as much as understanding the internal workings of this new platform. So put it differently: when storing HIGH/LOW values, what are the pros and cons of doing it in an int variable or in a bool variable?
My current knowledge and understanding only allow me to go as far as to say "well, I expect only 2 possible values and a bool allocates less memory so let's just use a bool". Is this a wrong way to think in this environment? And why would an int be better?
As in arduino an int is 2 byte declaring as a bool might very well save 1 byte.
But why not declare as a bool? Even if it does not save memory? It makes clear that you expect only 2 possible states (true/false LOW/HIGH 0/1).