Skip to content
Newer
Older
100644 635 lines (481 sloc) 12.7 KB
8f02b8c @madshobye initial commit - source for explosion village 2008
madshobye authored Jul 17, 2011
1 /*****************************************************************************
2 TODO
3 - fire sequence
4
5 - lav signal ud til jonas og sonny
6
7 - sleep mode
8 - lystofr¿reffekt
9 - eeproom: times hits, energylevel
10
11 - lav energi niveau over tid i stedet
12 - define energylevelAcc as energy level plus
13
14 *****************************************************************************/
15
16
17 //*****************************************************************************
18 // MAKROS
19 //*****************************************************************************
20
21
22 #define SET(x,y) (x |=(1<<y)) //-Bit set/clear macros
23 #define CLR(x,y) (x &= (~(1<<y))) // |
24 #define CHK(x,y) (x & (1<<y)) // |
25 #define TOG(x,y) (x^=(1<<y)) //-+
26
27
28 //*****************************************************************************
29 // DEFINES
30 //*****************************************************************************
31
32 // dmx
33 #define T_BIT 8
34 #define DmxOut 6
35 #define numDmx 16
36 #define numBoxesWithSound 3
37 #define numBoxesWithOutSound 8
38 #define energyChannel 11
39
40
41
42 // gas kanon
43 #define relay 24
44
45 #define tentFire 28
46 // Lystofr¿r
47 #define data 25
48 #define clock 26
49 #define latch 27
50 #define numLights 4
51
52 // io
53 #define diode1 35
54 #define diode2 36
55 #define diode3 37
56 #define btn1 29
57 #define btn2 4
58 #define btn3 5
59
60
61 // sound in
62 #define numChannels 3
63
64
65 // other
66 #define DEBUG 1
67 #define maxVal 100.0
68 #define energyLevelGran 0.05 //20min =0.016 // 0.05 = 6.6min 0.01 = 33min 0.03 = 11min 0.02 = 16min // std 0.03
69 #define channelStart 2
70
71
72 // timers
73 #define timerEnergy 0
74 #define timerFire 1
75 #define timerBlink 2
76
77 //*****************************************************************************
78 // VARIABLES
79 //*****************************************************************************
80
81 // Mappings
82
83 boolean switchlights = false;
84 byte boxToDmx[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22,23};
85
86
87 // Dmx
88 int Dmx[numDmx];
89 byte currentDmx =0;
90
91
92
93 // MIDI
94
95 int midiInPos =0;
96 int midiData[4];
97 int notetest = 0;
98
99 // SYSTEM ENERGY
100 float energyLevel =0;
101 float energyLevelAcc = 0;
102 int beatBox[numChannels];
103 //byte lights[] ={99,4(1),9(2),5(3),99,99,11(6),1(7),10(8),99,99,6(11),7(12),8(13),99,2(15),99}; //placeringpŒtŒrn[datapos]
104 byte lights[] ={0,0,0,0,0,0,0,0,0,0,3,4,2,1,0,0};
105 // hvis
106 // BEATS 14,13,11,12
107
108 int channelVolume[numChannels];
109 int channelGoingUp[numChannels];
110
111 int currentChannel =0;
112
113 // Fire sequence
114
115 boolean fireOn = false;
116
117
118 //*****************************************************************************
119 // SETUP
120 //*****************************************************************************
121
122
123 void setup() {
124
125 pinMode(DmxOut, OUTPUT);
126 pinMode(relay, OUTPUT);
127 pinMode(data, OUTPUT);
128 pinMode(clock, OUTPUT);
129 pinMode(latch, OUTPUT);
130 pinMode(tentFire,OUTPUT);
131 pinMode(diode1, OUTPUT);
132 pinMode(diode2, OUTPUT);
133 pinMode(diode3, OUTPUT);
134 pinMode(btn1, INPUT);
135 pinMode(btn2, INPUT);
136 pinMode(btn3, INPUT);
137 digitalWrite(relay,LOW);
138
139 Serial1.begin(31250);
140 Serial.begin(9600);
141 for(int i =0;i < currentChannel;i++)
142 {
143 beatBox[i] = random(numDmx-1);
144 }
145
146 }
147
148
149
150 //*****************************************************************************
151 // LOOP - THE ART
152 //*****************************************************************************
153 boolean diode1Blink = false;
154 boolean shootfire = true;
155 boolean btn1State = true;
156 void loop() {
157
158
159 digitalWrite(diode2,shootfire);
160 if(digitalRead(btn1) != btn1State && btn1State)
161 {
162 // shootfire = !shootfire;
163 doFireSequence(true);
164
165 }
166 btn1State =digitalRead(btn1);
167
168 if(timer(500,timerBlink))
169 {
170 digitalWrite(diode1,diode1Blink);
171 diode1Blink = !diode1Blink;
172 }
173
174 for(int i=0; i < 20;i++)
175 {
176 for(int i=0;i < 6;i++)
177 {
178 soundInHandler();
179
180
181 }
182
183 // Energy
184 energyHandler();
185
186
187 // Handle fadeout on dmx
188 DmxFadeOut();
189 DmxSend();
190 doFireSequence(false);
191
192 }
193
194 setLights();
195 //testLights();
196
197 }
198
199 //*****************************************************************************
200 // Timer
201 //*****************************************************************************
202 long times[10];
203 long last_millis =0;
204
205 boolean timer(int time, byte num)
206 {
207 timer( time, num, true);
208 }
209 boolean timer(int time, byte num, boolean reset)
210 {
211 if (millis() < last_millis)
212 {
213 for(int i=0;i< 10;i++)
214 {
215 times[i] = 0;
216 }
217
218 }
219 if (millis() > times[num] + time)
220 {
221 if(reset)
222 times[num] = millis();
223 return true;
224 }
225 else
226 {
227 return false;
228 }
229 }
230
231 void timerReset(byte num)
232 {
233 times[num] = millis();
234 }
235
236
237 //*****************************************************************************
238 // Energy
239 //*****************************************************************************
240
241
242 void energyHandler()
243 {
244
245 Dmx[energyChannel] = 100;
246
247 if(energyLevelAcc >= maxVal && !fireOn)
248 {
249 energyLevelAcc = 100;
250 doFireSequence(true);
251 }
252 if (timer(200,timerEnergy))
253 {
254 if(energyLevel > 50)
255 {
256 energyLevelAcc += energyLevelGran;
257 energyLevelAcc = min(energyLevelAcc,maxVal);
258 }
259 DP('E',(int)energyLevel, 'A', (int)energyLevelAcc);
260 energyLevel = max(0,energyLevel-1);
261 noteOut(0xB0, 19,100-energyLevel);
262 //xxx noteOut(0xB0, 20,energyLevelAcc);
263
264 }
265
266 }
267 //*****************************************************************************
268 // Fire
269 //*****************************************************************************
270
271 void doFireSequence(boolean start)
272 {
273 if (shootfire)
274 {
275 if(start)
276 {
277 digitalWrite(relay,HIGH);
278 digitalWrite(tentFire,HIGH);
279 noteOut(0x91,10, 126);
280 fireOn = true;
281 timerReset(timerFire);
282 if(DEBUG)
283 {
284 Serial.println("Fire on");
285 }
286 }
287 if(fireOn)
288 {
289 Dmx[(int)random(numDmx-1)] =(int) random(100);
290 energyLevelAcc =(int)random(100);
291
292 if(timer(20000,timerFire))// xx bug!!!
293 {
294 digitalWrite(relay,LOW);
295 digitalWrite(tentFire,LOW);
296 fireOn = false;
297 energyLevelAcc =0;
298
299 if(DEBUG)
300 {
301 Serial.println("Fire off");
302 }
303 }
304
305 }
306 }
307
308 }
309
310 void startFireSequence()
311 {
312
313 }
314
315
316
317 //*****************************************************************************
318 // Sound in
319 //*****************************************************************************
320 int tmpcounter = 0;
321 void soundInHandler()
322 {
323
324 int volume = (analogRead(currentChannel)- 412);
325 // DP('S', volume, currentChannel);
326
327
328 boolean tmpUp = volume > channelVolume[currentChannel];
329
330
331 if (!tmpUp && channelGoingUp[currentChannel] && channelVolume[currentChannel] > 15) // beat detected
332 {
333 switchlights = !switchlights;
334 //DP('B',(currentChannel+1));
335 // send dmx
336 if(!fireOn)
337 {
338 // noteOut(0x92, 40 + currentChannel, min(volume,127));
339 }
340 // tempxxxx
341
342
343
344 // raise energy
345 energyLevel += 7;
346
347 energyLevel = min(energyLevel, maxVal);
348
349
350 // set dmx
351 Dmx[boxToDmx[currentChannel]] = maxVal;
352
353 if(((int)random(5))==1)
354 {
355 // make dmx effect on the rest of the boxes
356
357 Dmx[boxToDmx[beatBox[currentChannel]+ numBoxesWithSound]] = maxVal;
358 beatBox[currentChannel]++;
359 if (beatBox[currentChannel]>= numBoxesWithOutSound)
360 {
361 beatBox[currentChannel] = 0;
362 }
363 }
364
365
366 }
367
368
369 channelGoingUp[currentChannel] = tmpUp;
370 channelVolume[currentChannel] = volume;
371
372 currentChannel++;
373 currentChannel = currentChannel % numChannels;
374 }
375
376
377
378
379
380
381 //*****************************************************************************
382 // MIDI
383 //*****************************************************************************
384
385
386 void noteOut(char cmd, char data1, char data2) {
387 Serial1.print(cmd, BYTE);
388 Serial1.print(data1, BYTE);
389 Serial1.print(data2, BYTE);
390 }
391
392 void noteIn()
393 {
394 /// MIDI IN
395 while (Serial1.available()>0)
396 {
397 int value = Serial1.read();
398
399 if (value < 0)
400 {
401 midiInPos = 0;
402 midiData[0] = 0;
403 midiData[1] = 0;
404 midiData[2] = 0;
405
406 }
407 else
408 {
409 midiInPos++;
410 if (midiInPos > 2)
411 {
412 midiInPos =1;
413 }
414 }
415
416 midiData[midiInPos] = value;
417
418
419 }
420
421
422
423 if (midiInPos == 2)
424 {
425
426 //Serial.println( midiData[1]);
427
428 }
429 }
430
431 //*****************************************************************************
432 // Lystofr¿r
433 //*****************************************************************************
434
435 int currentlight =0;
436
437 void testLights()
438 {
439 if(digitalRead(btn1) != btn1State && btn1State)
440 {
441 currentlight ++;
442 currentlight = currentlight % 5;
443 DP('l',currentlight);
444
445 }
446 btn1State =digitalRead(btn1);
447 digitalWrite(latch,HIGH);
448 delayMicroseconds(1);
449
450 for(int b = 5; b >=0; b >b--)
451 {
452 if(b==3)
453 digitalWrite(data,currentlight > 0);
454 else if(b==2)
455 digitalWrite(data,currentlight > 1);
456 else if(b==0)
457 digitalWrite(data,currentlight > 2);
458 else if(b==1)
459 digitalWrite(data, currentlight > 3);
460 else
461 digitalWrite(data,false);
462
463 //digitalWrite(data,switchlights);
464 delayMicroseconds(1);
465 digitalWrite(clock, HIGH);
466 delayMicroseconds(1);
467 digitalWrite(clock, LOW);
468 delayMicroseconds(1);
469
470 }
471 digitalWrite(latch,LOW);
472 delayMicroseconds(1);
473 digitalWrite(latch,HIGH);
474 delayMicroseconds(1);
475 }
476
477 void setLights()
478 {
479 digitalWrite(latch,HIGH);
480 delayMicroseconds(1);
481 currentlight = round((float)(numLights+1) * energyLevelAcc/maxVal);
482 //DP('l',currentlight);
483 for(int b = 5; b >= 0; b--)
484 {
485 if(b==3)
486 digitalWrite(data,currentlight > 0);
487 else if(b==2)
488 digitalWrite(data,currentlight > 1);
489 else if(b==0)
490 digitalWrite(data,currentlight > 2);
491 else if(b==1)
492 digitalWrite(data, currentlight > 3);
493 else
494 digitalWrite(data,false);
495
496 delayMicroseconds(1);
497 digitalWrite(clock, HIGH);
498 delayMicroseconds(1);
499 digitalWrite(clock, LOW);
500 delayMicroseconds(1);
501
502 }
503 digitalWrite(latch,LOW);
504 delayMicroseconds(1);
505 digitalWrite(latch,HIGH);
506 delayMicroseconds(1);
507 }
508
509
510 //*****************************************************************************
511 // Send Dmx
512 //*****************************************************************************
513
514 void DmxFadeOut()
515 {
516 // Dmx[currentDmx] = Dmx[currentDmx] * 0.95;
517 if(currentDmx >= numChannels)
518 {
519 Dmx[currentDmx] = max(Dmx[currentDmx]-4,0);
520 }
521 else
522 {
523 Dmx[currentDmx] = max(Dmx[currentDmx]-10,0);
524 }
525
526
527 }
528
529 unsigned int heartBeatPos;
530
531 int DmxSend()
532 {
533 if (currentDmx ==0)
534 {
535 digitalWrite(DmxOut, LOW);
536 delayMicroseconds(100);
537 digitalWrite(DmxOut, HIGH);
538 delayMicroseconds(10);
539 sends(0);
540 }
541
542 int valueDmx;
543 heartBeatPos = heartBeatPos +2;
544 heartBeatPos = heartBeatPos % 47000;
545 if (currentDmx >= numBoxesWithSound)
546 {
547 float heartBeat = (sin(((float)heartBeatPos)/1000 + PI/2*currentDmx)+1)*7+36;
548
549 valueDmx = 2.55*50.0*(sin((PI* ((Dmx[currentDmx])/2+heartBeat))/100.0 - PI/2.0)+1.0);
550 }
551 else
552 {
553
554 valueDmx = 2.55*50.0*(sin((PI* ((Dmx[currentDmx])))/100.0 - PI/2.0)+1.0);
555 }
556
557 sends((int)valueDmx);
558 // DP('D', valueDmx, currentDmx == 4);
559
560
561
562 currentDmx++;
563 currentDmx = currentDmx % numDmx;
564
565 }
566
567 int sends(char s)
568 {
569 cli();
570 unsigned int t;
571 unsigned char bit;
572 CLR(PORTD,DmxOut);
573
574 for(t=0;t<T_BIT;t++)
575 asm("NOP");
576 for(bit=0;bit<8;bit++)
577 {
578 if(s&0x01)
579 SET(PORTD,DmxOut);
580
581 else
582 CLR(PORTD,DmxOut);
583
584 s=s>>1;
585
586 asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
587 asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
588 asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
589 asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
590 asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
591 asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
592 asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
593 asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
594 asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
595 asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
596 asm("NOP");
597 }
598 SET(PORTD,DmxOut);
599
600 for(t=0;t<T_BIT;t++)
601 asm("NOP");
602 for(t=0;t<T_BIT;t++)
603 asm("NOP");
604 sei();
605 return 0;
606
607 }
608
609 void DP(char t, int v, boolean printit)
610 {
611 if (printit)
612 DP(t,v);
613 }
614
615 void DP(char t, int v)
616 {
617 if (DEBUG)
618 {
619 Serial.print(t);
620 Serial.println(v);
621 }
622 }
623
624 void DP(char t, int v, char t2, int v2)
625 {
626 if (DEBUG)
627 {
628 Serial.print(t);
629 Serial.print(v);
630 Serial.print(' ');
631 Serial.print(t2);
632 Serial.println(v2);
633 }
634 }
Something went wrong with that request. Please try again.