scott mangiacotti
Published © GPL3+

Halloween Scare Lightning Effect

Create a flashing-eyes project with a few LED, an Arduino and a simplified formula to create a lightning simulation

BeginnerFull instructions provided2 hours753
Halloween Scare Lightning Effect

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
LED, Blue
LED, Blue
×2
5 mm LED: Red
5 mm LED: Red
×1
Resistor 220 ohm
Resistor 220 ohm
×3
Slide Switch
Slide Switch
or equivalent on/off switch
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Miscellaneous
Paper towel roll center or rolled sheets of paper, template of eye shapes to cut into the roll, misc. craft stuff
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Breadboard, 170 Pin
Breadboard, 170 Pin
Or equivalent
10 Pc. Jumper Wire Kit, 5 cm Long
10 Pc. Jumper Wire Kit, 5 cm Long
Or equivalent for wiring the circuit on the breadboard

Story

Read more

Custom parts and enclosures

Design Specifications

Zip file containing Fritzing schematic file, eye cut-out template, spreadsheet with design details

Schematics

ol1_schematic_DQefOqH7bE.pdf

Code

Arduino code

Arduino
//Scott Mangiacotti
//West Roxbury, MA USA
//October 2020
//OL1

//Constants
int const BETWEEN_MIN = 1500; //The range of times between lightning strike events
int const BETWEEN_MAX = 3000;
int const DURATION_MIN = 200; //The range of times between flashes during a strike event
int const DURATION_MAX = 700;
int const TIMES_MIN = 4;      //The range of the number of flashes per strike event
int const TIMES_MAX = 10;

int const SW1_PIN = 2;
int const LED1_PIN = 3;
int const LED2_PIN = 4;
int const LED3_PIN = 5;

//Global variables
bool gVerboseDiagMode = false;
bool gEnabled = false;

unsigned long gLastTime = 0;
unsigned long gNextTime = 0;


//Runs once
void setup() 
{
  //Open a serial port
  Serial.begin(9600);

  //Setup digital I/O
  pinMode(SW1_PIN, INPUT);
  pinMode(LED1_PIN, OUTPUT);
  pinMode(LED2_PIN, OUTPUT);
  pinMode(LED3_PIN, OUTPUT);
  
  //Post product information to serial port
  reportProductInfo();
}


//Runs continuously
void loop()
{
  //Serial port message receipt processing
  if (Serial.available() > 0)
  {
    int iControlCode;
    iControlCode = Serial.parseInt();
    processSerialMessage(iControlCode);
  }

  if (digitalRead(SW1_PIN) == HIGH)
  {
    gEnabled=true;
  }
  else
  {
    gEnabled=false;
  }

  if (gEnabled == true)
  {
    lightningStrike();
  }
}


//ilem
void lightningStrike()
{
  unsigned long mils;
  int iRandNext;
  int iFlashes;
  int iDur;

  //Initialize
  mils = millis();

  //Check if it is time for a new flash
  if (mils > gNextTime)
  {
    //Store Arduino rolling timer
    gLastTime = mils;
    
    //here


    //Determine number of flashes for thi strike event
    iFlashes = random(TIMES_MIN, TIMES_MAX);

    if (gVerboseDiagMode==true)
    {
      Serial.print("Clock: ");
      Serial.print(mils);
      Serial.print(", Time until next: ");
      Serial.print(iRandNext);
      Serial.print(", Flashes: ");
      Serial.println(iFlashes);
      Serial.print("Time between: ");
    }

    //Iterate to create the flashes
    for (int i=0; i<iFlashes; i++)
    {
      //Turn on LEDs
      digitalWrite(LED1_PIN, HIGH); //turn LED-1 on
      digitalWrite(LED2_PIN, HIGH); //turn LED-2 on
      digitalWrite(LED3_PIN, HIGH); //turn LED-3 on

      //Wait a randomized length of time (ms)
      iDur = random(DURATION_MIN, DURATION_MAX);

      if (gVerboseDiagMode==true)
      {
        Serial.print(iDur);
        Serial.print(", ");
      }

      //On delay
      delay(iDur);

      digitalWrite(LED1_PIN, LOW);  //turn LED-1 off
      digitalWrite(LED2_PIN, LOW);  //turn LED-2 off
      digitalWrite(LED3_PIN, LOW);  //turn LED-3 off

      //Off delay
      delay(10); //10
    }

    if (gVerboseDiagMode==true)
    {
      Serial.println(" ");
      Serial.println("-----");
    }

    //Determine when next strike event to occur
    iRandNext = random(BETWEEN_MIN, BETWEEN_MAX);
    mils = millis();
    gNextTime = mils + iRandNext;
  }
}


