Button example.

Dependencies:   mbed

Committer:
eencae
Date:
Fri Mar 02 13:40:23 2018 +0000
Revision:
4:1676578fd4cd
Parent:
3:cb287ef68787
Child:
5:0143ac4615a0
Re-factored to use if, else if, else.

Who changed what in which revision?

UserRevisionLine numberNew 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 3:cb287ef68787 13 void init_leds();
eencae 1:f650db6c33e4 14
eencae 1:f650db6c33e4 15 int main()
eencae 1:f650db6c33e4 16 {
eencae 1:f650db6c33e4 17 init_buttons(); // turn off internal pull-up/pull-down resistors
eencae 3:cb287ef68787 18 init_leds(); // tursn off the LEDs
eencae 1:f650db6c33e4 19
eencae 0:2f4ee2a22324 20 while(1) {
eencae 1:f650db6c33e4 21
eencae 2:8211254a87fd 22 // check if button A pressed
eencae 1:f650db6c33e4 23 if ( button_A.read() == 1) {
eencae 2:8211254a87fd 24 // writing a 1 turns the LED off, 0 makes it turn on (active-low)
eencae 2:8211254a87fd 25 red_led.write(0); // if it is, turn the red LED on
eencae 4:1676578fd4cd 26 } else if (button_B.read() == 1) { // check if B pressed
eencae 4:1676578fd4cd 27 green_led.write(0); // if it is, turn the green LED on
eencae 4:1676578fd4cd 28 } else if ( button_C.read() == 1) { // check if C pressed
eencae 4:1676578fd4cd 29 blue_led.write(0); // if it is, turn the blue LED on
eencae 2:8211254a87fd 30 } else {
eencae 4:1676578fd4cd 31 // if no buttons pressed, ensure all the LEDs are off
eencae 4:1676578fd4cd 32 red_led.write(1);
eencae 4:1676578fd4cd 33 green_led.write(1);
eencae 4:1676578fd4cd 34 blue_led.write(1);
eencae 0:2f4ee2a22324 35 }
eencae 4:1676578fd4cd 36
eencae 4:1676578fd4cd 37 wait(0.1); // small delay
eencae 2:8211254a87fd 38
eencae 0:2f4ee2a22324 39 }
eencae 1:f650db6c33e4 40
eencae 1:f650db6c33e4 41 void init_buttons()
eencae 1:f650db6c33e4 42 {
eencae 1:f650db6c33e4 43 // PCB has external pull-down resistors so turn the internal ones off
eencae 1:f650db6c33e4 44 // (default for DigitalIn)
eencae 1:f650db6c33e4 45 button_A.mode(PullNone);
eencae 1:f650db6c33e4 46 button_B.mode(PullNone);
eencae 1:f650db6c33e4 47 button_C.mode(PullNone);
eencae 1:f650db6c33e4 48 button_D.mode(PullNone);
eencae 1:f650db6c33e4 49 }
eencae 3:cb287ef68787 50
eencae 3:cb287ef68787 51 void init_leds()
eencae 3:cb287ef68787 52 {
eencae 3:cb287ef68787 53 // LEDs are common anode (active-low) so writing a 1 will turn them off
eencae 3:cb287ef68787 54 red_led.write(1);
eencae 3:cb287ef68787 55 green_led.write(1);
eencae 3:cb287ef68787 56 blue_led.write(1);
eencae 3:cb287ef68787 57
eencae 3:cb287ef68787 58 // this syntax is equivalent
eencae 3:cb287ef68787 59 //red_led = 1;
eencae 3:cb287ef68787 60 //green_led = 1;
eencae 3:cb287ef68787 61 //blue_led = 1;
eencae 3:cb287ef68787 62 }
eencae 3:cb287ef68787 63