Read button to toggle LED
main.cpp@0:131ad5c8b45a, 2014-02-07 (annotated)
- Committer:
- salatron
- Date:
- Fri Feb 07 10:17:57 2014 +0000
- Revision:
- 0:131ad5c8b45a
Read Button
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
salatron | 0:131ad5c8b45a | 1 | /******************************************************************************* |
salatron | 0:131ad5c8b45a | 2 | * This program demonstrates how to read a switch and toggle a LED. * |
salatron | 0:131ad5c8b45a | 3 | * Connect LED and resistor between to DIP36 (P0_21) and DIP1 (0V). * |
salatron | 0:131ad5c8b45a | 4 | * Connect switch between DIP5 (P0_9) and DIP1 (0V). * |
salatron | 0:131ad5c8b45a | 5 | * * |
salatron | 0:131ad5c8b45a | 6 | * Jon Fuge * |
salatron | 0:131ad5c8b45a | 7 | * V1.0 12/11/2013 First issue of code * |
salatron | 0:131ad5c8b45a | 8 | *******************************************************************************/ |
salatron | 0:131ad5c8b45a | 9 | |
salatron | 0:131ad5c8b45a | 10 | #include "mbed.h" // has prototypes for DigitalIn and DigitalOut |
salatron | 0:131ad5c8b45a | 11 | |
salatron | 0:131ad5c8b45a | 12 | DigitalIn mybutton(P0_9); // mybutton reads bit 9 of port 0 (DIP5). |
salatron | 0:131ad5c8b45a | 13 | DigitalOut myled(P0_21); // myled controls bit 21 of port 0 (DIP36). |
salatron | 0:131ad5c8b45a | 14 | |
salatron | 0:131ad5c8b45a | 15 | int main() { |
salatron | 0:131ad5c8b45a | 16 | mybutton.mode(PullUp); // Configure pin to be a pull‐up input |
salatron | 0:131ad5c8b45a | 17 | for(;;) { //Create infinite loop to keep program running. |
salatron | 0:131ad5c8b45a | 18 | if (mybutton == 0) { // if mybutton = 0, then it has been pressed |
salatron | 0:131ad5c8b45a | 19 | myled = !myled; // myled will now become !myled (! means NOT). |
salatron | 0:131ad5c8b45a | 20 | wait(0.1); // Wait for 0.1 seconds to "debounce" the switch. |
salatron | 0:131ad5c8b45a | 21 | while (mybutton == 0) {} // Wait for my button to be released. |
salatron | 0:131ad5c8b45a | 22 | } // End of if (mybutton == 0) |
salatron | 0:131ad5c8b45a | 23 | } // End of for(;;) |
salatron | 0:131ad5c8b45a | 24 | } // end of int main() |