Get Help
Recap of Integer Variables
What We’ve Covered So Far
Declaring an integer variable:
1 |
int distance; // an empty integer variable named distance for use later |
1 |
distance = 15; // distance integer variable changed to fifteen |
Declaring and assigning an integer variable at the same time:
1 |
int distance = 10; // integer variable named distance with a value of ten |
Adding to an integer variable:
1 2 3 |
distance = distance + 15; //or below does the same thing, just in shorthand distance += 15; |
Subtracting from an integer variable:
1 2 3 |
distance = distance - 15; //or below does the same thing, just in shorthand distance -= 15; |
Multiplying an integer variable:
1 |
distance = distance * 5; //multiplies the integer distance by five |
Dividing an integer variable:
Don’t do it! If you wind up with a decimal the computer will just throw it away without tell you. But if you must it looks like this-
1 |
distance = distance / 2; //divides the integer distance by two |
Adding and subtracting just one with an integer variable in shorthand:
1 2 3 |
distance ++; //add one to distance distance --; //subtract one from distance |
Creating a random integer value:
1 2 |
rando = random(100); // stores random number between 0 & 99 in rando rando = random(-15, 30); // stores random number between -15 & 29 |
Questions and Activities to Try
1. Declare three different integer variables and use them to control the forward movement of Sparki using the sparki.moveForward() command. Here’s a little help to get you started-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <Sparki.h> // include the sparki library // declare three integer variables here void setup() { } void loop() { sparki.moveForward( place variable 1 name here ); delay(1000); // this makes Sparki wait for one second so you // can tell the difference between the variables sparki.moveForward( place variable 2 name here); delay(1000); sparki.moveForward( place variable 3 name here); delau(1000); } |
2. Using the code you create above to make Sparki move three different distances add some addition and multiplication commands so that Sparki continues to move three different distances, but two of the distances increase each time Sparki executes the loop function code. For example Sparki might move forward three centimeters, wait one second, move forward two centimeters, wait one second, move forward eight centimeters and then wait again one second before the loop function starts over again. The second time through Sparki might move forward three centimeters, wait one second, move forward four centimeters (because this variable is being multiplied by two), wait one second, move forward ten centimeters (because this variable gets two added to it) and then waits a second before starting loop a third time.
3. Add a variable and commands to the code above that causes Sparki to wait a different amount of time between movement commands. Once you’re successful doing that try changing your code a little bit more so that, while Sparki waits a different amount of time between each movement command, the first time Sparki waits at least one second, the second time at least two seconds and the before the third movement command Sparki waits at least three seconds. (Hint, there are actually two different ways to do this last particular challenge.)
Next Step-
Now that you feel a little more comfortable with integer variables let’s move on to decimal points with floating point numbers….