 what is int for

Ello all! I am a full time mechanical engineering student with a begree in network engineering and business, however, with all the routers and firewalls i have configured, i have never had to program before. So this is all new to me.

I am following the basic blinking light test for my nano

by looking at the nano, i cant seem to figure out how people can tell that pin D13 is attached to the on-board led. But ill trust that info for now haha.

Ok, i have the sketch actually working and uploaded to the nano. It works! yay.

but i dont want to just copy text and paste it. i want to know what is going on.

So with thta in mind, im am curious to what the int function is actually doing. here is some partial code:

int ledPin = 13;

pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, HIGH);

what is that above function actually doing. as in, what is int doing? To me, i was told that int means integer but the only integer i see up there is 13, so what is ledPin doing in the command?

I also noticed that if i change the following, it still works

int lp = 13;

pinMode(lp, OUTPUT);

digitalWrite(lp, HIGH);

so i am confused to what the int comand actually does.. haha

thanks for the help

int ledPin = 13;

A variable, being an integer, called "ledPin" is equal to 13.

int lp = 13; A variable, being an integer, called "lp" is equal to 13.

See Data Types --

https://www.arduino.cc/en/Reference/HomePage

(you need to be on that page to get on with things Arduino)

And you only need byte there as 13 fits in 8-bits (0-255). You don't need 16 bits (0-65535).

i cant seem to figure out how people can tell that pin D13 is attached to the on-board led

You can see that on the schematic. https://www.arduino.cc/en/uploads/Main/ArduinoNano30Schematic.pdf

"int" tells the compiler how to reserve space in memory, e.g. on AVR Arduinos it is two byts or 16 bits. Arduino is using the GCC C++ compiler, but I think it has all the C data types.

https://en.wikipedia.org/wiki/C_data_types

[quote author=Runaway Pancake date=1437074441 link=msg=2318934] int ledPin = 13;

A variable, being an integer, called "ledPin" is equal to 13.

int lp = 13; A variable, being an integer, called "lp" is equal to 13. [/quote]

So let me make sure i have this right, the variable is named ledPin and that is = to 13, which the arduino interprets as pin 13. im not seeing how the variable itself is an integer?

are you saying that the word "ledPin" is the variable that represents some sort of integer, but is displayed as letters rather than numbers?

LOL, this is why different than anything i have done haha. I once had a diff eq professor tell me that if i can do diff eq and linear algebra, then learning c++ or c is easy.... i dont know bout that.

thanks

A variable, being an integer, called "ledPin" is equal to 13.

If you're at all mathematically minded, this is a problem. You should instead say "A variable, being an integer, called "ledPin" is assigned the value 13."

ok, i think my confusion lies with the fact of... i dont know what is being called a variable in the programing language.

is the string

int ledPin = 13

the variable? or what exactly is the variable?

i mean im used to seeing stuff like

