Temi Solesi
/
1620_App_Board_Buttons
qwerty
Fork of 1620_App_Board_Buttons by
Diff: main.cpp
- Revision:
- 1:f650db6c33e4
- Parent:
- 0:2f4ee2a22324
- Child:
- 2:8211254a87fd
--- a/main.cpp Fri Feb 17 11:13:02 2017 +0000 +++ b/main.cpp Wed Feb 22 14:06:34 2017 +0000 @@ -1,11 +1,3 @@ -/* ELEC1620 Application Board Example - -Buttons - -(c) Dr Craig A. Evans, University of Leeds, Feb 2017 - -*/ - #include "mbed.h" DigitalIn button_A(p29); @@ -13,32 +5,39 @@ DigitalIn button_C(p27); DigitalIn button_D(p26); -int main() { - +void init_buttons(); + +int main() +{ + init_buttons(); // turn off internal pull-up/pull-down resistors + while(1) { - - // read each of the buttons and store in variable - int button_A_value = button_A.read(); - // int button_A_value = button_A; // this is equivalent - int button_B_value = button_B.read(); - int button_C_value = button_C.read(); - int button_D_value = button_D.read(); - - // check if pressed (value will be 1 i.e. true) and print message - if (button_A_value) { - printf("Button A is pressed\n"); + + // check each button in turn and print message + if ( button_A.read() == 1) { + printf("Button A pressed\n"); + } + if ( button_B.read() == 1) { + printf("Button B pressed\n"); } - if (button_B_value) { - printf("Button B is pressed\n"); - } - if (button_C_value) { - printf("Button C is pressed\n"); + if ( button_C.read() == 1) { + printf("Button C pressed\n"); } - if (button_D_value) { - printf("Button D is pressed\n"); + if ( button_D.read() == 1) { + printf("Button D pressed\n"); } - - wait(0.5); // small delay - won't be able to read button during delay - + + wait(0.1); // small delay + } } + +void init_buttons() +{ + // PCB has external pull-down resistors so turn the internal ones off + // (default for DigitalIn) + button_A.mode(PullNone); + button_B.mode(PullNone); + button_C.mode(PullNone); + button_D.mode(PullNone); +}