Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 DigitalIn button_A(p29); 00004 DigitalIn button_B(p28); 00005 DigitalIn button_C(p27); 00006 DigitalIn button_D(p26); 00007 00008 DigitalOut red_led(p24); 00009 DigitalOut green_led(p23); 00010 DigitalOut blue_led(p22); 00011 00012 void init_buttons(); 00013 void init_leds(); 00014 00015 int main() 00016 { 00017 init_buttons(); // turn off internal pull-up/pull-down resistors 00018 init_leds(); // tursn off the LEDs 00019 00020 while(1) { 00021 00022 // check if button A pressed 00023 if ( button_A.read() == 1) { 00024 // writing a 1 turns the LED off, 0 makes it turn on (active-low) 00025 red_led.write(0); // if it is, turn the red LED on 00026 } else if (button_B.read() == 1) { // check if B pressed 00027 green_led.write(0); // if it is, turn the green LED on 00028 } else if ( button_C.read() == 1) { // check if C pressed 00029 blue_led.write(0); // if it is, turn the blue LED on 00030 } else { 00031 // if no buttons pressed, ensure all the LEDs are off 00032 red_led.write(1); 00033 green_led.write(1); 00034 blue_led.write(1); 00035 } 00036 00037 wait(0.1); // small delay 00038 00039 } 00040 } 00041 00042 void init_buttons() 00043 { 00044 // PCB has external pull-down resistors so turn the internal ones off 00045 // (default for DigitalIn) 00046 button_A.mode(PullNone); 00047 button_B.mode(PullNone); 00048 button_C.mode(PullNone); 00049 button_D.mode(PullNone); 00050 } 00051 00052 void init_leds() 00053 { 00054 // LEDs are common anode (active-low) so writing a 1 will turn them off 00055 red_led.write(1); 00056 green_led.write(1); 00057 blue_led.write(1); 00058 00059 // this syntax is equivalent 00060 //red_led = 1; 00061 //green_led = 1; 00062 //blue_led = 1; 00063 } 00064
Generated on Tue Jul 19 2022 09:12:28 by
1.7.2