08) Variables

Don't worry, variables were meant to help. If you had a bad experience with them in math or not, they really are not that bad in programming. Instead of "solving for" a variable, you set them! It is really easy!
There are multiple types of variables that we can use:
  1. boolean
  2. char
  3. byte
  4. int (with unsigned int)
  5. long (with unsigned long)
  6. float
  7. double
  8. string
The standard variable that we use for numbers is an int, which stands for integer. This int variable has a range of -32,768 to +32,767. Which is fairly decent (NOTE: this range changes in different languages of programming, but this is the Arduino's consistent int range). An unsigned int variable has double the positive value, but the number has no negative. So the range of this variable unsigned is 0 to +365,535. If you go over the variable cap, it wraps around to the other side of the number-line.

As disused in another lesson, there are two positions you can define a variable, either globally or locally.
  1. Global variables:
    1. can be accessed in multiple functions
    2. Is not deleted at the end of the function
    3. BUT can only have one instance of that name in the whole code
  2. Local variables:
    1. Are deleted when the function is done
    2. Can only be accessed in that function that it was created in
    3. Can have multiple variables of the same name as long as they are not in the same function or defined globally. (this will keep them seperate from each other)
LOCAL VARIABLE USE:
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int myVar=5;
  myVar=myVar+2;
  Serial.println(myVar);
  delay(1000);//Delaying one second to show the results
}
Upload it and look at the results in the serial monitor!
It should display
7
7
7
7
and so on...
The action is constantly re-done every loop cycle, thus only showing 7's.

GLOBAL VARIABLE USE:
int myVar=0;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  myVar=myVar+1;
  Serial.println(myVar);
  delay(1000);//Delaying one second to show the results
}
Now this one is pretty cool to see when you run it! You will notice that the serial monitor gives out an incrementing cycle:
1
2
3
4
5
6
and so on... (approximately) every second it increments.

however, there are come tricks to write this a little bit easier for those that like to use long variable names like: "thisIsMyVeryLongVariableNameThatICannotFindTheNameOf" that do not like writing the name twice. But this technique that will be shown below is good even if you use short variable names like "i".

The command of "myVar=myVar+2;" can be shortened by using "myVar+=2;" (the += in that order). This allows the user to only need to write the variable once and show how much it need to be increased by (NOTE: you can also use negative numbers with this!). If you want to subtract, do -= and multiplying, *= and dividing /=. If you are adding by one, use "myVar++;" or subtracting by one "myVar--;".
Hey you might have realized why they named it C++ now! (just remind them to add the semicolon at the end... that was a pretty bad joke, now carrying on)


Lets now lets use our new found technique! Change them on both, you should get the same result. The second one should look like:
int myVar=0;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  myVar++;
  Serial.println(myVar);
  delay(1000);//Delaying one second to show the result
}
It may not be clear yet on why you will need to use a local variable yet, but I will show you some reasons why you will need to use a local variable with some other lessons.
Comments