The Japanino, Loop Control

Next, looping.  There are a variety of ways to control the flow of a program.  Check out the Arduino reference page for the full list and more complete descriptions.  The ones I normally use are:

    if
    switch-case
    for
    while


The if-statement:

    if( test ) {
       commands
    }
    else {
       more_commands
    }

If the "test" portion evaluates to a 1, then the first section of commands will execute.  Otherwise, the more_commands section will execute.  If we combine the if-statement with the previously described shift-right command:

    int ledPin = 13;
    int cntr = 0b0111;

    if( 0b0001 & (cntr >> 2)) {
       digitalWrite(ledPin, HIGH);
    } else {
       digitalWrite(ledPin, LOW);
    }

This will take the value of the variable cntr , shift it right 2 times (same as dividing 7 by 4), and then test if the resulting bit-1 position is a 1.  If so, pin D13 will turn on, otherwise it will turn off.  It's a fairly useless code example.  I'll expand on this later.

--------------

The for-statement:

    for(var = start_value; stop_condition; increment_condition) {
       commands to run
    }

The for-statement is really useful for counting the number of times you perform an action, or to do an action that is related to counting numbers.  We start by initializing the variable we want to use.  Then, we specify the ending condition for the loop.  That is, when the stop_condition is reached the for-statement quits.  Finally, we say how the value of the variable will change.  Often, we just use the "++" operator to add one to the variable.

    void setup() {
       for(int x=7; x<=13; x++) {
          pinMode(x, OUTPUT);
       }
    }

In this example, we start by creating x as an integer and storing the number 7 to it.  Then we drop down to the pinMode function and set pin 7 as OUTPUT .  We return to the top of the for loop and, using x++ , we increment x to 8.  We then go back to the pinMode function and set pin 8 as OUTPUT .  We keep doing this until x reaches 14 and then stop.

We can use the for-statement within the setup() function to intialize all of the pins on the Japanino connected to the P.O.V. LEDs to be set as output pins.

---------------

The while-statement:

    while (test) {
       commands to run
    }

The while statement is kind of like a cross between if() and for(), in that as long as the "test" evaluates to a 1, the while() will continue to loop.  We have to be careful here, because if you forget to change the values of the variables in the "test" the while() loop will never end.

I'll talk more about the millis() command later, but for the moment it's enough to know that millis() returns the number of milliseconds since the program started running.  The below example will cause the D13 pin LED to flash really fast for 1 second before going on to the next part of the program.


    unsigned long start_time;
    boolean display_value = HIGH;

    start_time = millis();

    while ( millis() - start_time <= 1000) {
       display_value != display_value;
       digitalWrite(ledPin, display_value);
       delay(1);
    }

---------------

The switch-case statement:

    switch(variable) {
       case 0:
             commands
          break;
       case 1:
             commands
          break;
         :
       default:
          commands
             :
}

I'm just going to gloss over this one because you can learn about switch case just as easily on the Arduino site. Basically, we take an integer variable and we use switch() to set up different conditions based on the value of the variable. Say we want to turn on the LEDs of the P.O.V. light stick. There are 7 LEDs, so if our variable took on the values of 0 to 7, we could do something like the following:

    void setup() {
       for(int x=7; x<=13; x++) {
          pinMode(x, OUTPUT);
       }
       }

    void loop() {
       int LED_pntr = 0;
       switch(LED_pntr) {
          case 1:
             digitalWrite(7, HIGH);
             break;
          case 2:
             digitalWrite(8, HIGH);
             break;
             :                 // Just repeat this code for the other conditions
          case 7:
             digitalWrite(13, HIGH);
             break;
       }
    }

This is actually not a great example of how to program the P.O.V., so I'm not going to bother trying to show how to make the code here workable.

------------------

Ok, so now let's put this all together to create a real sketch that makes it look like the LED stick has the light running from one end to the other.

    // Blink, ver. 3

    int ledPin = 7;   // First LED on the P.O.V. is at pin D7.
    int number_of_LEDs = 7;
    unsigned long start_time;

    void setup() {
       for(int x=ledPin; x < ledPin + number_of_LEDs; x++) {
          pinMode(x, OUTPUT);
       }
    }

    void loop() {
       for(int i = 0; i < number_of_LEDs; i++) {
          digitalWrite(ledPin + i, HIGH);
          start_time = millis();

          while ( millis() - start_time <= 100) {
             // do nothing.
          }
       digitalWrite(ledPin + i, LOW);
       }
    }

You may ask why I'm using an empty while-statement instead of delay().  It's because the delay() command causes the Japanino to stop and not do anything else until the delay time is up.  This includes not reading the input pins to see if you've closed a switch for turning the speaker on and off.  We'll be getting to reading input pins soon, so now's as good a time as any to quit using delay();

Return to the main Index
Learning about Tone() and millis()