03-01 WhileLoop
From Manuals
(Difference between revisions)
(Created page with '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. [[Image:BAM210_WhileLoop.jpeg|ce…') |
|||
| (4 intermediate revisions not shown) | |||
| Line 1: | Line 1: | ||
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. | 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. | ||
| + | |||
| + | The LEDs for the MBED-SDK and the Bambino line of microcontrollers are mapped to the following LEDs: | ||
| + | {| border="1" cellpadding="5" cellspacing="2" align="center" style="text-align: center;" | ||
| + | |+ align="bottom"|'''LED Colors''' | ||
| + | |'''LED''' | ||
| + | |'''PIN NAME''' | ||
| + | |'''BAM210E''' | ||
| + | |'''BAM210''' | ||
| + | |'''BAM200E''' | ||
| + | |'''BAM200''' | ||
| + | |- | ||
| + | |LED1 | ||
| + | |P6_11 | ||
| + | |Yellow LED | ||
| + | |Yellow LED | ||
| + | |Yellow LED | ||
| + | |Yellow LED | ||
| + | |- | ||
| + | |LED2 | ||
| + | |P2_5 | ||
| + | |Green LED | ||
| + | |Green LED | ||
| + | |Green LED | ||
| + | |Green LED | ||
| + | |- | ||
| + | |LED3 | ||
| + | |P6_1 | ||
| + | |RED LED | ||
| + | |RED LED | ||
| + | |n/p | ||
| + | |n/p | ||
| + | |- | ||
| + | |LED4 | ||
| + | |P6_2 | ||
| + | |Blue LED | ||
| + | |Blue LED | ||
| + | |n/p | ||
| + | |n/p | ||
| + | |- | ||
| + | |} | ||
| + | |||
| + | /*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 | ||
| + | |||
[[Image:BAM210_WhileLoop.jpeg|center|]] | [[Image:BAM210_WhileLoop.jpeg|center|]] | ||
Current revision as of 17:30, 17 September 2014
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.
The LEDs for the MBED-SDK and the Bambino line of microcontrollers are mapped to the following LEDs:
| LED | PIN NAME | BAM210E | BAM210 | BAM200E | BAM200 |
| LED1 | P6_11 | Yellow LED | Yellow LED | Yellow LED | Yellow LED |
| LED2 | P2_5 | Green LED | Green LED | Green LED | Green LED |
| LED3 | P6_1 | RED LED | RED LED | n/p | n/p |
| LED4 | P6_2 | Blue LED | Blue LED | n/p | n/p |
/*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

