sample program for dimming LED with PWM.

Dependencies:   mbed

if your platform is F401 or F411, you can use LED1 on board. else connect pwm pin to LED.

main.cpp

Committer:
Match314
Date:
2015-02-22
Revision:
0:9f98248c4dd5

File content as of revision 0:9f98248c4dd5:

/* sample for dimming led with pwm.
 * Copyright (c) 2015 Match
 */

#include "mbed.h"

// F401, F411 can use LED1.
// Other platform, use pwm pin and connect led.
PwmOut myled(LED1);

int main() {
    int duty = 0;   // unit:%
    myled.period_ms(1);     // pwm frequency set 1kHz.
    
    while(1) {
        // until 100%, increase by 5% in 50ms intervals
        for (; duty <= 100; duty += 5) {
            myled = duty / 100.0;
            wait_ms(50);
        }
        // until 0%, decrease by 5% in 50ms intervals
        for (; duty >= 0; duty -= 5) {
            myled = duty / 100.0;
            wait_ms(50);
        }
    }
}