int ledPin = 13

Hi,

I'm trying to teach myself how to program an Arduino but I am stumped at the first hurdle. The very first line of programming anyone comes across is: int ledPin = 13 I've looked everywhere for an explanation of this with no joy. All the explanations start with the next line which is the void setup. Can someone pull the line apart and explain each of the terms? By the way, it won't be helpful if you say that int stores a 16 bit value.

Regards, Alan

Yes, should be
byte ledPin = 13;
as its value is in the range 0 to 255 and one byte of memory can store it.

This lets you use the name ledPin in the code that follows when you want to read the level (HIGH, LOW) of pin 13 as an input, or when setting the level (HIGH, LOW) of pin 13 as an output.

If you are using all 20 IO pins, it is a lot easier to keep track of what’s what with names vs just #s.

Thanks CrossRoads but I still don't understand what the int or the byte term is for. Why do you need to specify a storage volume? In other parts of this site, int is defined as 2 bytes which corresponds to -32,768 to 32,767 whereas you say it's 1 byte corresponding to 0 to 255. So, I'm still confused.

That line, which is outside the setup and loop functions, declares a variable.

Specifically, it declares a variable named "ledPin". Because it is not inside any functions, this is a global variable - it can be referred to from anywhere within your sketch. It is a variable of type int - that is, a 16 bit signed integer, which can represent a value between -32768 and 32767. And it is being initialized to the number 13.

Pin 13 happens to be the number of the pin with the LED connected to it on the Arduino boards; The point of this variable is simply so that later on, when we want to refer to the pin with the LED connected to it, we can refer to ledPin, instead of pin number 13. Then if we wanted to use a different pin, we'd just change it in that one place - plus as Crossroads said, it's easier to remember.

Also as crossroads noted, there's no reason that has to be an int - it could just as well be a byte (8 bit unsigned integer - 0 to 255), and still store any pin number, thus saving a byte of memory.

The reason the size of the variable must be specified is that memory needs to be allocated for that variable, and you need to tell the compiler how much memory. Keep in mind, you only have 2048 bytes of ram, so every byte matters... In some higher level languages, the type of a variable doesn't need to be declared (they figure it out for you behind the scenes) - but this is C, and you do have to declare a type for all variables.

You may want to use:
const byte ledPin = 13;

Adding const (constant) will save you one byte of RAM.
A good habit to get into if your variable will not change in your program.
This may not be an issue in smaller programs but becomes important in larger sketches.
https://www.arduino.cc/en/Reference/Const

.

LarryD: You may want to use: const byte ledPin = 13; Adding const (constant) will save you one byte of RAM.

I was going to suggest the same. I've been amazed at how many examples declare pin numbers as int variables instead of as constants. It's not a good habit to get into, and those examples don't help at all.

@Kiwi2, if you declare it as a constant, (const), as suggested by Larry, it uses no RAM at all, (RAM is limited), and instead the compiler substitutes '13' for 'ledPin' during compilation, before the 'sketch' is uploaded to the chip.

If it is not declared as a constant, RAM space is allocated by the compiler, because it interprets it as a 'variable', a value that you might want to change while the sketch is running.

Sorry, I am not diving into explaining the most basic of terms & definitions.

sheesh. If someone isn't understanding "int ledpin = 13;", how do decide to say "well, it should be 'const byte ledpin=13;'", instead?

The reason the size of the variable must be specified is that memory needs to be allocated for that variable, and you need to tell the compiler how much memory.

A "variable" is like a little box where the computer will store a value. This is quite different than the definition of a "variable" in mathematics, sort-of. I've heard that people with a math background can get very confused the first time they see computer software, because of differences like this. I've never SEEN that happen...

The fact that you have to tell the compiler the type (and size (implied)) of the variable is not universal for computer languages. Some more modern languages take more of "I see what you've got there; I can find a box that's big enough" rather than the "if you're going to put that in a box, you have to tell be how big the box needs to be!" attitude of C/C++.