dy/x = y`-4y - 12y =6cost

i know what my variables are in this sense of diff eq's but not programing haha.

thanks again for all the help

The variable is called "ledPin" To be picky, it should be a constant "const int led pin = 13;", and to be even pickier, it shouldn't be an "int", it should be a "byte". "byte" and "int" are data types and describe the range of values that can be stored in a variable or constant of that type and the operations available for that type.

You could do it this way

void setup(){
pinMode (13, OUTPUT);
}
void loop(){
digitalWrite (13, HIGH);
delay(250);
digitalWrite (13, LOW);
delay(250);
}

But then you have to remember "What is 13 doing?" And what if you want to use another pin? Now you have to change 13 to something else in at least 3 places. What if you used it 25 times? That gets tedious to make a change. So give it a name - x - and define what pin x will be:

byte x = 13;
void setup(){
pinMode (x, OUTPUT);
}
void loop(){
digitalWrite (x, HIGH);
delay(250);
digitalWrite (x, LOW);
delay(250);
}

Now you can change it once and the whole sketch is now updated. x, y, z - still not very descriptive - what is x doing? y? z? So give it a better name, and add some comments

byte ledPin = 13; // status LED
void setup(){  // code that runs once
pinMode (ledPin, OUTPUT); // set pin to OUTPUT mode
} // end of setup code
void loop(){ // code that runs over and over
digitalWrite (ledPin, HIGH); // turn on LED
delay(250);                       // hold to let user see it on
digitalWrite (ledPin, LOW); // turn off LED
delay(250);                      // hold to let user see it off
} // end of loop code

and now you can read the code and have a much better idea of what is going on. This example is a bit extreme, but the same principles can be applied so you can follow the code and remember what you were doing in 6 months when you want to make a change.

A variable is a named address in memory. The Bucket Analogy might help. The statement:

int ledPin = 13;

defines a variable named ledPin and it is assigned a memory address by the compiler (also called the variable's lvalue) where it resides in memory. Let's say the compiler places it starting at memory address 200. The int keyword is called a data type specifier, and it determine how much memory is reserved starting at address 200. For the Arduino family, an int is an integral data type (no decimal values) and takes two bytes (or 16 bits) of memory. In the statement above, we have written the statement so that ledPin has a value (also called an rvalue) of 13.

The Bucket Analogy says that a bucket exists at memory address 200 with the name of ledPin painted on its side. Because it is an int data type, the bucket is big enough to hold 2 bytes of data. If we look inside the bucket after the statement above is processed, we will see the bucket contains a binary value equal to the (decimal) value of 13.

You can also say ledPin has an lvalue of 200 with an rvalue of 13, which is nerd shorthand for what is said above.

@LT72884:

When programming, you will also need to get used to the fact that notation in C is different than notation in mathematics.

For example:

int x = 3; // OK, now x is worth 3

int y = 7; // OK, now y is worth 7

x = y; // this changes the value of x
// remember, we set y to 7
// so now x is also worth 7

y = 8 - y; // this is a little funny
// if this were algebra, then y would be worth 4
// but this isn't algebra, it's c++
// so how much is y worth here?
// remember, last time we did anything with y, it was worth 7
// so now we calculate 8 minus 7, which equals 1
// so after this statement, y will be worth 1
// this statement DOES NOT affect the value of x
// (x is still worth 7)

LT72884: So let me make sure i have this right,...

What you must do is delve into the link that I provided there in "Reply #2" - emphasizing, for your purpose here, those entries related to Data Types (that's all in the middle of the page.)

Here's that link again -- https://www.arduino.cc/en/Reference/HomePage

> > > Maybe I shouldn't bring this up, but there's a difference between int in the Arduino context, your concern, and that in a C++ context where there's int main () (the equivalent of Arduino void loop () )

Another gotcha here is that the Arduino default int size is 16 bits (unlike nearly every other C implementation you will find where an int is 32 bits).

The data type says what the size and representation of values is. int x = 13 ; is actually stored in memory something like

x: 00001101
00000000

(Assuming int is 16 bits and the machine is "little endian")

A float variable would store 13.0 very differently. The character string "13" again is very different, the C language defines the meaning and representation for each type (although the actual hardware will have some bearing, such as endianness, default int size, etc).

Variables in C are either at top level, meaning they are globally accessible (global scope), or inside a function in which case they come into existance each time the function is called, and each call has its own variable. Such "local" variables vanish when the function returns.

[ I'm glossing over things like the static keyword and C++ things here, note ]

MarkT: ...unlike nearly every other C implementation you will find where an int is 32 bits...

That is true only if you're willing to ignore the literally thousands of c and c++ implementations on 8 and 16-bit processors over the last 30+ that also define int as 16-bit. The fact is, an int is nearly always defined as the default "word" size for the processor on which the code is run. For 8 and 16-bit processors, which dominate the embedded world, this is 16-bits. For 32 bit processors it will nearly alwaus be 32-bits, and for 64-bit processors, 64-bits. The c and c++ standards, from the very first formal definition of the language by Brian Kernighan and Dennis Ritchie back in the early '70s, explicitly leave this detail up to the specific implementation.

Regards, Ray L.

First things first. Thank you all for helping me out with this. Thank you all for your replies and very descriptive posts. I know some can pick up programing faster than others, whilst others can pick up math or other things faster. I am one who likes to see what each part is doing. Thank you all for taking the time out of your day, family life etc etc to reply and help me out.

@runaway pancake: Thank you for that link. i am still reading it, as in im reading it for the 5th time haha. Since i am new to this, im still deciphering what it means.

as of right now, im still reading all the posts

@MarkT: actually, there are three scope levels:

global scope – the variable is defined outside of any function and is “visible” everywhere from its point
of definition to the end of the file in which the definition is made.

function (local) scope – the variable is defined within the start/end braces for the function block. The
variable is visible from its point of definition to the closing brace of the function block

statement block scope – the variable is defined within a single statement block. The variable is visible
from its point of definition to the end of the statement block, e.g.,

for (int i = 0; i < 10; i++) { // i becomes visible here
char c; // c becomes visible here
// some code…
} // closing brace of for statement block…i and c are now dead

econjack: global scope -- the variable is defined outside of any function and is "visible" everywhere from its point of definition to the end of the file in which the definition is made.

NOT "end of the file", but "end of the compilation unit". You can, for instance, declare a variable in an include file and it will be visible to all following include files. If it is declared outside any function, it also is global, and can be accessed by OTHER compilation modules using extern.

Then there is static, which restricts the scope of a variable declared "at global scope".

Regards, Ray L.

"The Bucket Analogy says that a bucket exists at memory address 200 with the name of ledPin painted on its side. Because it is an int data type, the bucket is big enough to hold 2 bytes of data. If we look inside the bucket after the statement above is processed, we will see the bucket contains a binary value equal to the (decimal) value of 13."

So, is the binary value of 13, the two bytes of data? or what is considered the two bytes of data that said bucket holds?

Also, how does the program know that the numerical value of 13 is referring to the pinout of D13 for the LED? Is that were pinMode comes in and says "hey, i have an input named ledPin which is set to a value of 13, so therefore i must send output to pin 13"

Thanks

@MarkT: actually, there are three scope levels:

There's also file scope; a static variable declared at global level is only visible in that file, but has global scope within the file.