En helaas gaat de code ook over de 9000 tekens heen! tijd om hem wat schoner te schrijven
Hierbij het laatste deel
void detect_thunder() {
n = analogRead(MIC_PIN); // Raw reading from mic
n = abs(n - 512 - DC_OFFSET); // Center on zero
n = (n <= NOISE) ? 0 : (n - NOISE); // Remove noise/hum
vol[volCount] = n; // Save sample for dynamic leveling
if(++volCount >= SAMPLES) volCount = 0; // Advance/rollover sample counter
total = 0;
for(int i=0; i<SAMPLES; i++) {
total += vol[i];
}
// If you're having trouble getting the cloud to trigger, uncomment this block to output a ton of debug on current averages.
// Note that this WILL slow down the program and make it less sensitive due to lower sample rate.
/*
for(int t=0; t<SAMPLES; t++) {
//initial data is zero. to avoid initial burst, we ignore zero and just add the current l
Serial.print("Sample item ");
Serial.print(t);
Serial.print(":");
Serial.println(vol[t]);
}
Serial.print("total");
Serial.println(total);
Serial.print("divided by sample size of ");
Serial.println(SAMPLES);
Serial.print("average:");
Serial.println(average);
Serial.print("current:");
Serial.println(n);
*/
average = (total/SAMPLES)+2;
if(n>average){
Serial.println("TRIGGERED");
reset();
//I've programmed 3 types of lightning. Each cycle, we pick a random one.
switch(random(1,3)){
//switch(3){
case 1:
thunderburst();
delay(random(10,500));
Serial.println("Thunderburst");
break;
case 2:
rolling();
Serial.println("Rolling");
break;
case 3:
crack();
delay(random(50,250));
Serial.println("Crack");
break;
}
}
}
// utility function to turn all the lights off.
void reset(){
for (int i=0;i<NUM_LEDS;i++){
leds[i] = CHSV( 0, 0, 0);
}
FastLED.show();
}
void acid_cloud(){
// a modification of the rolling lightning which adds random colour. trippy.
//iterate through every LED
for(int i=0;i<NUM_LEDS;i++){
if(random(0,100)>90){
leds[i] = CHSV( random(0,255), 255, LUMENS);
}
else{
leds[i] = CHSV(0,0,0);
}
}
FastLED.show();
delay(random(5,100));
reset();
//}
}
void rolling(){
// a simple method where we go through every LED with 1/10 chance
// of being turned on, up to 10 times, with a random delay wbetween each time
for(int r=0;r<random(2,10);r++){
//iterate through every LED
for(int i=0;i<NUM_LEDS;i++){
if(random(0,100)>90){
leds[i] = CHSV( 0, 0, LUMENS);
}
else{
//dont need reset as we're blacking out other LEDs her
leds[i] = CHSV(0,0,0);
}
}
FastLED.show();
delay(random(5,100));
reset();
}
}
void crack(){
//turn everything white briefly
for(int i=0;i<NUM_LEDS;i++) {
leds[i] = CHSV( 0, 0, LUMENS);
}
FastLED.show();
delay(random(10,100));
reset();
}
void thunderburst(){
// this thunder works by lighting two random lengths
// of the strand from 10-20 pixels.
int rs1 = random(0,NUM_LEDS/2);
int rl1 = random(10,20);
int rs2 = random(rs1+rl1,NUM_LEDS);
int rl2 = random(10,20);
//repeat this chosen strands a few times, adds a bit of realism
for(int r = 0;r<random(3,6);r++){
for(int i=0;i< rl1; i++){
leds[i+rs1] = CHSV( 0, 0, LUMENS);
}
if(rs2+rl2 < NUM_LEDS){
for(int i=0;i< rl2; i++){
leds[i+rs2] = CHSV( 0, 0, LUMENS);
}
}
FastLED.show();
//stay illuminated for a set time
delay(random(10,50));
reset();
delay(random(10,50));
}
}
// basically just a debug mode to show off the lightning in all its glory, no sound reactivity.
void play_sound(){
switch(random(1,10))
switch(random(1,10)){
case 1:
thunderburst();
delay(random(10,500));
Serial.println("Thunderburst");
break;
case 2:
rolling();
Serial.println("Rolling");
break;
case 3:
crack();
delay(random(50,250));
Serial.println("Crack");
break;
}
int thunderDelay = random (500, 3000); // Min. and max. delay between flashing and playing sound
int thunderFile = random (1, 17); // There are 17 soundfiles: 0001.mp3 ... 0017.mp3
int loopDelay = random (5000, 30000); // Min. and max. delay between each loop
Serial.print(F("Pausing before playing thunder sound, milliseconds: "));
Serial.println(thunderDelay);
delay(thunderDelay);
Serial.print(F("Playing thunder sound, file number: "));
Serial.println(thunderFile);
myDFPlayer.playMp3Folder(thunderFile);
delay(1000); // Give the DFPlayer some time
while (digitalRead(busyPin) == LOW) { // Wait for the DFPlayer to finish playing the MP3 file
}
Serial.print(F("Pausing before next loop, milliseconds: "));
Serial.println(loopDelay);
delay(loopDelay);
}
void constant_lightning(){
switch(random(1,10)){
case 1:
thunderburst();
delay(random(10,500));
Serial.println("Thunderburst");
break;
case 2:
rolling();
Serial.println("Rolling");
break;
case 3:
crack();
delay(random(50,250));
Serial.println("Crack");
break;
}
}