Eray Ozturk / Mbed 2 deprecated QuadBrightness

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     A featured example for beginners
00003 
00004     Control brightnesses of all 4 LEDs on the board using +/- keys for increment or decrement, 1/2/3/4 keys to select LED through your computer
00005 
00006     Eray Ozturk | erayozturk1@gmail.com
00007 */
00008 
00009 #include "mbed.h"
00010 
00011 // The mbed board can communicate with a host PC through a "USB Virtual Serial Port" over the same USB cable that is used for programming.
00012 // If you use Windows, you need a driver. http://mbed.org/media/downloads/drivers/mbedWinSerial_16466.exe
00013 // With your mbed plugged in, and no explorer drive windows open, run the installer.
00014 // The default baud rate is 9600
00015 Serial pc(USBTX, USBRX); // tx, rx
00016 
00017 Ticker flipper;
00018 PwmOut led1(LED1);
00019 PwmOut led2(LED2);
00020 PwmOut led3(LED3);
00021 PwmOut led4(LED4);
00022 
00023 #define PWM_MAX 0.5
00024 
00025 float brightness = 0.0;
00026 float brigh_bckp;
00027 
00028 int SelectedLED = 1;
00029 int Selec_bckp;
00030 
00031 void callback() {
00032     if (brightness>PWM_MAX) // limiter
00033         brightness = PWM_MAX;
00034 
00035     if (brightness<0.0)     // limiter
00036         brightness = 0.0;
00037 
00038     if (Selec_bckp!=SelectedLED || brigh_bckp!=brightness) {
00039         pc.printf("*Led%u : %5.1f%%\n",SelectedLED,brightness*100.0/PWM_MAX);
00040         Selec_bckp = SelectedLED;
00041         brigh_bckp = brightness;
00042     }
00043 }
00044 
00045 void SetBright() {
00046     switch (SelectedLED) {
00047         case 1:
00048             led1 = brightness;
00049             break;
00050         case 2:
00051             led2 = brightness;
00052             break;
00053         case 3:
00054             led3 = brightness;
00055             break;
00056         case 4:
00057             led4 = brightness;
00058             break;
00059         default:
00060             SelectedLED = 1;
00061     }
00062 }
00063 
00064 int main() {
00065     pc.printf("\n## Quad Brightness Controller ##\n");
00066     pc.printf("Press '+/-' to change selected LED's brightness\n");
00067     pc.printf("Use '1'-'4' keys to change selected LED\n");
00068 
00069     flipper.attach(&callback, 1.0); // call flip() in every 1 sec
00070 
00071     while (true) {
00072         if (pc.readable()) {
00073             char c = pc.getc();
00074 
00075             if ((c == '+') && (brightness < PWM_MAX)) {
00076                 brightness += 0.01;
00077                 SetBright();
00078             } else if ((c == '-') && (brightness > 0.0)) {
00079                 brightness -= 0.01;
00080                 SetBright();
00081             } else if (c == '1') {
00082                 pc.printf("Led1 selected\n");
00083                 SelectedLED = 1;
00084                 brightness = 0.0;
00085             } else if (c == '2') {
00086                 pc.printf("Led2 selected\n");
00087                 SelectedLED = 2;
00088                 brightness = 0.0;
00089             } else if (c == '3') {
00090                 pc.printf("Led3 selected\n");
00091                 SelectedLED = 3;
00092                 brightness = 0.0;
00093             } else if (c == '4') {
00094                 pc.printf("Led4 selected\n");
00095                 SelectedLED = 4;
00096                 brightness = 0.0;
00097             } else if (c == 'a') {
00098                 pc.printf("Author: Eray Ozturk\n");
00099             }
00100         }
00101     }
00102 }