My first program to create a chase pattern with the on-board LEDs, but this uses the concept of a rotating phasor and pulse width modulation to dim the LEDs.
Diff: main.cpp
- Revision:
- 0:cf65b0672fcd
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Nov 30 15:05:44 2010 +0000 @@ -0,0 +1,43 @@ +/* Chase pattern using a rotating phasor + * Dan Minear + * 2010-11-20 +*/ + +#include "mbed.h" +#include "math.h" + +Serial pc(USBTX, USBRX); // tx, rx +PwmOut led1(LED1); +PwmOut led2(LED2); +PwmOut led3(LED3); +PwmOut led4(LED4); + +float brightness = 0.0; +float factor = 0.01; + +int main() { + pc.printf("\nPress 'u' or 'd'\n"); + char c; + float angle = 0; + float factor = 0.01; + + while(1) { + if (pc.readable()) { + c = pc.getc(); + if (c == 'u') { factor += 0.01; } + if (c =='d') { factor -= 0.01; } + } + angle += factor; + if (angle > 2 * 3.14159 ) { angle = 0; } + + led1 = (sin(angle) / 2)/2; + led2 = (sin(angle + 3.1416 / 4))/2; + led3 = (sin(angle + 3.1416 / 2))/2; + led4 = (sin(angle + 3.1416 * 3 / 4))/2; + + wait(0.01); + } +} + + +