Used in class for algorithm development NMHU. Looking at the behavior of the PWM class

Dependencies:   mbed

Fork of blink_kl46z by Stanley Cohen

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
diff -r e23fffd4b9a7 -r 24090ed5f981 main.cpp
--- 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