Guide

1/12/2018 mbed Starter Kit Experiment Guide - learn.sparkfun.com
https://learn.sparkfun.com/tutorials/mbed-starter-kit-experiment-guide/all 11/65
#include "mbed.h"
// Define buttons
InterruptIn button_red(p5);
InterruptIn button_green(p6);
InterruptIn button_blue(p7);
// Define LED colors
PwmOut led_red(p21);
PwmOut led_green(p22);
PwmOut led_blue(p23);
// Interrupt Service Routine to increment the red color
void inc_red() {
float pwm;
// Read in current PWM value and increment it
pwm = led_red.read();
pwm += 0.1f;
if (pwm > 1.0f) {
pwm = 0.0f;
}
led_red.write(pwm);
}
// Interrupt Service Routine to increment the green color
void inc_green() {
float pwm;
// Read in current PWM value and increment it
pwm = led_green.read();
pwm += 0.1f;
if (pwm > 1.0f) {
pwm = 0.0f;
}
led_green.write(pwm);
}
// Interrupt Service Routine to increment the blue color
void inc_blue() {
float pwm;
// Read in current PWM value and increment it
pwm = led_blue.read();
pwm += 0.1f;
if (pwm > 1.0f) {
pwm = 0.0f;
}
led_blue.write(pwm);
}
// Main loop
int main() {
// Initialize all LED colors as off
led_red.write(0.0f);
led_green.write(0.0f);
led_blue.write(0.0f);
// Define three interrupts - one for each color
button_red.fall(&inc_red);
button_green.fall(&inc_green);
button_blue.fall(&inc_blue);
// Do nothing! We wait for an interrupt to happen
while(1) {
}
}
Run
Click the “Compile” button to download a binary file with your compiled program. Copy that file to the LPC1768. You can choose to delete the previous
mbed_blinky_LPC1768.bin or just leave it there. If you have more than one .bin file on the mbed when you press the restart button, the mbed will choose the
newest file to load and run.
Press the LPC1768’s restart button. You should be able to press any of the 3 buttons on your breadboard to increment the red, green, and blue intensity of the
LED. Note that when you reach the maximum brightness, the intensity resets to 0.