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.
rgb_buttons/main.cpp@0:6a73d3dc037e, 2014-07-28 (annotated)
- Committer:
- ShawnHymel
- Date:
- Mon Jul 28 20:29:28 2014 +0000
- Revision:
- 0:6a73d3dc037e
Added main.cpp files from each of the demo programs. Libraries are not included.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ShawnHymel | 0:6a73d3dc037e | 1 | #include "mbed.h" |
| ShawnHymel | 0:6a73d3dc037e | 2 | |
| ShawnHymel | 0:6a73d3dc037e | 3 | // Define buttons |
| ShawnHymel | 0:6a73d3dc037e | 4 | InterruptIn button_red(p5); |
| ShawnHymel | 0:6a73d3dc037e | 5 | InterruptIn button_green(p6); |
| ShawnHymel | 0:6a73d3dc037e | 6 | InterruptIn button_blue(p7); |
| ShawnHymel | 0:6a73d3dc037e | 7 | |
| ShawnHymel | 0:6a73d3dc037e | 8 | // Define LED colors |
| ShawnHymel | 0:6a73d3dc037e | 9 | PwmOut led_red(p21); |
| ShawnHymel | 0:6a73d3dc037e | 10 | PwmOut led_green(p22); |
| ShawnHymel | 0:6a73d3dc037e | 11 | PwmOut led_blue(p23); |
| ShawnHymel | 0:6a73d3dc037e | 12 | |
| ShawnHymel | 0:6a73d3dc037e | 13 | // Interrupt Service Routine to increment the red color |
| ShawnHymel | 0:6a73d3dc037e | 14 | void inc_red() { |
| ShawnHymel | 0:6a73d3dc037e | 15 | |
| ShawnHymel | 0:6a73d3dc037e | 16 | float pwm; |
| ShawnHymel | 0:6a73d3dc037e | 17 | |
| ShawnHymel | 0:6a73d3dc037e | 18 | // Read in current PWM value and increment it |
| ShawnHymel | 0:6a73d3dc037e | 19 | pwm = led_red.read(); |
| ShawnHymel | 0:6a73d3dc037e | 20 | pwm += 0.1f; |
| ShawnHymel | 0:6a73d3dc037e | 21 | if (pwm > 1.0f) { |
| ShawnHymel | 0:6a73d3dc037e | 22 | pwm = 0.0f; |
| ShawnHymel | 0:6a73d3dc037e | 23 | } |
| ShawnHymel | 0:6a73d3dc037e | 24 | led_red.write(pwm); |
| ShawnHymel | 0:6a73d3dc037e | 25 | } |
| ShawnHymel | 0:6a73d3dc037e | 26 | |
| ShawnHymel | 0:6a73d3dc037e | 27 | // Interrupt Service Routine to increment the green color |
| ShawnHymel | 0:6a73d3dc037e | 28 | void inc_green() { |
| ShawnHymel | 0:6a73d3dc037e | 29 | |
| ShawnHymel | 0:6a73d3dc037e | 30 | float pwm; |
| ShawnHymel | 0:6a73d3dc037e | 31 | |
| ShawnHymel | 0:6a73d3dc037e | 32 | // Read in current PWM value and increment it |
| ShawnHymel | 0:6a73d3dc037e | 33 | pwm = led_green.read(); |
| ShawnHymel | 0:6a73d3dc037e | 34 | pwm += 0.1f; |
| ShawnHymel | 0:6a73d3dc037e | 35 | if (pwm > 1.0f) { |
| ShawnHymel | 0:6a73d3dc037e | 36 | pwm = 0.0f; |
| ShawnHymel | 0:6a73d3dc037e | 37 | } |
| ShawnHymel | 0:6a73d3dc037e | 38 | led_green.write(pwm); |
| ShawnHymel | 0:6a73d3dc037e | 39 | } |
| ShawnHymel | 0:6a73d3dc037e | 40 | |
| ShawnHymel | 0:6a73d3dc037e | 41 | // Interrupt Service Routine to increment the blue color |
| ShawnHymel | 0:6a73d3dc037e | 42 | void inc_blue() { |
| ShawnHymel | 0:6a73d3dc037e | 43 | |
| ShawnHymel | 0:6a73d3dc037e | 44 | float pwm; |
| ShawnHymel | 0:6a73d3dc037e | 45 | |
| ShawnHymel | 0:6a73d3dc037e | 46 | // Read in current PWM value and increment it |
| ShawnHymel | 0:6a73d3dc037e | 47 | pwm = led_blue.read(); |
| ShawnHymel | 0:6a73d3dc037e | 48 | pwm += 0.1f; |
| ShawnHymel | 0:6a73d3dc037e | 49 | if (pwm > 1.0f) { |
| ShawnHymel | 0:6a73d3dc037e | 50 | pwm = 0.0f; |
| ShawnHymel | 0:6a73d3dc037e | 51 | } |
| ShawnHymel | 0:6a73d3dc037e | 52 | led_blue.write(pwm); |
| ShawnHymel | 0:6a73d3dc037e | 53 | } |
| ShawnHymel | 0:6a73d3dc037e | 54 | |
| ShawnHymel | 0:6a73d3dc037e | 55 | // Main loop |
| ShawnHymel | 0:6a73d3dc037e | 56 | int main() { |
| ShawnHymel | 0:6a73d3dc037e | 57 | |
| ShawnHymel | 0:6a73d3dc037e | 58 | // Initialize all LED colors as off |
| ShawnHymel | 0:6a73d3dc037e | 59 | led_red.write(0.0f); |
| ShawnHymel | 0:6a73d3dc037e | 60 | led_green.write(0.0f); |
| ShawnHymel | 0:6a73d3dc037e | 61 | led_blue.write(0.0f); |
| ShawnHymel | 0:6a73d3dc037e | 62 | |
| ShawnHymel | 0:6a73d3dc037e | 63 | // Define three interrupts - one for each color |
| ShawnHymel | 0:6a73d3dc037e | 64 | button_red.fall(&inc_red); |
| ShawnHymel | 0:6a73d3dc037e | 65 | button_green.fall(&inc_green); |
| ShawnHymel | 0:6a73d3dc037e | 66 | button_blue.fall(&inc_blue); |
| ShawnHymel | 0:6a73d3dc037e | 67 | |
| ShawnHymel | 0:6a73d3dc037e | 68 | // Do nothing! We wait for an interrupt to happen |
| ShawnHymel | 0:6a73d3dc037e | 69 | while(1) { |
| ShawnHymel | 0:6a73d3dc037e | 70 | } |
| ShawnHymel | 0:6a73d3dc037e | 71 | } |