PWM

Dependencies:   mbed

Fork of 1620_App_Board_Pots by Craig Evans

Files at this revision

API Documentation at this revision

Comitter:
eencae
Date:
Tue Feb 28 20:00:48 2017 +0000
Parent:
0:74d086537907
Commit message:
Initial commit.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 74d086537907 -r 30d1c1087477 main.cpp
--- a/main.cpp	Fri Feb 24 14:54:55 2017 +0000
+++ b/main.cpp	Tue Feb 28 20:00:48 2017 +0000
@@ -1,6 +1,6 @@
 /* ELEC1620 Application Board Example
 
-Potentiometers
+PWM
 
 (c) Dr Craig A. Evans, University of Leeds, Feb 2017
 
@@ -8,24 +8,37 @@
 
 #include "mbed.h"
 
-AnalogIn pot0(p20);
-AnalogIn pot1(p19);
-AnalogIn pot2(p17);
+PwmOut red_led(p24);
+PwmOut green_led(p23);
+PwmOut blue_led(p22);
 
-int main() {
-    
+int main()
+{
+    float frequency = 100.0f;  // 100 Hz
+    red_led.period(1.0f/frequency);  // set the period of the waveform
+    // all PWM channels share the same period so only need to set for one
+
     while(1) {
-        
-        float pot0_val = pot0.read();  // returns a float in the range 0.0 to 1.0
-        //float pot0_val = pot0;       // short-hand 
-        
-        float pot0_voltage = pot0_val*3.3f;  // multiply by 3.3 to get the voltage
-        
-        int pot0_int_val = pot0.read_u16();  // can also get int in range 0 to 65,535
-        
-        printf("Pot 0 val = %.2f [%i] (%.2f V)\n",pot0_val,pot0_int_val,pot0_voltage);
-        
-        wait(0.2);
-        
+
+        // loop through 0 to 100% duty cycle in steps of 10% for each LED
+
+        // each for loop has 10 iterations
+        for (float dc1 = 0.0 ; dc1 <= 1.0 ; dc1 += 0.1) {
+            // they are 'nested' and so we have
+            for (float dc2 = 0.0 ; dc2 <= 1.0 ; dc2 += 0.1) {
+                // 10 x 10 x 10 = 1000 iterations (colours) in total
+                for (float dc3 = 0.0 ; dc3 <= 1.0 ; dc3 += 0.1) {
+
+                    red_led.write(dc1);    // fade LED
+                    green_led.write(dc2);  // fade LED
+                    blue_led.write(dc3);   // fade LED
+
+                    //red_led = dc1; // equivalent syntax
+
+                    wait_ms(20);  // small delay
+                }
+
+            }
+        }
     }
 }