703
edits
(Created page with "== Binary numbers and Variables == Any modern computer uses binary stored data. There are several ways to do that and there's are many different standards - however most of the ...") |
No edit summary |
||
Line 143: | Line 143: | ||
now myNumber has the same value as anotherNumber and its value is printed. | now myNumber has the same value as anotherNumber and its value is printed. | ||
==== Testing roll over ==== | |||
<source lang="c"> | |||
void loop () { | |||
Serial.println(myNumber); | |||
myNumber = myNumber + 1; | |||
} | |||
</source> | |||
Each time the loop is started again the number is first sent to the computer and then increased by 1. | |||
Let the program run for a while and see what happens when the border of an integer variable is reached. (when reaching 32,767) the number is suddenly "wrapped over" to the most negative number possible: -32,768 and from there starts again countin towards 0 - reaching the limit at the upper and again and so on. This roll over can be used in programs to infinitely count - however since it is not obviously at what point the number will roll over it's no nice programming style. | |||
Now you can use the same program but use "unsiged int" instead int. Observe that now the number rolls over to 0 and not to a negative number. | |||
You can't only modify variables inside the loop function, but also in the setup fuction: | |||
<source lang="c"> | |||
unsigned int myNumber; | |||
void setup () { | |||
Serial.begin(9600); | |||
Serial.println(myNumber); | |||
myNumber = myNumber - 1; | |||
Serial.println(myNumber); | |||
} | |||
void loop () { | |||
} | |||
</source> | |||
Now everything happens in the setup function - so it happens only once. Then the program will continue in the loop but there it does nothing and nothing and nothing again infinitely. | |||
Notice the roll over also happens in both directions. |
edits