Get Help
Integer Numbers
Plain Ol’ Numbers
There are two basic types of numbers you will need to worry about when writing code for Sparki or Hexy: integers and floating point numbers (also know as numbers with decimal points) Integers are numbers without a decimal point. An integer in SparkiDuino can be a positive or negative number anywhere from -2,147,483,648 to +2,147,483,647. Most of the time when you need to store a number, you will use an integer. Here’s what we’ll be going over in this lesson about integers:Declaring an Integer Variable
Before you can store a number you need to create a container to hold that number. If we think of variables like mailboxes to hold information and we think of the values stored inside the variables as letters then declaring a variable is kind of like putting a mailbox into the ground. Before you can get any information out of the mailbox you first have to put it somewhere so that it can receive letters! This is called declaring a variable. You can declare an integer using the word “int” followed by the name you want to give to the variable.
1 |
int integerOne; |
This will create a variable named “integerOne.” The type of this variable will be an integer. Since you didn’t give the variable a value when you first created it the value of the variable is automatically zero. Some programming languages will give empty variables a value called “null” but SparkiDuino is intended for beginners so it assigns new variables the value of zero.
Assigning Values (Putting Information in the Variables)
You can then change the variable by assigning a value to the variable. This is done simple by writing the variable name followed by an equals sign, followed by the value you want to put in the variable. Then you end your command for assigning a variable with the all important semicolon. Everything else is pretty self explanatory but the semicolon is there so the computer knows that it has reached the end of a line of instructions that should now be executed. You can think of the semicolon as a green light that tells the computer it has all the parts of a command and it can start moving ones and zeros around. Here’s what it looks like when we assign the value of eight to the integer variable “integerOne.”
1 |
integerOne = 8; |
1 |
int integerTwo = 6; |
Doing Math with Integers
Integers are good when you don’t need accuracy. They are great for counting, adding, subtracting and multiplying. Let’s do some basic math using integer variables. We’ll create two variables, one named “foo” and another named “blerp.”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <Sparki.h> // include the sparki library void setup() { } void loop() { sparki.clearLCD(); int foo; foo = 10; int blerp = 4; sparki.print( "foo: "); sparki.println( foo ); sparki.print( "blerp: "); sparki.println( blerp ); sparki.print( "foo + blerp: "); sparki.println( foo + blerp ); // foo added to blerp sparki.print( "foo * blerp: "); sparki.println( foo * blerp ); // foo multiplied times blerp sparki.print( "foo / blerp: "); sparki.println( foo / blerp ); // foo divided by blerp sparki.updateLCD(); delay(1000); // wait 0.1 seconds (100 milliseconds) } |
Shorthand Math with Integers
Programmers are lazy so there are ways to do addition and subtraction operations that look a little different because they take less time to type but do the same thing as math commands that take longer to type. Below are examples of shorthand addition followed by the same code, only written out longhand. For adding to a variable programmers will often use this command-
1 2 3 |
blerg += 6; //used to add six to the variable blerg //this is the same as blerg = blerg + 6; //longhand version |
1 2 3 |
flerm -= 6; //used to subtract six from the variable flerm //this is the same as flerm = flerm - 6; //longhand version |
1 2 3 |
flazzle ++; //used to add one to the variable flazzle //this is the same as flazzle = flazzle + 1; //longhand version |
1 2 3 |
flazzle --; //used to subtract one from the variable flazzle //this is the same as flazzle = flazzle - 1; //longhand version |
Creating a Random Integer
(Feel free to skip this if you’re new to coding and a little overwhelmed.) Before we leave integers let’s talk about a neat little command that allows you to ask Sparki to make a random integer for you. (It’s not really random, but it’s pretty close.) This is useful for making Sparki act a little less like a robot or for choosing variables values that you don’t really care about too much. The command you will use is random( ). We’ll need to do a couple more advanced things to make this command work, first we need to create a variable to hold this random value and then we need to use the random( ) command with one or two numbers inside the parentheses. These numbers will control the highest and lowest possible number that our random command can create. Below is an example of the two ways you can use this command for creating a random number (you’ll need to include the sparki library and add a setup function to make this code work on Sparki):
1 2 3 4 5 6 7 |
int rando; // create an empty integer variable named rando void loop() { rando = random(100); // stores random number between 0 & 99 in rando rando = random(-15, 30); // stores random number between -15 & 29 } |