SEARCH
TOOLBOX
LANGUAGES
modified on 14 August 2014 at 14:36 ••• 23,981 views

03-01 WhileLoop

From Manuals

Revision as of 14:36, 14 August 2014 by Support (Talk | contribs)
Jump to: navigation, search

A very simple example that demonstrates the use of while loops to blink two different User LEDs. LED1 blinks 10 times then LED4 blinks 10 times.

/*Program Example 3.1: Demonstrates use of while loops. No external connection required
                                                                               */
#include "mbed.h"
DigitalOut myled(LED1);
DigitalOut yourled(LED4);
int main()
{
    char i=0;         //declare variable i, and set to 0
    while(1) {        //start endless loop
        while(i<10) {     //start first conditional while loop
            myled = 1;
            wait(0.2);
            myled = 0;
            wait(0.2);
            i = i+1;         //increment i
        }                  //end of first conditional while loop
        while(i>0) {       //start second conditional loop
            yourled = 1;
            wait(0.2);
            yourled = 0;
            wait(0.2);
            i = i-1;
        }
    }                  //end infinite loop block
}                    //end of main