Demo of LED lighting effects using PWM and wait for time delays. Pins are setup for LPC1768 platform’s LEDs. For complete information, see http://developer.mbed.org/users/4180_1/notebook/led-lighting-effects-for-modelers/

Dependencies:   mbed

Committer:
4180_1
Date:
Fri Nov 28 18:02:35 2014 +0000
Revision:
0:5fc0791b9f3c
ver 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:5fc0791b9f3c 1 #include "mbed.h"
4180_1 0:5fc0791b9f3c 2 //LED welding lighting effect
4180_1 0:5fc0791b9f3c 3 PwmOut myled(LED1);
4180_1 0:5fc0791b9f3c 4 //Use PWM output to enable LED dimming
4180_1 0:5fc0791b9f3c 5
4180_1 0:5fc0791b9f3c 6 //Use Cs random number generator rand(), but scaled and
4180_1 0:5fc0791b9f3c 7 //converted to a float 0.0 to 1.0 for PWM output
4180_1 0:5fc0791b9f3c 8 inline float random_number()
4180_1 0:5fc0791b9f3c 9 {
4180_1 0:5fc0791b9f3c 10 return (rand()/(float(RAND_MAX)));
4180_1 0:5fc0791b9f3c 11 }
4180_1 0:5fc0791b9f3c 12
4180_1 0:5fc0791b9f3c 13 int main()
4180_1 0:5fc0791b9f3c 14 {
4180_1 0:5fc0791b9f3c 15 float x = 0.0;
4180_1 0:5fc0791b9f3c 16 while(1) {
4180_1 0:5fc0791b9f3c 17 //get a new random number for PWM
4180_1 0:5fc0791b9f3c 18 x = random_number();
4180_1 0:5fc0791b9f3c 19 //add some exponential brightness scaling
4180_1 0:5fc0791b9f3c 20 //for more of a fast flash effect
4180_1 0:5fc0791b9f3c 21 myled = x*x*x;
4180_1 0:5fc0791b9f3c 22 //fast update rate for welding flashes
4180_1 0:5fc0791b9f3c 23 wait(0.02);
4180_1 0:5fc0791b9f3c 24 //add a random pause between welds
4180_1 0:5fc0791b9f3c 25 if (random_number()>0.9925) {
4180_1 0:5fc0791b9f3c 26 myled=0.0;
4180_1 0:5fc0791b9f3c 27 wait(4.0*random_number());
4180_1 0:5fc0791b9f3c 28 }
4180_1 0:5fc0791b9f3c 29 }
4180_1 0:5fc0791b9f3c 30 }