lab1

Dependencies:   mbed

Committer:
mbarros7
Date:
Tue Feb 02 20:18:27 2021 +0000
Revision:
0:5af73d509412
kk

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbarros7 0:5af73d509412 1 #include "mbed.h"
mbarros7 0:5af73d509412 2
mbarros7 0:5af73d509412 3 // Define buttons
mbarros7 0:5af73d509412 4 InterruptIn button_red(p16);
mbarros7 0:5af73d509412 5 InterruptIn button_green(p17);
mbarros7 0:5af73d509412 6 InterruptIn button_blue(p18);
mbarros7 0:5af73d509412 7
mbarros7 0:5af73d509412 8 // Define LED colors
mbarros7 0:5af73d509412 9 PwmOut led_red(p21);
mbarros7 0:5af73d509412 10 PwmOut led_green(p22);
mbarros7 0:5af73d509412 11 PwmOut led_blue(p23);
mbarros7 0:5af73d509412 12
mbarros7 0:5af73d509412 13 // Interrupt Service Routine to increment the red color
mbarros7 0:5af73d509412 14 void inc_red() {
mbarros7 0:5af73d509412 15
mbarros7 0:5af73d509412 16 float pwm;
mbarros7 0:5af73d509412 17
mbarros7 0:5af73d509412 18 // Read in current PWM value and increment it
mbarros7 0:5af73d509412 19 pwm = led_red.read();
mbarros7 0:5af73d509412 20 pwm += 0.1f;
mbarros7 0:5af73d509412 21 if (pwm > 1.0f) {
mbarros7 0:5af73d509412 22 pwm = 0.0f;
mbarros7 0:5af73d509412 23 }
mbarros7 0:5af73d509412 24 led_red.write(pwm);
mbarros7 0:5af73d509412 25 }
mbarros7 0:5af73d509412 26
mbarros7 0:5af73d509412 27 // Interrupt Service Routine to increment the green color
mbarros7 0:5af73d509412 28 void inc_green() {
mbarros7 0:5af73d509412 29
mbarros7 0:5af73d509412 30 float pwm;
mbarros7 0:5af73d509412 31
mbarros7 0:5af73d509412 32 // Read in current PWM value and increment it
mbarros7 0:5af73d509412 33 pwm = led_green.read();
mbarros7 0:5af73d509412 34 pwm += 0.1f;
mbarros7 0:5af73d509412 35 if (pwm > 1.0f) {
mbarros7 0:5af73d509412 36 pwm = 0.0f;
mbarros7 0:5af73d509412 37 }
mbarros7 0:5af73d509412 38 led_green.write(pwm);
mbarros7 0:5af73d509412 39 }
mbarros7 0:5af73d509412 40
mbarros7 0:5af73d509412 41 // Interrupt Service Routine to increment the blue color
mbarros7 0:5af73d509412 42 void inc_blue() {
mbarros7 0:5af73d509412 43
mbarros7 0:5af73d509412 44 float pwm;
mbarros7 0:5af73d509412 45
mbarros7 0:5af73d509412 46 // Read in current PWM value and increment it
mbarros7 0:5af73d509412 47 pwm = led_blue.read();
mbarros7 0:5af73d509412 48 pwm += 0.1f;
mbarros7 0:5af73d509412 49 if (pwm > 1.0f) {
mbarros7 0:5af73d509412 50 pwm = 0.0f;
mbarros7 0:5af73d509412 51 }
mbarros7 0:5af73d509412 52 led_blue.write(pwm);
mbarros7 0:5af73d509412 53 }
mbarros7 0:5af73d509412 54
mbarros7 0:5af73d509412 55 // Main loop
mbarros7 0:5af73d509412 56 int main() {
mbarros7 0:5af73d509412 57
mbarros7 0:5af73d509412 58 // Initialize all LED colors as off
mbarros7 0:5af73d509412 59 led_red.write(0.0f);
mbarros7 0:5af73d509412 60 led_green.write(0.0f);
mbarros7 0:5af73d509412 61 led_blue.write(0.0f);
mbarros7 0:5af73d509412 62
mbarros7 0:5af73d509412 63 // Define three interrupts - one for each color
mbarros7 0:5af73d509412 64 button_red.fall(&inc_red);
mbarros7 0:5af73d509412 65 button_green.fall(&inc_green);
mbarros7 0:5af73d509412 66 button_blue.fall(&inc_blue);
mbarros7 0:5af73d509412 67
mbarros7 0:5af73d509412 68 // Do nothing! We wait for an interrupt to happen
mbarros7 0:5af73d509412 69 while(1) {
mbarros7 0:5af73d509412 70 }
mbarros7 0:5af73d509412 71 }