Change LED brightness brightness from 0 to 100%

Dependencies:   mbed

main.cpp

Committer:
virajjayaweera
Date:
2013-07-12
Revision:
0:adc859eb22d0

File content as of revision 0:adc859eb22d0:

//Change the brightness of onboard LED using PWM
// Tested with FRDM-KL25Z board
#include "mbed.h"

PwmOut led(LED1);


int main()
{
   float duty_cycle = 1; //PWM output is inverted for LED. Therefore duty_cycle = 1 means LED off   
    
   led.period_ms(1); // PWM freequency = 1/period = 1 kHz

    while(1)
    {
        led = duty_cycle;  // also "led.write(duty_cycle)" will do the same thing
        duty_cycle = duty_cycle - 0.01;
        wait(0.05); // delay for visually see the effect 
        
        if(duty_cycle <= 0)
            duty_cycle = 1;
        
    }
}