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@2:d955fe04abd9, 2010-10-22 (annotated)
- Committer:
- diffstorm
- Date:
- Fri Oct 22 12:23:34 2010 +0000
- Revision:
- 2:d955fe04abd9
- Parent:
- 1:8012a60a982d
added percentage view
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| diffstorm | 1:8012a60a982d | 1 | /* |
| diffstorm | 1:8012a60a982d | 2 | A featured example for beginners |
| diffstorm | 1:8012a60a982d | 3 | |
| diffstorm | 1:8012a60a982d | 4 | 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 |
| diffstorm | 1:8012a60a982d | 5 | |
| diffstorm | 1:8012a60a982d | 6 | Eray Ozturk | erayozturk1@gmail.com |
| diffstorm | 1:8012a60a982d | 7 | */ |
| diffstorm | 1:8012a60a982d | 8 | |
| diffstorm | 0:655158a7c194 | 9 | #include "mbed.h" |
| diffstorm | 0:655158a7c194 | 10 | |
| diffstorm | 1:8012a60a982d | 11 | // 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. |
| diffstorm | 1:8012a60a982d | 12 | // If you use Windows, you need a driver. http://mbed.org/media/downloads/drivers/mbedWinSerial_16466.exe |
| diffstorm | 1:8012a60a982d | 13 | // With your mbed plugged in, and no explorer drive windows open, run the installer. |
| diffstorm | 1:8012a60a982d | 14 | // The default baud rate is 9600 |
| diffstorm | 0:655158a7c194 | 15 | Serial pc(USBTX, USBRX); // tx, rx |
| diffstorm | 0:655158a7c194 | 16 | |
| diffstorm | 0:655158a7c194 | 17 | Ticker flipper; |
| diffstorm | 0:655158a7c194 | 18 | PwmOut led1(LED1); |
| diffstorm | 0:655158a7c194 | 19 | PwmOut led2(LED2); |
| diffstorm | 0:655158a7c194 | 20 | PwmOut led3(LED3); |
| diffstorm | 0:655158a7c194 | 21 | PwmOut led4(LED4); |
| diffstorm | 0:655158a7c194 | 22 | |
| diffstorm | 2:d955fe04abd9 | 23 | #define PWM_MAX 0.5 |
| diffstorm | 2:d955fe04abd9 | 24 | |
| diffstorm | 0:655158a7c194 | 25 | float brightness = 0.0; |
| diffstorm | 0:655158a7c194 | 26 | float brigh_bckp; |
| diffstorm | 0:655158a7c194 | 27 | |
| diffstorm | 0:655158a7c194 | 28 | int SelectedLED = 1; |
| diffstorm | 0:655158a7c194 | 29 | int Selec_bckp; |
| diffstorm | 0:655158a7c194 | 30 | |
| diffstorm | 1:8012a60a982d | 31 | void callback() { |
| diffstorm | 2:d955fe04abd9 | 32 | if (brightness>PWM_MAX) // limiter |
| diffstorm | 2:d955fe04abd9 | 33 | brightness = PWM_MAX; |
| diffstorm | 2:d955fe04abd9 | 34 | |
| diffstorm | 2:d955fe04abd9 | 35 | if (brightness<0.0) // limiter |
| diffstorm | 2:d955fe04abd9 | 36 | brightness = 0.0; |
| diffstorm | 2:d955fe04abd9 | 37 | |
| diffstorm | 0:655158a7c194 | 38 | if (Selec_bckp!=SelectedLED || brigh_bckp!=brightness) { |
| diffstorm | 2:d955fe04abd9 | 39 | pc.printf("*Led%u : %5.1f%%\n",SelectedLED,brightness*100.0/PWM_MAX); |
| diffstorm | 0:655158a7c194 | 40 | Selec_bckp = SelectedLED; |
| diffstorm | 0:655158a7c194 | 41 | brigh_bckp = brightness; |
| diffstorm | 0:655158a7c194 | 42 | } |
| diffstorm | 0:655158a7c194 | 43 | } |
| diffstorm | 0:655158a7c194 | 44 | |
| diffstorm | 0:655158a7c194 | 45 | void SetBright() { |
| diffstorm | 0:655158a7c194 | 46 | switch (SelectedLED) { |
| diffstorm | 0:655158a7c194 | 47 | case 1: |
| diffstorm | 0:655158a7c194 | 48 | led1 = brightness; |
| diffstorm | 0:655158a7c194 | 49 | break; |
| diffstorm | 0:655158a7c194 | 50 | case 2: |
| diffstorm | 0:655158a7c194 | 51 | led2 = brightness; |
| diffstorm | 0:655158a7c194 | 52 | break; |
| diffstorm | 0:655158a7c194 | 53 | case 3: |
| diffstorm | 0:655158a7c194 | 54 | led3 = brightness; |
| diffstorm | 0:655158a7c194 | 55 | break; |
| diffstorm | 0:655158a7c194 | 56 | case 4: |
| diffstorm | 0:655158a7c194 | 57 | led4 = brightness; |
| diffstorm | 0:655158a7c194 | 58 | break; |
| diffstorm | 0:655158a7c194 | 59 | default: |
| diffstorm | 0:655158a7c194 | 60 | SelectedLED = 1; |
| diffstorm | 0:655158a7c194 | 61 | } |
| diffstorm | 0:655158a7c194 | 62 | } |
| diffstorm | 0:655158a7c194 | 63 | |
| diffstorm | 0:655158a7c194 | 64 | int main() { |
| diffstorm | 1:8012a60a982d | 65 | pc.printf("\n## Quad Brightness Controller ##\n"); |
| diffstorm | 0:655158a7c194 | 66 | pc.printf("Press '+/-' to change selected LED's brightness\n"); |
| diffstorm | 0:655158a7c194 | 67 | pc.printf("Use '1'-'4' keys to change selected LED\n"); |
| diffstorm | 0:655158a7c194 | 68 | |
| diffstorm | 1:8012a60a982d | 69 | flipper.attach(&callback, 1.0); // call flip() in every 1 sec |
| diffstorm | 0:655158a7c194 | 70 | |
| diffstorm | 0:655158a7c194 | 71 | while (true) { |
| diffstorm | 0:655158a7c194 | 72 | if (pc.readable()) { |
| diffstorm | 0:655158a7c194 | 73 | char c = pc.getc(); |
| diffstorm | 2:d955fe04abd9 | 74 | |
| diffstorm | 2:d955fe04abd9 | 75 | if ((c == '+') && (brightness < PWM_MAX)) { |
| diffstorm | 0:655158a7c194 | 76 | brightness += 0.01; |
| diffstorm | 0:655158a7c194 | 77 | SetBright(); |
| diffstorm | 2:d955fe04abd9 | 78 | } else if ((c == '-') && (brightness > 0.0)) { |
| diffstorm | 0:655158a7c194 | 79 | brightness -= 0.01; |
| diffstorm | 0:655158a7c194 | 80 | SetBright(); |
| diffstorm | 2:d955fe04abd9 | 81 | } else if (c == '1') { |
| diffstorm | 0:655158a7c194 | 82 | pc.printf("Led1 selected\n"); |
| diffstorm | 0:655158a7c194 | 83 | SelectedLED = 1; |
| diffstorm | 0:655158a7c194 | 84 | brightness = 0.0; |
| diffstorm | 2:d955fe04abd9 | 85 | } else if (c == '2') { |
| diffstorm | 0:655158a7c194 | 86 | pc.printf("Led2 selected\n"); |
| diffstorm | 0:655158a7c194 | 87 | SelectedLED = 2; |
| diffstorm | 0:655158a7c194 | 88 | brightness = 0.0; |
| diffstorm | 2:d955fe04abd9 | 89 | } else if (c == '3') { |
| diffstorm | 0:655158a7c194 | 90 | pc.printf("Led3 selected\n"); |
| diffstorm | 0:655158a7c194 | 91 | SelectedLED = 3; |
| diffstorm | 0:655158a7c194 | 92 | brightness = 0.0; |
| diffstorm | 2:d955fe04abd9 | 93 | } else if (c == '4') { |
| diffstorm | 0:655158a7c194 | 94 | pc.printf("Led4 selected\n"); |
| diffstorm | 0:655158a7c194 | 95 | SelectedLED = 4; |
| diffstorm | 0:655158a7c194 | 96 | brightness = 0.0; |
| diffstorm | 2:d955fe04abd9 | 97 | } else if (c == 'a') { |
| diffstorm | 2:d955fe04abd9 | 98 | pc.printf("Author: Eray Ozturk\n"); |
| diffstorm | 0:655158a7c194 | 99 | } |
| diffstorm | 0:655158a7c194 | 100 | } |
| diffstorm | 0:655158a7c194 | 101 | } |
| diffstorm | 0:655158a7c194 | 102 | } |