Updated to vary RGB values based on different frequency sine waves. Outputs angle and RGB values to serial console.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
icenyne
Date:
Mon Jul 29 19:51:02 2013 +0000
Parent:
4:0b2084fce8c7
Commit message:
Modified RGB values to be calculated by different frequency sin waves. Angle and RGB values print to serial console. Added some comments, too.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 0b2084fce8c7 -r c024e3b15ca0 main.cpp
--- a/main.cpp	Tue Feb 19 23:47:40 2013 +0000
+++ b/main.cpp	Mon Jul 29 19:51:02 2013 +0000
@@ -1,21 +1,38 @@
 #include "mbed.h"
+#include "math.h"               // include math.h to get sin function
 
 PwmOut r (LED_RED);
 PwmOut g (LED_GREEN);
 PwmOut b (LED_BLUE);
 
+float lr, lg, lb;               // temp variables for printing current values
+const float pi = 3.1415927;     // create a constant to use for value of pi
+
+Serial PC(USBTX,USBRX);         // define serial port for outputting RGB values
+
 int main() {
+
+    PC.baud(115200);            // set baud rate for serial port
+
     r.period(0.001);
     g.period(0.001);
     b.period(0.001);
 
     while (true) {
-        for (float i = 0.0; i < 1.0 ; i += 0.001) {
-            float p = 3 * i;
-            r = 1.0 - ((p < 1.0) ? 1.0 - p : (p > 2.0) ? p - 2.0 : 0.0);
-            g = 1.0 - ((p < 1.0) ? p : (p > 2.0) ? 0.0 : 2.0 - p);
-            b = 1.0 - ((p < 1.0) ? 0.0 : (p > 2.0) ? 3.0 - p : p - 1.0);
-            wait (0.0025);
+        for (float i = 0.0; i < 60*pi ; i += 0.001) {
+
+            lr = (1+sin(2*i))/2;    // calculate values for RGB based on different
+            lg = (1+sin(3*i))/2;    // frequency sin waves. This should give a nice
+            lb = (1+sin(5*i))/2;    // smooth transistion between colors and a 
+                                    // somewhat non repeating color sequence 
+
+            r = lr;                 // send RGB values to LED PWMs
+            g = lg;
+            b = lb;
+            
+            PC.printf("%f  %f  %f  %f\r\n", i, lr, lg, lb);    // output to console
+
+            wait (0.0025);          // some delay so we can see the changes
         }
     }
 }
\ No newline at end of file