Fading a led (A basic example of PWM out)

Dependencies:   mbed

main.cpp

Committer:
jose_23991
Date:
2014-09-08
Revision:
0:6a5b7fc7aa18

File content as of revision 0:6a5b7fc7aa18:

#include "mbed.h"

int main()
{
    PwmOut led(LED1);                         // Create the LED object
    float brightness = 0.0;                   // How bright the LED is
    float fadeAmount = 0.02;                  // How many points to fade the LED by
    
    while(1)
    {
        led.write(brightness);                // Write a PWM analog brightness value on LED
        wait_ms(30);                          // wait for 30ms to see the dimming effect
        brightness = brightness + fadeAmount; // Increase the brightness             
        
        if((int)brightness == 1.0f)           // Turn brightness to 0 at the ends of the fade (f is for literal float - Delete the warning)
            brightness = 0;
    }
}