One thing that always confused me was why we use int or other commands to set the variables to have pin numbers as if those commands were always restricted to pin numbers only. For example
int ledPin = 13;
Does putting a random number in place of 13 that isn't a pin number cause an error? Right now I haven't checked yet.
I've only begun to realize that if you want to use variables in relation to pins or push buttons you have to set values first and then do associated functions like digitalRead. The commands like int are not like in other programming languages where you can set any values and do mathematical computations. I know it might seem obvious to someone who is used to Arduino, but this perspective is something that helped me grasp things better. Before that, I totally could not understand how things worked because of being confused with how int or other similar variables worked.
int ledPin = 13;
does in C++? This actually does two things:1) It declares the variableledPin
as a variable of type "integer" and 2) It assigns the value 13 to that variable. It could also be written as two separate commandsint ledPin;
andledPin = 13;
. It has nothing to do with port numbers until you use the variable to refer to an IO port like withdigitalRead();
ordigitalWrite();
.int
is not a “command”, it's a data type. The whole line is a variable declaration:int ledPin = 13;
means “let ledPin be an integer initially having the value 13”.