Read button to toggle LED
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /******************************************************************************* 00002 * This program demonstrates how to read a switch and toggle a LED. * 00003 * Connect LED and resistor between to DIP36 (P0_21) and DIP1 (0V). * 00004 * Connect switch between DIP5 (P0_9) and DIP1 (0V). * 00005 * * 00006 * Jon Fuge * 00007 * V1.0 12/11/2013 First issue of code * 00008 *******************************************************************************/ 00009 00010 #include "mbed.h" // has prototypes for DigitalIn and DigitalOut 00011 00012 DigitalIn mybutton(P0_9); // mybutton reads bit 9 of port 0 (DIP5). 00013 DigitalOut myled(P0_21); // myled controls bit 21 of port 0 (DIP36). 00014 00015 int main() { 00016 mybutton.mode(PullUp); // Configure pin to be a pull‐up input 00017 for(;;) { //Create infinite loop to keep program running. 00018 if (mybutton == 0) { // if mybutton = 0, then it has been pressed 00019 myled = !myled; // myled will now become !myled (! means NOT). 00020 wait(0.1); // Wait for 0.1 seconds to "debounce" the switch. 00021 while (mybutton == 0) {} // Wait for my button to be released. 00022 } // End of if (mybutton == 0) 00023 } // End of for(;;) 00024 } // end of int main()
Generated on Wed Jul 27 2022 03:27:21 by 1.7.2