ver 1.0

Dependencies:   FATFileSystem SDFileSystem mbed-rtos mbed wave_player

Fork of LED_effects_w_sound by jim hamblen

LED PWM lighting effects with sounds played from *.wav files using threads and the mbed RTOS. For more info see http://developer.mbed.org/users/4180_1/notebook/led-lighting-effects-for-modelers/.

Committer:
4180_1
Date:
Fri Dec 05 18:16:56 2014 +0000
Revision:
10:21943bd35341
Parent:
9:f1aebfbe7e78
Child:
11:50e44c84411c
ver 1.0
;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:0d68fe822228 1 #include "mbed.h"
samux 0:0d68fe822228 2 #include "USBHostMSD.h"
4180_1 9:f1aebfbe7e78 3 #include "wave_player.h"
4180_1 10:21943bd35341 4 //LED lighting effects with sound using the RTOS
4180_1 9:f1aebfbe7e78 5 //Plays the wave file "sample.wav" on the USB flash drive
4180_1 10:21943bd35341 6 //using waveplayer code library
4180_1 10:21943bd35341 7 //Class D style audio output on P26 or
4180_1 10:21943bd35341 8 //use the Audio Out jack for connection to a set of amplified PC speakers (at higher volume)
4180_1 10:21943bd35341 9 //Needs a USB flash drive inserted with the *.wav files on it to run with audio effects
4180_1 10:21943bd35341 10 //Pins are setup for mbed LPC1768 on mbed application board
4180_1 10:21943bd35341 11 //Analog Audio Out Jack for PC Speakers- Sparkfun also has a breakout
4180_1 10:21943bd35341 12 //For more info see:
4180_1 10:21943bd35341 13 //http://developer.mbed.org/users/4180_1/notebook/led-lighting-effects-for-modelers/
4180_1 10:21943bd35341 14 AnalogOut DACout(p18);
4180_1 10:21943bd35341 15 //On Board Speaker on Application board but very low volume using PWM
4180_1 10:21943bd35341 16 PwmOut PWMout(p26);
4180_1 10:21943bd35341 17 //LEDs used in demo
4180_1 10:21943bd35341 18 PwmOut myled2(LED2);
4180_1 10:21943bd35341 19 PwmOut myled4(LED4);
4180_1 10:21943bd35341 20 //Use a PWM output to enable dimming
4180_1 10:21943bd35341 21 //1.0 is full on, 0.0 off, 0.5 50% on
samux 0:0d68fe822228 22
4180_1 10:21943bd35341 23 inline float random_number()
4180_1 10:21943bd35341 24 {
4180_1 10:21943bd35341 25 return (rand()/(float(RAND_MAX)));
4180_1 10:21943bd35341 26 }
4180_1 10:21943bd35341 27 void beacon(void const *args)
4180_1 10:21943bd35341 28 {
4180_1 10:21943bd35341 29 while(1) {
4180_1 10:21943bd35341 30 //LED warm up effect using PWM
4180_1 10:21943bd35341 31 for(int i=0; i<50; i++) {
4180_1 10:21943bd35341 32 myled2 = i/50.0;
4180_1 10:21943bd35341 33 Thread::wait(1000.0*0.02);
4180_1 10:21943bd35341 34 }
4180_1 10:21943bd35341 35 //LED at full brightness level
4180_1 10:21943bd35341 36 myled2 = 1.0;
4180_1 10:21943bd35341 37 Thread::wait(1000.0*0.25);
4180_1 10:21943bd35341 38 //LED cool down effect using PWM
4180_1 10:21943bd35341 39 for(int i=49; i>0; i--) {
4180_1 10:21943bd35341 40 myled2 = i/50.0;
4180_1 10:21943bd35341 41 Thread::wait(1000.0*0.02);
4180_1 10:21943bd35341 42 }
4180_1 10:21943bd35341 43 //LED off
4180_1 10:21943bd35341 44 myled2 = 0.0;
4180_1 10:21943bd35341 45 Thread::wait(1000.0*1.5);
4180_1 10:21943bd35341 46 }
4180_1 10:21943bd35341 47 }
4180_1 10:21943bd35341 48 void welding(void const *args)
4180_1 10:21943bd35341 49 {
4180_1 10:21943bd35341 50 float x = 0.0;
4180_1 10:21943bd35341 51 while(1) {
4180_1 10:21943bd35341 52 //get a new random number for PWM
4180_1 10:21943bd35341 53 x = random_number();
4180_1 10:21943bd35341 54 //add some exponential brightness scaling
4180_1 10:21943bd35341 55 //for more of a fast flash effect
4180_1 10:21943bd35341 56 myled2 = x*x*x;
4180_1 10:21943bd35341 57 //fast update rate for welding flashes
4180_1 10:21943bd35341 58 Thread::wait(1000.0*0.02);
4180_1 10:21943bd35341 59 //add a random pause between welds
4180_1 10:21943bd35341 60 if (random_number()>0.995) {
4180_1 10:21943bd35341 61 myled2 = 0.0;
4180_1 10:21943bd35341 62 Thread::wait(1000.0*random_number());
4180_1 10:21943bd35341 63 }
4180_1 10:21943bd35341 64 }
4180_1 10:21943bd35341 65 }
4180_1 10:21943bd35341 66 void fire(void const *args)
4180_1 10:21943bd35341 67 {
4180_1 10:21943bd35341 68 while(1) {
4180_1 10:21943bd35341 69 //get a new random number for PWM
4180_1 10:21943bd35341 70 myled2 = random_number();
4180_1 10:21943bd35341 71 //a bit slower time delay can be used for fire
4180_1 10:21943bd35341 72 wait(0.04);
4180_1 10:21943bd35341 73 }
4180_1 10:21943bd35341 74 }
4180_1 10:21943bd35341 75 void police(void const *args)
4180_1 10:21943bd35341 76 {
4180_1 10:21943bd35341 77 while(1) {
4180_1 10:21943bd35341 78 //flash three times on LED1
4180_1 10:21943bd35341 79 for(int i=0; i<3; i++) {
4180_1 10:21943bd35341 80 //ramp up brightness level
4180_1 10:21943bd35341 81 for(double x = 0.0; x <= 1.0; x = x+0.2) {
4180_1 10:21943bd35341 82 myled2 = x*x;
4180_1 10:21943bd35341 83 wait(.02);
4180_1 10:21943bd35341 84 }
4180_1 10:21943bd35341 85 }
4180_1 10:21943bd35341 86 myled2=0.0; //LED1 off
4180_1 10:21943bd35341 87 //flash three times on LED2
4180_1 10:21943bd35341 88 for(int i=0; i<3; i++) {
4180_1 10:21943bd35341 89 //ramp up brightness level
4180_1 10:21943bd35341 90 for(double x = 0.0; x <= 1.0; x = x+0.2) {
4180_1 10:21943bd35341 91 myled4 = x*x;
4180_1 10:21943bd35341 92 wait(.02);
4180_1 10:21943bd35341 93 }
4180_1 10:21943bd35341 94 }
4180_1 10:21943bd35341 95 myled4=0.0; //LED2 off
4180_1 10:21943bd35341 96 }
4180_1 10:21943bd35341 97 }
4180_1 10:21943bd35341 98 void lighthouse(void const *args)
4180_1 10:21943bd35341 99 {
4180_1 10:21943bd35341 100 float y=0.0;
4180_1 9:f1aebfbe7e78 101
4180_1 10:21943bd35341 102 while(1) {
4180_1 10:21943bd35341 103 for(double x=0.0; x <= 3.14159; x = x + 0.0314159) {
4180_1 10:21943bd35341 104 y = sin(x); //nice periodic function 0..1..0
4180_1 10:21943bd35341 105 myled2 = y*y*y;//exponential effect - needs a sharp peak
4180_1 10:21943bd35341 106 Thread::wait(1000.0*.025);
4180_1 10:21943bd35341 107 }
4180_1 10:21943bd35341 108 myled2 = 0.0;
4180_1 10:21943bd35341 109 //most lighthouses have a 5 second delay - so add another 2.5
4180_1 10:21943bd35341 110 Thread::wait(1000.0*2.5);
4180_1 10:21943bd35341 111 }
4180_1 10:21943bd35341 112 }
4180_1 10:21943bd35341 113
4180_1 10:21943bd35341 114
4180_1 10:21943bd35341 115 //function to simulate incandescent bulb warm up
4180_1 10:21943bd35341 116 //and cool down cycles
4180_1 10:21943bd35341 117 void incandescent_bulb(PwmOut led, float on_time = 0.1,
4180_1 10:21943bd35341 118 float warm_time = 0.2)
4180_1 10:21943bd35341 119 {
4180_1 10:21943bd35341 120 //LED warm up effect using PWM
4180_1 10:21943bd35341 121 for(int i=0; i<10; i++) {
4180_1 10:21943bd35341 122 led = i/10.0;
4180_1 10:21943bd35341 123 Thread::wait(1000.0*warm_time/10.0);
4180_1 10:21943bd35341 124 }
4180_1 10:21943bd35341 125 //LED at full brightness level
4180_1 10:21943bd35341 126 led = 1.0;
4180_1 10:21943bd35341 127 Thread::wait(1000.0*on_time);
4180_1 10:21943bd35341 128 //LED cool down effect using PWM
4180_1 10:21943bd35341 129 for(int i=9; i>0; i--) {
4180_1 10:21943bd35341 130 led = i/10.0;
4180_1 10:21943bd35341 131 Thread::wait(1000.0*warm_time/10.0);
4180_1 10:21943bd35341 132 }
4180_1 10:21943bd35341 133 //LED off
4180_1 10:21943bd35341 134 led = 0.0;
4180_1 10:21943bd35341 135 }
4180_1 10:21943bd35341 136 //LED Railroad Crossing Lighting Effect
4180_1 10:21943bd35341 137 void rrcrossing(void const *args)
4180_1 10:21943bd35341 138 {
4180_1 10:21943bd35341 139 while(1) {
4180_1 10:21943bd35341 140 incandescent_bulb(myled2);
4180_1 10:21943bd35341 141 incandescent_bulb(myled4);
4180_1 10:21943bd35341 142 }
4180_1 10:21943bd35341 143 }
4180_1 10:21943bd35341 144
4180_1 10:21943bd35341 145 //wave player plays a *.wav file to D/A and a PWM
4180_1 9:f1aebfbe7e78 146 wave_player waver(&DACout,&PWMout);
4180_1 9:f1aebfbe7e78 147
4180_1 9:f1aebfbe7e78 148 int main()
4180_1 9:f1aebfbe7e78 149 {
samux 0:0d68fe822228 150 USBHostMSD msd("usb");
4180_1 9:f1aebfbe7e78 151 FILE *wave_file;
4180_1 9:f1aebfbe7e78 152 //setup PWM hardware for a Class D style audio output
4180_1 9:f1aebfbe7e78 153 PWMout.period(1.0/400000.0);
4180_1 9:f1aebfbe7e78 154 // wait until connected to a USB device
4180_1 9:f1aebfbe7e78 155 while(!msd.connect()) {
4180_1 10:21943bd35341 156 Thread::wait(750);
samux 0:0d68fe822228 157 }
4180_1 10:21943bd35341 158 while(1) {
4180_1 10:21943bd35341 159 {
4180_1 10:21943bd35341 160 //Lighthouse demo with foghorn
4180_1 10:21943bd35341 161 Thread thread(lighthouse); //Start LED effect thread
4180_1 10:21943bd35341 162 for(int i=0; i<4; ++i) {
4180_1 10:21943bd35341 163 //open wav file and play it
4180_1 10:21943bd35341 164 wave_file=fopen("/usb/foghorn.wav","r");
4180_1 10:21943bd35341 165 waver.play(wave_file); //Plays (*.wav file
4180_1 10:21943bd35341 166 fclose(wave_file);
4180_1 10:21943bd35341 167 Thread::wait(1925);
4180_1 10:21943bd35341 168 }
4180_1 10:21943bd35341 169 //end of lighthouse demo;
4180_1 10:21943bd35341 170 thread.terminate(); //kills LED effect thread whan audio stops
4180_1 10:21943bd35341 171
4180_1 10:21943bd35341 172 } //code block forces thread out of scope reclaims mem-can recreate thread object
4180_1 10:21943bd35341 173 {
4180_1 10:21943bd35341 174 //Arc Welding with sound
4180_1 10:21943bd35341 175 Thread thread(welding);
4180_1 10:21943bd35341 176 //open wav file and play it
4180_1 10:21943bd35341 177 wave_file=fopen("/usb/welding.wav","r");
4180_1 10:21943bd35341 178 waver.play(wave_file);
4180_1 10:21943bd35341 179 fclose(wave_file);
4180_1 10:21943bd35341 180 //end of welding demo;
4180_1 10:21943bd35341 181 thread.terminate();
4180_1 10:21943bd35341 182 }
4180_1 10:21943bd35341 183 {
4180_1 10:21943bd35341 184 //Police Lights with siren
4180_1 10:21943bd35341 185 //code block forces thread out of scope
4180_1 10:21943bd35341 186 Thread thread(police);
4180_1 10:21943bd35341 187 //open wav file and play it
4180_1 10:21943bd35341 188 wave_file=fopen("/usb/emgsiren.wav","r");
4180_1 10:21943bd35341 189 waver.play(wave_file);
4180_1 10:21943bd35341 190 fclose(wave_file);
4180_1 10:21943bd35341 191 //end of police light demo;
4180_1 10:21943bd35341 192 thread.terminate();
4180_1 10:21943bd35341 193 }
4180_1 10:21943bd35341 194 {
4180_1 10:21943bd35341 195 //Fire with sound
4180_1 10:21943bd35341 196 Thread thread(fire);
4180_1 10:21943bd35341 197 //open wav file and play it
4180_1 10:21943bd35341 198 wave_file=fopen("/usb/fire.wav","r");
4180_1 10:21943bd35341 199 waver.play(wave_file);
4180_1 10:21943bd35341 200 fclose(wave_file);
4180_1 10:21943bd35341 201 //end of fire demo;
4180_1 10:21943bd35341 202 thread.terminate();
4180_1 10:21943bd35341 203 }
4180_1 10:21943bd35341 204
4180_1 10:21943bd35341 205 {
4180_1 10:21943bd35341 206 //RR Crossing Lights with Signal Bells
4180_1 10:21943bd35341 207 Thread thread(rrcrossing);
4180_1 10:21943bd35341 208 //open wav file and play it
4180_1 10:21943bd35341 209 wave_file=fopen("/usb/SignalBell.wav","r");
4180_1 10:21943bd35341 210 waver.play(wave_file);
4180_1 10:21943bd35341 211 fclose(wave_file);
4180_1 10:21943bd35341 212 //end of railroad crossing demo;
4180_1 10:21943bd35341 213 thread.terminate();
4180_1 10:21943bd35341 214 }
4180_1 10:21943bd35341 215 }
4180_1 10:21943bd35341 216 }