A Halloween Suprise

A Halloween Suprise

2023, Oct 31    

Happy Halloween!

The other day, my girlfriend and I were shopping for Halloween decorations and found an excellent decoration we liked. It was a tiny plastic spider, nothing special, but I knew I could do something cool with it the moment I saw it. So, I bought it, and when I arrived home, I started searching for my Arduino stuff to make something.

spider

Making Up The Idea

When I was a child, my dad made a lot of cool stuff with Arduino, and I was always amazed by the things he could do with it. Looking for inspiration, I started searching for my old stuff. But I found some jumper cables, breadboard, Arduino Nano, motors, and sensors. I only have a weekend before Halloween, so I need to make something simple with the things that I have.

materials

I wanted a decoration that could scare people, so I made a spider fall from the ceiling when someone passed by. This project has three requirements. I just need to make the spider:

  1. Detect when someone is near
  2. Fall from the ceiling quickly
  3. Return to the ceiling and wait for the next victim

Making The Spider

Let’s solve the second requirement first because it is the most relevant to do the trick. The best way to make the spider fall fast is to use gravity, so I glue the motor to a servo so I can rotate the motor and make the spider fall. This is the result:

Now, I need to make the spider detect when someone is near. Fortunately, I have a PIR HC-SR501 sensor that can detect when someone is close. I just need to connect the sensor to the Arduino to trigger the rotation of the servo so it can release the spider.

sensor

Finally, to solve the third requirement, I just need to rotate the servo again and start the motor to make the spider return to the ceiling. I would like to find a relay to control the motor. Still, I didn’t find it, so I used a keyboard switch to control the motor, which was not the best solution, but I didn’t have time to find a better solution. This is how the switch works:

As you can see, the motor presses the switch, so I need to rotate the servo a little bit more to push it and start the motor.

The prototype works; just pack everything and wait for the next victim.

final1 final2 final3

As you may have noticed, I added cardboard padding and metal wires to the motor to prevent the spider from getting stuck between the motor and the plastic box. I also added a switch for the motor so it can be turned off when I’m not using it. I used two AA batteries to power the motor and a USB cable to power the Arduino. Now, I just need to put the spider in the ceiling and wait.

Another video from the final prototype:

The code

The code is pretty simple. I just need to read the sensor, rotate the servo when someone is near, and turn the servo again to press the switch and start the motor. I used the Servo library to control the servo and the delay function to control the time between states. Here is the code:

// Include the servo library:
#include <Servo.h>

#define TriggerPin 10
#define ServoPin 9
#define StartTimeMS 5000
#define DropTimeMS 1500
#define CooldownTimeMS 4000
#define ReloadTimeMS  4900
#define ReloadDelayTimeMS  2000
#define TriggerTimeMS 1000
#define TriggerSleepTimeMS 500

#define HoldAngle 90
#define ReloadAngle 110
#define DropAngle 0

// States
enum State { 
  DEBUG,        // Debug the code for calibration purposes
  START,        // Start the spider, reset to a valid initial state
  ARMED,        // Armed and waiting for the trigger
  DROP,         // Release the spider
  COOLDOWN,     // Wait for the spider to scare the victim
  RELOAD,       // Return the spider to the ceiling
};

// State control
State current_state = START;
// State current_state = DEBUG;
 
// Create a new servo object:
Servo servo;


void setup() {
  // Attach the Servo variable to a pin:
  servo.attach(ServoPin);
  // Enable the trigger pin
  pinMode(TriggerPin, INPUT);
  // Debug
  Serial.begin(9600);
}

void loop() {

  switch(current_state) {
  case DEBUG:
    Serial.println("Debuging...");
    delay(500);
    servo.write(90);
    delay(500);
    servo.write(0);
    break;
  case START:
    Serial.println("Starting...");
    servo.write(HoldAngle);
    delay(StartTimeMS);
    current_state = DROP;
    break;
  case DROP:
    Serial.println("Dropping...");
    servo.write(DropAngle);
    delay(DropTimeMS);
    servo.write(HoldAngle);
    current_state = COOLDOWN;
    break;
  case COOLDOWN:
    Serial.println("Cooldown...");
    delay(CooldownTimeMS);
    current_state = RELOAD;
    break;
  case RELOAD:
    Serial.println("Reloading...");
    servo.write(ReloadAngle);
    delay(ReloadTimeMS);
    servo.write(HoldAngle);
    current_state = ARMED;
    Serial.println("Armed and waiting!");
    delay(ReloadDelayTimeMS);
    break;
  case ARMED:
    //Read data from the sensor
    if(digitalRead(TriggerPin) == HIGH) {
      Serial.println("Shoot!");
      delay(TriggerTimeMS);
      current_state = DROP;
    }
    else {
      Serial.println("No one is there");
      delay(TriggerSleepTimeMS);
    }
    break;
  }
}

I used a state machine to control the spider. I’m a Pythonist developer, so I enjoy writing verbose code, which helps others (and me) quickly understand what the code does. I also added a debug state to calibrate the servo angles, press the switch correctly, and release the spider.

The Halloween Surprise

The day arrived, and I was ready to scare some people. Here are some videos of the Halloween decorations:

Conclusion

This was a fun project; I could make something cool with the few things I had in just a weekend. I wonder what I could do with more time and materials; I will have to wait until next Halloween.

I hope you like it, and I hope you have a happy Halloween!