sample program for dimming LED with PWM.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* sample for dimming led with pwm.
00002  * Copyright (c) 2015 Match
00003  */
00004 
00005 #include "mbed.h"
00006 
00007 // F401, F411 can use LED1.
00008 // Other platform, use pwm pin and connect led.
00009 PwmOut myled(LED1);
00010 
00011 int main() {
00012     int duty = 0;   // unit:%
00013     myled.period_ms(1);     // pwm frequency set 1kHz.
00014     
00015     while(1) {
00016         // until 100%, increase by 5% in 50ms intervals
00017         for (; duty <= 100; duty += 5) {
00018             myled = duty / 100.0;
00019             wait_ms(50);
00020         }
00021         // until 0%, decrease by 5% in 50ms intervals
00022         for (; duty >= 0; duty -= 5) {
00023             myled = duty / 100.0;
00024             wait_ms(50);
00025         }
00026     }
00027 }