qwerty

Dependencies:   mbed

Fork of 1620_App_Board_Buttons by Craig Evans

Committer:
el17ts
Date:
Sun Apr 22 08:53:30 2018 +0000
Revision:
4:bb53d2d88f12
Parent:
3:cb287ef68787
qwertyuiop

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 }
el17ts 4:bb53d2d88f12 48
el17ts 4:bb53d2d88f12 49 if (button_D.read() == 1) {
el17ts 4:bb53d2d88f12 50 red_led.write(0) && green_led.write(0);
el17ts 4:bb53d2d88f12 51 } else {
el17ts 4:bb53d2d88f12 52 red_led.write(1) && green_led.write(1);
el17ts 4:bb53d2d88f12 53 }
eencae 1:f650db6c33e4 54
eencae 2:8211254a87fd 55
eencae 1:f650db6c33e4 56 wait(0.1); // small delay
eencae 1:f650db6c33e4 57
eencae 0:2f4ee2a22324 58 }
eencae 0:2f4ee2a22324 59 }
eencae 1:f650db6c33e4 60
eencae 1:f650db6c33e4 61 void init_buttons()
eencae 1:f650db6c33e4 62 {
eencae 1:f650db6c33e4 63 // PCB has external pull-down resistors so turn the internal ones off
eencae 1:f650db6c33e4 64 // (default for DigitalIn)
eencae 1:f650db6c33e4 65 button_A.mode(PullNone);
eencae 1:f650db6c33e4 66 button_B.mode(PullNone);
eencae 1:f650db6c33e4 67 button_C.mode(PullNone);
eencae 1:f650db6c33e4 68 button_D.mode(PullNone);
eencae 1:f650db6c33e4 69 }
eencae 3:cb287ef68787 70
eencae 3:cb287ef68787 71 void init_leds()
eencae 3:cb287ef68787 72 {
eencae 3:cb287ef68787 73 // LEDs are common anode (active-low) so writing a 1 will turn them off
eencae 3:cb287ef68787 74 red_led.write(1);
eencae 3:cb287ef68787 75 green_led.write(1);
eencae 3:cb287ef68787 76 blue_led.write(1);
eencae 3:cb287ef68787 77
eencae 3:cb287ef68787 78 // this syntax is equivalent
eencae 3:cb287ef68787 79 //red_led = 1;
eencae 3:cb287ef68787 80 //green_led = 1;
eencae 3:cb287ef68787 81 //blue_led = 1;
eencae 3:cb287ef68787 82 }
eencae 3:cb287ef68787 83