![](/media/cache/profiles/a36470cf1f8e5aaf38466d65bd080699.jpg.50x50_q85.jpg)
RGB LED example using BusOut
Fork of 1620_App_Board_RGB_GPIO by
Diff: main.cpp
- Revision:
- 1:11303019663d
- Parent:
- 0:2f4ee2a22324
- Child:
- 2:12e0dd6bced5
--- a/main.cpp Fri Feb 17 11:13:02 2017 +0000 +++ b/main.cpp Fri Feb 17 11:41:13 2017 +0000 @@ -1,6 +1,6 @@ /* ELEC1620 Application Board Example -Buttons +RGB LED (c) Dr Craig A. Evans, University of Leeds, Feb 2017 @@ -8,37 +8,47 @@ #include "mbed.h" -DigitalIn button_A(p29); -DigitalIn button_B(p28); -DigitalIn button_C(p27); -DigitalIn button_D(p26); +DigitalOut red_led(p24); +DigitalOut green_led(p23); +DigitalOut blue_led(p22); + +void init_leds(); -int main() { - +int main() +{ + + init_leds(); + 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(); + + // writing a 1 turns the LED off, 0 makes it turn on - // check if pressed (value will be 1 i.e. true) and print message - if (button_A_value) { - printf("Button A is pressed\n"); - } - if (button_B_value) { - printf("Button B is pressed\n"); - } - if (button_C_value) { - printf("Button C is pressed\n"); - } - if (button_D_value) { - printf("Button D is pressed\n"); - } + blue_led.write(1); // blue off + red_led.write(0); // red on + wait(0.5); - wait(0.5); // small delay - won't be able to read button during delay + red_led.write(1); // red off + green_led.write(0); // green on + wait(0.5); + green_led.write(1); // green off + blue_led.write(0); // blue on + wait(0.5); + + } } + +void init_leds() +{ + // LEDs are common anode (active-low) so writing a 1 will turn them off + red_led.write(1); + green_led.write(1); + blue_led.write(1); + + // this syntax is equivalent + //red_led = 1; + //green_led = 1; + //blue_led = 1; +} +