Thanks for all the replies. DrAzzy explained it really well and I understand the line now. CrossRoads comment about not being into explaining basic concepts confirmed my suspicion that the meaning of the line was blazingly obvious to an expert and therefore wasn't worth talking about in the tutorials for beginners. This is the reason why experts shouldn't be the ones writing tutorials - they should be written by people who have only just learned the topic and who haven't forgotten how ignorant they were - or by people like DrAzzy who seems to have a gift for teaching.

Regards, Alan

CrossRoads comment about not being into explaining basic concepts confirmed my suspicion that the meaning of the line was blazingly obvious to an expert and therefore wasn't worth talking about in the tutorials for beginners.

Bob was just adding some information/suggestion that new users might want to be aware of. You do have the option of not reading or absorbing information that people have to offer.

Edit: You may want to look at Defining Data Types, or not. https://learn.sparkfun.com/tutorials/data-types-in-arduino

Kiwi2:
This is the reason why experts shouldn’t be the ones writing tutorials - they should be written by people who have only just learned the topic and who haven’t forgotten how ignorant they were

There are many tutorials that start from the absolute beginning with C++ and Arduino. You’ve just been looking at the wrong ones.

Hi Steve,

Would you care to point me to one which explains the ins and outs of:

int ledPin = 13

I'm blowed if I can find one - and I've looked at a lot. My point is that this is the first line of an Arduino sketch newcomers are likely to see - and it's not explained anywhere I can find.

Regards, Alan

Have you seen? https://www.arduino.cc/en/Reference/VariableDeclaration

Arduino Foundations Page, a good place to start learning Arduino.

LarryD: Have you seen? https://www.arduino.cc/en/Reference/VariableDeclaration

Thanks Larry and GoForSmoke. I couldn't immediately think of a specific example and was about to do a quick search. No need now. :D

Kiwi2, once you've been through those articles, don't forget the point of using "const byte" or "const int" rather than just "byte" or "int"when declaring pin numbers. Without the preceding "const", (precious) RAM space is wasted for no good reason. This could become an issue when you get some practice/experience and start writing large, complicated programs that use lots of RAM. (You might run out). It's best to get into good habits right from the beginning. :)

Also, you'll find plenty of tutorials on C++. No need to limit yourself to just Arduino documentation. C++ has so much more to offer.....

Hi Larry, I've had a look at your link and it's a good example of an explanation by an expert. The reason for the "int" term isn't explained - presumably because it is so obvious. I couldn't understand the ins and outs of the line:

int ledPin = 13

from your link whereas DrAzzy's explanation made it clear. My point is that being an expert isn't sufficient by itself to qualify you to write a tutorial - you need to have a gift for imparting your knowledge simply and clearly as well.

Thanks for your link GoForSmoke. I'll give it a go.

Regards, Alan

The last link I offered says: Declaring a variable means defining its type . . . The word "type" is very important to understand. Did you take a look at the link I posted in #9 ?

Computer compilers are stupid, you have to tell them the "type" of your variable, it could be an integer, a byte, a float, a character, a long, unsigned int etc.

The choice of the “type” for your variable determines what you can do with your variable.
For example, if you make your variable “type” byte, then you can only have numbers between 0-255 in that variable.
This happens to be what one 8 bit byte can store.

If you use “type” unsigned long int, your variable can represent numbers between 0 to 65535 with it.
This “type” needs 16 bits or two bytes of RAM.

As mentioned, it is up to the programmer to assign the “type” to their variables.
By selecting the right “type” you can save your limited RAM

.

LarryD: If you use "type" unsigned long, your variable can represent numbers between 0 to 65535 with it. This "type" needs 16 bits or two bytes of RAM.

Larry, a typo - I'm sure you meant 'unsigned int'. An 'unsigned long' is 32-bit, 0 to 4,294,967,295.

Kiwi2: I'm blowed if I can find one - and I've looked at a lot. My point is that this is the first line of an Arduino sketch newcomers are likely to see - and it's not explained anywhere I can find.

Which C/C++ tutorials did you look at, which did not adequately explain the absolute fundamentals of how to declare a variable?