![](/media/cache/group/UCL_E.png.50x50_q85.jpg)
Code to demonstrate use of the two buttons to toggle LED's
Dependencies: mbed
Simple code to demonstrate the use of buttons to toggle LEDs on and off.
main.cpp@0:f932320f7606, 2014-12-12 (annotated)
- Committer:
- djmannion
- Date:
- Fri Dec 12 09:01:15 2014 +0000
- Revision:
- 0:f932320f7606
- Child:
- 1:0133472f4825
Button Demo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
djmannion | 0:f932320f7606 | 1 | /* |
djmannion | 0:f932320f7606 | 2 | This code uses the button1 to toggle LED1 and button2 to toggle LED2 |
djmannion | 0:f932320f7606 | 3 | Author: Dan M |
djmannion | 0:f932320f7606 | 4 | Date Created: 12.12.2014 |
djmannion | 0:f932320f7606 | 5 | Last Update: 12.12.2014 |
djmannion | 0:f932320f7606 | 6 | */ |
djmannion | 0:f932320f7606 | 7 | #include "mbed.h" |
djmannion | 0:f932320f7606 | 8 | //Define pin names and numbers ( I know these probably aren't needed but I put them in to demonstrate pin numbers) |
djmannion | 0:f932320f7606 | 9 | #define LED1 p18 |
djmannion | 0:f932320f7606 | 10 | #define LED2 p19 |
djmannion | 0:f932320f7606 | 11 | #define SW1 p16 |
djmannion | 0:f932320f7606 | 12 | #define SW2 p17 |
djmannion | 0:f932320f7606 | 13 | //Instantiate IO objects |
djmannion | 0:f932320f7606 | 14 | DigitalOut led_1(LED1); |
djmannion | 0:f932320f7606 | 15 | DigitalOut led_2(LED2); |
djmannion | 0:f932320f7606 | 16 | DigitalIn sw_1(SW1); |
djmannion | 0:f932320f7606 | 17 | DigitalIn sw_2(SW2); |
djmannion | 0:f932320f7606 | 18 | |
djmannion | 0:f932320f7606 | 19 | int main() { |
djmannion | 0:f932320f7606 | 20 | led_1=0; |
djmannion | 0:f932320f7606 | 21 | led_2=0; |
djmannion | 0:f932320f7606 | 22 | while(1) { |
djmannion | 0:f932320f7606 | 23 | if(!sw_1) |
djmannion | 0:f932320f7606 | 24 | { |
djmannion | 0:f932320f7606 | 25 | led_1=!led_1; |
djmannion | 0:f932320f7606 | 26 | wait(0.25); //Button Debounce ;) |
djmannion | 0:f932320f7606 | 27 | } |
djmannion | 0:f932320f7606 | 28 | if(!sw_2) |
djmannion | 0:f932320f7606 | 29 | { |
djmannion | 0:f932320f7606 | 30 | led_2=!led_2; |
djmannion | 0:f932320f7606 | 31 | wait(0.25); //Button Debounce ;) |
djmannion | 0:f932320f7606 | 32 | } |
djmannion | 0:f932320f7606 | 33 | } |
djmannion | 0:f932320f7606 | 34 | } |