//Process received messages from the serial port interface
//Input parameter iControlCode is the value received from the serial port to be processed
//First two digits are the control command, remaining three are the value to process
void processSerialMessage(int iControlCode)
{
  int iControlCommand;
  int iControlValue;
  
  //Calculate command and value
  iControlCommand = iControlCode / 1000;
  iControlValue = iControlCode % 1000;

  //Report control code
  Serial.print("control code: ");
  Serial.println(iControlCode);

  //Report command
  if (gVerboseDiagMode == true)
  {
    Serial.print("control command: ");
    Serial.println(iControlCommand);
    
    Serial.print("control value: ");
    Serial.println(iControlValue);
  }

  //Misc command category
  if (iControlCommand == 10)
  {
    if (iControlValue == 0)
    { //display app info
      reportProductInfo();
      
    }
    else if (iControlValue == 1)
    { //verbose messaging toggle
      if (gVerboseDiagMode == false)
      {
        gVerboseDiagMode = true;
        Serial.println("verbose messaging enabled");
      }
      else
      {
        gVerboseDiagMode = false;
        Serial.println("verbose messaging disabled");
      }
      
    }
    else if (iControlValue == 2)
    { //system enable toggle
      if (gEnabled == true)
      {
        gEnabled = false;
        Serial.println("System disabled with serial message");
      }
      else
      {
        gEnabled = true;
        digitalWrite(LED1_PIN, LOW);  //shut LED in case was manually turned on previously
        Serial.println("System enabled with serial message");
      }

    }
    else if (iControlValue == 3)
    { //Energize LEDs (only when system disabled)
      if (gEnabled == false)
      {
        digitalWrite(LED1_PIN, HIGH);
        digitalWrite(LED2_PIN, HIGH);
        digitalWrite(LED3_PIN, HIGH);
        Serial.println("LEDs energized manaully");
      }
      else
      {
        Serial.println("Cannot manually energize LEDs when system is enabled");
      }
      
    }
    else if (iControlValue == 4)
    { //De-energize LEDs (only when system disabled)
      if (gEnabled == false)
      {
        digitalWrite(LED1_PIN, LOW);
        digitalWrite(LED2_PIN, LOW);
        digitalWrite(LED3_PIN, LOW);
        Serial.println("LEDs de-energized manaully");
      }
      else
      {
        Serial.println("Cannot manually de-energize LEDs when system is enabled");
      }

    }
    else if (iControlValue == 5)
    { //spare


    }
    else if (iControlValue == 6)
    { //spare

    }
    else if (iControlValue == 7)
    { //spare

    }
    else if (iControlValue == 8)
    { //spare

    }
    else if (iControlValue == 9)
    { //spare     

    }
    else if (iControlValue == 10)
    { //spare

    }
  }

  if (iControlCommand == 11)
  { //spare
    
  }

  if (iControlCommand == 12)
  { //spare
    
  }

  if (iControlCommand == 12)
  { //spare

  }
}


//Send product information to the serial port
void reportProductInfo()
{
  //Report product and other information to serial port
  Serial.println("OL1");
  Serial.println("by Scott Mangiacotti");
  Serial.println("West Roxbury, MA USA");
  Serial.println("October 2020");
}

Credits

scott mangiacotti
9 projects • 19 followers
Contact

Comments

Please log in or sign up to comment.