Shows a little more logic and defines off and on for the led's which are false and true respectively. Also, time for blink is a 2-element array.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
scohennm
Date:
Wed Sep 03 15:03:30 2014 +0000
Parent:
0:e23fffd4b9a7
Commit message:
Example of the use of PWM for the KL46Z. Used for algorithms class at NMHU

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Aug 14 21:18:38 2014 +0000
+++ b/main.cpp	Wed Sep 03 15:03:30 2014 +0000
@@ -1,19 +1,22 @@
 #include "mbed.h"
 #define LEDON false
 #define LEDOFF true
+#define PWMDWELL 50// milliseconds
+#define DFDELTA 0.01
 
 // slightly more interesting blinky 140814 sc
+// Change to use PWM
 
-float blinks[]={0.200, 0.700};
-int ledState = LEDON;
-DigitalOut greenColor(LED_GREEN);
-DigitalOut redColor(LED_RED);
+float dutyFactor = 0.0;
+PwmOut greenColor(LED_GREEN);
+PwmOut redColor(LED_RED);
 
 int main() {
     while(true) {
-        ledState = !ledState; // Flip the general state
-        redColor = ledState;
-        greenColor = !ledState;// flip state but don't store it.
-        wait(blinks[ledState]);
+        redColor.write(dutyFactor);
+        greenColor.write(1.0 - dutyFactor);
+        dutyFactor += DFDELTA; 
+        if(dutyFactor >= 1.0) dutyFactor = 0.0;  
+        wait_ms(PWMDWELL);
     }
 }
\ No newline at end of file