Craig Evans
/
1620_App_Board_Buttons
Button example.
main.cpp@2:8211254a87fd, 2018-01-10 (annotated)
- Committer:
- eencae
- Date:
- Wed Jan 10 14:22:54 2018 +0000
- Revision:
- 2:8211254a87fd
- Parent:
- 1:f650db6c33e4
- Child:
- 3:cb287ef68787
Buttons A,B,C now controls RGB LED.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:2f4ee2a22324 | 1 | #include "mbed.h" |
eencae | 0:2f4ee2a22324 | 2 | |
eencae | 0:2f4ee2a22324 | 3 | DigitalIn button_A(p29); |
eencae | 0:2f4ee2a22324 | 4 | DigitalIn button_B(p28); |
eencae | 0:2f4ee2a22324 | 5 | DigitalIn button_C(p27); |
eencae | 0:2f4ee2a22324 | 6 | DigitalIn button_D(p26); |
eencae | 0:2f4ee2a22324 | 7 | |
eencae | 2:8211254a87fd | 8 | DigitalOut red_led(p24); |
eencae | 2:8211254a87fd | 9 | DigitalOut green_led(p23); |
eencae | 2:8211254a87fd | 10 | DigitalOut blue_led(p22); |
eencae | 2:8211254a87fd | 11 | |
eencae | 1:f650db6c33e4 | 12 | void init_buttons(); |
eencae | 1:f650db6c33e4 | 13 | |
eencae | 1:f650db6c33e4 | 14 | int main() |
eencae | 1:f650db6c33e4 | 15 | { |
eencae | 1:f650db6c33e4 | 16 | init_buttons(); // turn off internal pull-up/pull-down resistors |
eencae | 1:f650db6c33e4 | 17 | |
eencae | 0:2f4ee2a22324 | 18 | while(1) { |
eencae | 1:f650db6c33e4 | 19 | |
eencae | 2:8211254a87fd | 20 | // check if button A pressed |
eencae | 1:f650db6c33e4 | 21 | if ( button_A.read() == 1) { |
eencae | 2:8211254a87fd | 22 | |
eencae | 2:8211254a87fd | 23 | // writing a 1 turns the LED off, 0 makes it turn on (active-low) |
eencae | 2:8211254a87fd | 24 | red_led.write(0); // if it is, turn the red LED on |
eencae | 2:8211254a87fd | 25 | } else { |
eencae | 2:8211254a87fd | 26 | red_led.write(1); // if it isn't, turn the red LED on |
eencae | 0:2f4ee2a22324 | 27 | } |
eencae | 2:8211254a87fd | 28 | |
eencae | 2:8211254a87fd | 29 | // check if button B pressed |
eencae | 2:8211254a87fd | 30 | if ( button_B.read() == 1) { |
eencae | 2:8211254a87fd | 31 | |
eencae | 2:8211254a87fd | 32 | // writing a 1 turns the LED off, 0 makes it turn on (active-low) |
eencae | 2:8211254a87fd | 33 | green_led.write(0); // if it is, turn the red LED on |
eencae | 2:8211254a87fd | 34 | } else { |
eencae | 2:8211254a87fd | 35 | green_led.write(1); // if it isn't, turn the red LED on |
eencae | 0:2f4ee2a22324 | 36 | } |
eencae | 2:8211254a87fd | 37 | |
eencae | 2:8211254a87fd | 38 | // check if button C pressed |
eencae | 2:8211254a87fd | 39 | if ( button_C.read() == 1) { |
eencae | 2:8211254a87fd | 40 | |
eencae | 2:8211254a87fd | 41 | // writing a 1 turns the LED off, 0 makes it turn on (active-low) |
eencae | 2:8211254a87fd | 42 | blue_led.write(0); // if it is, turn the red LED on |
eencae | 2:8211254a87fd | 43 | } else { |
eencae | 2:8211254a87fd | 44 | blue_led.write(1); // if it isn't, turn the red LED on |
eencae | 0:2f4ee2a22324 | 45 | } |
eencae | 1:f650db6c33e4 | 46 | |
eencae | 2:8211254a87fd | 47 | |
eencae | 1:f650db6c33e4 | 48 | wait(0.1); // small delay |
eencae | 1:f650db6c33e4 | 49 | |
eencae | 0:2f4ee2a22324 | 50 | } |
eencae | 0:2f4ee2a22324 | 51 | } |
eencae | 1:f650db6c33e4 | 52 | |
eencae | 1:f650db6c33e4 | 53 | void init_buttons() |
eencae | 1:f650db6c33e4 | 54 | { |
eencae | 1:f650db6c33e4 | 55 | // PCB has external pull-down resistors so turn the internal ones off |
eencae | 1:f650db6c33e4 | 56 | // (default for DigitalIn) |
eencae | 1:f650db6c33e4 | 57 | button_A.mode(PullNone); |
eencae | 1:f650db6c33e4 | 58 | button_B.mode(PullNone); |
eencae | 1:f650db6c33e4 | 59 | button_C.mode(PullNone); |
eencae | 1:f650db6c33e4 | 60 | button_D.mode(PullNone); |
eencae | 1:f650db6c33e4 | 61 | } |