Variables

A variable is a placeholder that we can assign a value to later, and can also be used to throughout a program to limit redundant logic and add advanced logic. For example, you could create a condition where the program loops until myVar == 1, then add myVar = 1 to an onCollision event so that a collision forces the loop to exit. Below are a few basic methods to use variables, but you are not limited to these.

Define

variableTime

  1. Must begin with either a letter or an underscore and can contain letters, digits, and the _ character.
  2. Upper and lowercase letters are distinct because Oval is case-sensitive.
  3. A variable declared outside of any function is global. It can be accessed from the main loop or from any function defined afterward. For simplicity, it can be helpful to define variables before the main loop, as we have done in the examples below.
  4. A float value is any number with a decimal.
  5. An int value is an integer; a whole number with no decimal places. These are not available in the block canvas.
  6. More than one variable can be defined on a line using comma separation, or they can be defined on many lines using semi colons separation like this:
float time = 3.55, float repeat = 6.1;
int predict = 2;

void StartProgram()
{
    //code
}

Call

To use a variable you need to call it from the main loop. For example, to write a program where the robot draws 3 sqaures, where each side length increases by 15% using floattime as a variable, use:

float time = 0.5;

void startProgram()
{
    int _loopCount_1 = 0;
    while (_loopCount_1 < 12)
    {
        _loopCount_1 = _loopCount_1 + 1;
        controlSystemTargetSpeed = 40;
        setRgbLed(nextRandomFloat * 255, nextRandomFloat * 255, nextRandomFloat * 255);
        controlSystemTargetYaw = (controlSystemTargetYaw + 90);
        delay(time);
        controlSystemTargetSpeed = 0;
        delay(0.5);
        time = (time * 1.15);
    }
    controlSystemTargetSpeed = 0;
}

Count

It's useful to set a variable value in the main loop to use as a counter based on a condition. For example, let's extend our growing square program to report the number of squares drawn. The LED's can be set to blink for the number of squares drawn after the program ends. We can use a second variable named floatsquareCount and add it to the program:

float time = 0.5;
float squareCount = 0;

void startProgram()
{
    int _loopCount_1 = 0;
    while (_loopCount_1 < 12)
    {
        _loopCount_1 = _loopCount_1 + 1;
        controlSystemTargetSpeed = 40;
        setRgbLed(nextRandomFloat * 255, nextRandomFloat * 255, nextRandomFloat * 255);
        controlSystemTargetYaw = (controlSystemTargetYaw + 90);
        delay(time);
        controlSystemTargetSpeed = 0;
        delay(0.5);
        time = (time * 1.15);
        squareCount = (squareCount + 1);
    }
    controlSystemTargetSpeed = 0;
    int _loopCount_2 = 0;
    while (_loopCount_2 < (squareCount / 4))
//divide by 4 becuase to report the number of squares, becuase there are 4 sides in a square
    {
        _loopCount_2 = _loopCount_2 + 1;
        playSound(0, 7);
        delay(0.05);
        setRgbLed(255, 255, 255);
        delay(1.2);
        setRgbLed(0, 0, 0);
        delay(0.2);
  }
}

Compare

You can also compare two variable values to create conditional logic. The Hot Potato game from the "Blocks 3: Variables" Activity is a great example of using a variable as a counter and comparing it to another variable. In this case the float toss variable increases each time you toss the robot, and eventually exceeds the value for float expire, to create the Hot Potato game effect of expiration:

float toss = 0;
float expire = 0;

void startProgram()
{
  controlSystemIsOn = false;
  while (1)
  {
    expire = ((nextRandomFloat * 8 - 2) + 2);
    while (not((toss > expire)))
    {
      setRgbLed(0, 255, 0);
      if ((( ( ( accelSensorXRight ^ 2 ) + ( accelSensorYForward ^ 2 ) + ( accelSensorZUp ^ 2 ) ) ^ 0.5 ) > 4))
      {
        playSound(0, 7);
        delay(0.05);
        setRgbLed(255, 255, 255);
        delay(0.5);
        toss = (toss + 1);
      }
    }
    delay(0.7);
    playSound(1, 10);
    delay(0.05);
    setRgbLed(255, 0, 18);
    delay(2);
    toss = 0;
  }
}

There are a lot more uses for variables beyond these. Do you have a new-use case you want us to document? Click the pencil button at the top of the page to let us know.