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

Dependencies:   mbed

Committer:
icenyne
Date:
Mon Jul 29 19:51:02 2013 +0000
Revision:
5:c024e3b15ca0
Parent:
2:03e5c29343d1
Modified RGB values to be calculated by different frequency sin waves. Angle and RGB values print to serial console.  Added some comments, too.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 0:cf8a48b1fb23 1 #include "mbed.h"
icenyne 5:c024e3b15ca0 2 #include "math.h" // include math.h to get sin function
chris 0:cf8a48b1fb23 3
chris 1:eabc6f5b51d6 4 PwmOut r (LED_RED);
chris 1:eabc6f5b51d6 5 PwmOut g (LED_GREEN);
chris 1:eabc6f5b51d6 6 PwmOut b (LED_BLUE);
chris 0:cf8a48b1fb23 7
icenyne 5:c024e3b15ca0 8 float lr, lg, lb; // temp variables for printing current values
icenyne 5:c024e3b15ca0 9 const float pi = 3.1415927; // create a constant to use for value of pi
icenyne 5:c024e3b15ca0 10
icenyne 5:c024e3b15ca0 11 Serial PC(USBTX,USBRX); // define serial port for outputting RGB values
icenyne 5:c024e3b15ca0 12
emilmont 2:03e5c29343d1 13 int main() {
icenyne 5:c024e3b15ca0 14
icenyne 5:c024e3b15ca0 15 PC.baud(115200); // set baud rate for serial port
icenyne 5:c024e3b15ca0 16
chris 1:eabc6f5b51d6 17 r.period(0.001);
chris 1:eabc6f5b51d6 18 g.period(0.001);
chris 1:eabc6f5b51d6 19 b.period(0.001);
chris 1:eabc6f5b51d6 20
emilmont 2:03e5c29343d1 21 while (true) {
icenyne 5:c024e3b15ca0 22 for (float i = 0.0; i < 60*pi ; i += 0.001) {
icenyne 5:c024e3b15ca0 23
icenyne 5:c024e3b15ca0 24 lr = (1+sin(2*i))/2; // calculate values for RGB based on different
icenyne 5:c024e3b15ca0 25 lg = (1+sin(3*i))/2; // frequency sin waves. This should give a nice
icenyne 5:c024e3b15ca0 26 lb = (1+sin(5*i))/2; // smooth transistion between colors and a
icenyne 5:c024e3b15ca0 27 // somewhat non repeating color sequence
icenyne 5:c024e3b15ca0 28
icenyne 5:c024e3b15ca0 29 r = lr; // send RGB values to LED PWMs
icenyne 5:c024e3b15ca0 30 g = lg;
icenyne 5:c024e3b15ca0 31 b = lb;
icenyne 5:c024e3b15ca0 32
icenyne 5:c024e3b15ca0 33 PC.printf("%f %f %f %f\r\n", i, lr, lg, lb); // output to console
icenyne 5:c024e3b15ca0 34
icenyne 5:c024e3b15ca0 35 wait (0.0025); // some delay so we can see the changes
chris 1:eabc6f5b51d6 36 }
chris 1:eabc6f5b51d6 37 }
chris 0:cf8a48b1fb23 38 }