/* Code by Siddharth Agarwal, 30/05/2024 This code was made to give a "Lightning effect" for the Final Project of Siddharth Agarwal, "A Witch's Atelier". All rights reserved. Replication, modification, distribution, or display of this work for any purpose requires proper accreditation to the original creator, Siddharth Agarwal. */ int ledPin = D7; // LED connected to digital pin D7 void setup() { // nothing happens in setup } void loop() { int p = 0; //number of times the lightning effect will take place in a loop is 3. for (p = 0; p <= 2; p++) { // we determine this in the loop statement (p=0; p<=2; p++) delay(random(8000, 12000)); //Before each lightning occurs, there is a 8 to 12 second delay. analogWrite(ledPin, 255); //The LED shines bright suddenly delay(100); //but it is short lived (only 0.1 seconds of brightness) analogWrite(ledPin, 0); //LED goes off delay(50); //It stays off for 0.05 seconds // for (int fadeValue = 0; fadeValue <= 255; fadeValue += 5) { analogWrite(ledPin, 255); // The second pulse of the same lightning. LED goes bright delay(50); for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 5) { // This time, it fades out from max to min in increments of 5 points: // sets the value (range from 255 to 0): analogWrite(ledPin, fadeValue); delay(10); // The dimming also occurs quickly } analogWrite(ledPin, 0); //Now, the LED remains off for 0.5 seconds and then the Lightning cycle repeats. delay(500); } // End of Lightning cycle, which occurs 3 times due to the for loop. for (int fadeValue = 0; fadeValue <= 255; fadeValue++) { // After the lightning cycle, we have the LED fade in very slowly. // sets the value (range from 0 to 255): analogWrite(ledPin, fadeValue + random(-100, 100)); //The random here contributes to the "flicker" of the LED as it gets brighter delay(10); } analogWrite(ledPin, 255); //Finally, the LED stays ON at max for 5 seconds before turning off. delay(5000); analogWrite(ledPin, 0); }