Fading a led (A basic example of PWM out)

Dependencies:   mbed

Revision:
0:6a5b7fc7aa18
diff -r 000000000000 -r 6a5b7fc7aa18 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Sep 08 19:49:11 2014 +0000
@@ -0,0 +1,18 @@
+#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;
+    }
+}