03-03 IfElseSwitch
From Manuals
(Difference between revisions)
| Line 1: | Line 1: | ||
Flashes one of two LEDs, depending on the state of a 2-way switch. The LEDs are connected to D0 and D1 of J9. The switch is connected to D2 of J9. Please see schematic below for further details. | Flashes one of two LEDs, depending on the state of a 2-way switch. The LEDs are connected to D0 and D1 of J9. The switch is connected to D2 of J9. Please see schematic below for further details. | ||
| + | |||
| + | [[Image:BAM210_03_03_SCH.png|center|alt=BAM210|frame|<div align="center">'''Schematic for example 03-03''']] | ||
| + | |||
| + | [[Image:BAM210_IfElse.jpeg|center|]] | ||
/*Program Example 3.3: Flashes one of two LEDs, depending on the state of a 2-way switch | /*Program Example 3.3: Flashes one of two LEDs, depending on the state of a 2-way switch | ||
| Line 28: | Line 32: | ||
} //end of while(1) | } //end of while(1) | ||
} | } | ||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
Revision as of 15:49, 17 September 2014
Flashes one of two LEDs, depending on the state of a 2-way switch. The LEDs are connected to D0 and D1 of J9. The switch is connected to D2 of J9. Please see schematic below for further details.
/*Program Example 3.3: Flashes one of two LEDs, depending on the state of a 2-way switch
*/
#include "mbed.h"
DigitalOut redled(D0);
DigitalOut greenled(D1);
DigitalIn switchinput(D2);
int main()
{
while(1) {
if (switchinput==1) { //test value of switchinput
//execute following block if switchinput is 1
greenled = 0; //green led is off
redled = 1; // flash red led
wait(0.2);
redled = 0;
wait(0.2);
} //end of if
else { //here if switchinput is 0
redled = 0; //red led is off
greenled = 1; // flash green led
wait(0.2);
greenled = 0;
wait(0.2);
} //end of else
} //end of while(1)
}

