#define MAXDARKTIME 500 //about 5s #define TRIGGERPULSE 90 //about 900ms #define MAXSTORM 9000 //about 90s #define LEDCOUNT 5 //number of LEDs int ledPin[LEDCOUNT]={0,1,6,9,10}; //pins to flash LEDs boolean isStriking=false; //LEDs are flickering int darkTime=0; int triggerPin = 12; //triggers audio int playingPin = 2; //active when thunder is playing int shakePin = 3; //active when shaking is detected int stormCount = 0; //length of storm int triggerCount = 0; //length of trigger pulse int stormInc = 0; //time to add to storm on each shake int onboardLED = 7; void setup() { // initialize LED pins as an output. for(int i=0;i95) digitalWrite(ledPin[i],HIGH); else digitalWrite(ledPin[i],LOW); } else //turn off LEDs { digitalWrite(ledPin[i],LOW); } } } void loop() { //the longer you shake, the longer the storm if(digitalRead(shakePin)==HIGH ) { //increment storm count only if thunder's not playing (thunder shakes the sensor) if (stormCount < MAXSTORM && digitalRead(playingPin)==HIGH) stormCount+=stormInc; stormInc=0; } else { stormInc=500; } if(stormCount>0) { if(darkTime==0) //end of dark interval { if(triggerCountTRIGGERPULSE) darkTime=random(MAXDARKTIME)+10; } else { triggerCount=0; darkTime--; } stormCount--; //if(triggerCount==1) Serial.println(stormCount); //uncomment for debug info (shows storm countdown) } else { isStriking=false; digitalWrite(triggerPin,HIGH); triggerCount = 900; darkTime = 0; } //flicker LEDs while sound is triggered isStriking=digitalRead(playingPin)==LOW&&triggerCount<200; lightning(); delay(10); // wait for a 10ms }