Button example.

Dependencies:   mbed

Committer:
eencae
Date:
Wed Jan 10 14:27:38 2018 +0000
Revision:
3:cb287ef68787
Parent:
2:8211254a87fd
Child:
4:1676578fd4cd
Added in LED initialisation

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
eencae 2:8211254a87fd 25 // writing a 1 turns the LED off, 0 makes it turn on (active-low)
eencae 2:8211254a87fd 26 red_led.write(0); // if it is, turn the red LED on
eencae 2:8211254a87fd 27 } else {
eencae 2:8211254a87fd 28 red_led.write(1); // if it isn't, turn the red LED on
eencae 0:2f4ee2a22324 29 }
eencae 2:8211254a87fd 30
eencae 2:8211254a87fd 31 // check if button B pressed
eencae 2:8211254a87fd 32 if ( button_B.read() == 1) {
eencae 2:8211254a87fd 33
eencae 2:8211254a87fd 34 // writing a 1 turns the LED off, 0 makes it turn on (active-low)
eencae 2:8211254a87fd 35 green_led.write(0); // if it is, turn the red LED on
eencae 2:8211254a87fd 36 } else {
eencae 2:8211254a87fd 37 green_led.write(1); // if it isn't, turn the red LED on
eencae 0:2f4ee2a22324 38 }
eencae 2:8211254a87fd 39
eencae 2:8211254a87fd 40 // check if button C pressed
eencae 2:8211254a87fd 41 if ( button_C.read() == 1) {
eencae 2:8211254a87fd 42
eencae 2:8211254a87fd 43 // writing a 1 turns the LED off, 0 makes it turn on (active-low)
eencae 2:8211254a87fd 44 blue_led.write(0); // if it is, turn the red LED on
eencae 2:8211254a87fd 45 } else {
eencae 2:8211254a87fd 46 blue_led.write(1); // if it isn't, turn the red LED on
eencae 0:2f4ee2a22324 47 }
eencae 1:f650db6c33e4 48
eencae 2:8211254a87fd 49
eencae 1:f650db6c33e4 50 wait(0.1); // small delay
eencae 1:f650db6c33e4 51
eencae 0:2f4ee2a22324 52 }
eencae 0:2f4ee2a22324 53 }
eencae 1:f650db6c33e4 54
eencae 1:f650db6c33e4 55 void init_buttons()
eencae 1:f650db6c33e4 56 {
eencae 1:f650db6c33e4 57 // PCB has external pull-down resistors so turn the internal ones off
eencae 1:f650db6c33e4 58 // (default for DigitalIn)
eencae 1:f650db6c33e4 59 button_A.mode(PullNone);
eencae 1:f650db6c33e4 60 button_B.mode(PullNone);
eencae 1:f650db6c33e4 61 button_C.mode(PullNone);
eencae 1:f650db6c33e4 62 button_D.mode(PullNone);
eencae 1:f650db6c33e4 63 }
eencae 3:cb287ef68787 64
eencae 3:cb287ef68787 65 void init_leds()
eencae 3:cb287ef68787 66 {
eencae 3:cb287ef68787 67 // LEDs are common anode (active-low) so writing a 1 will turn them off
eencae 3:cb287ef68787 68 red_led.write(1);
eencae 3:cb287ef68787 69 green_led.write(1);
eencae 3:cb287ef68787 70 blue_led.write(1);
eencae 3:cb287ef68787 71
eencae 3:cb287ef68787 72 // this syntax is equivalent
eencae 3:cb287ef68787 73 //red_led = 1;
eencae 3:cb287ef68787 74 //green_led = 1;
eencae 3:cb287ef68787 75 //blue_led = 1;
eencae 3:cb287ef68787 76 }
eencae 3:cb287ef68787 77