Dumi Loghin
Published © CC BY

Warty Monster

Warty Monster is a "smart" Halloween Pumpkin featuring scary sounds, lights, and cool mist, activated by a proximity sensor and a sonar.

IntermediateFull instructions provided4 hours1,276
Warty Monster

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
PIR Sensor, 7 m
PIR Sensor, 7 m
×1
MG995 Servo Motor
×1
Arduino 4 Relays Shield
Arduino 4 Relays Shield
×1
LED (generic)
LED (generic)
×1
Sound Device
×1
USB Battery Pack
×1

Story

Read more

Schematics

schematics_3SFXWkBo8h.png

Code

Pumpkin.ino

Arduino
#include<Servo.h>

Servo Myservo;                    // cool mist spray servo motor
#define servoPin            5     // servo pin
#define sprayTimerTh        20    // cool mist spray timer threshold
int sprayTimer = 0;               // cool mist spray timer (counter)
#define sprayServoAngle     40    // rotate to 40 degrees
#define sprayServoDuration  1000  // spray for 1000 ms
#define sprayDistanceTh     20    // sonar distance threshold to activate the spray

#define proximityPin  A0      // analogue
#define proximityTh   100     // threshold

#define soundOnPin 8          // sound on relay pin
#define soundTimerTh  10      // how long is the sound on
int soundOn = 0;              // is sound on? (flag)
int soundTimer = 0;           // sound on timer (counter)

#define lightOnPin      7     // light on relay pin
#define lightTimerTh   30     // how long 
int lightOn = 0;              // is light on? (flag)
int lightOnTimer = 0;         // light on timer (counter)

#define echoPin       2       // HC-SR04 sonar echo pin
#define trigPin       3       // HC-SR04 sonar trigger pin


void doSound() {
  int proximityVal;

  // if there is movement, on the sound
  proximityVal = analogRead(proximityPin);
  if (proximityVal > proximityTh) {
    if (soundOn == 0) {
      digitalWrite(soundOnPin, HIGH);
      delay(100);
      digitalWrite(soundOnPin, LOW);
      soundOn = 1;      
      lightOnTimer = 1;
      soundTimer = 0;
    }
  }

  // keep the sound on for "soundTimerTh"
  soundTimer++;
  if (soundTimer > soundTimerTh) {
    soundTimer = 0;
    if (soundOn == 1) {
      digitalWrite(soundOnPin, HIGH);
      delay(100);
      digitalWrite(soundOnPin, LOW);
      soundOn = 0;
    }
  }
}

void doLight() {
  // while the timer is active, keep blinking the light
  if (lightOnTimer > 0) {
    digitalWrite(lightOnPin, lightOn == 0 ? HIGH : LOW);
    lightOn = (lightOn == 0 ? 1 : 0);
    lightOnTimer++;
    if (lightOnTimer == lightTimerTh) {
      lightOnTimer = 0;
    }
  }
}

void doSpray() {
  long duration; // duration of sound wave travel
  int distance;  // sonar distance

  // read the sonar
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2; // speed of sound wave divided by 2 (trip)

  // if the distance is less than 20 cm, spray the mist
  if (distance < 20) {
    if (sprayTimer == 0) {
      Myservo.write(sprayServoAngle);
      delay(sprayServoDuration);
      Myservo.write(0);
      sprayTimer++;
    }
  }
  if (sprayTimer > 0) {
    sprayTimer++;
    if (sprayTimer == sprayTimerTh) {
      sprayTimer = 0;
    }
  }
}

void setup()
{
  pinMode(soundOnPin, OUTPUT);
  digitalWrite(soundOnPin, LOW);
  pinMode(lightOnPin, OUTPUT);
  digitalWrite(lightOnPin, LOW);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  Myservo.attach(servoPin);

  // for debugging
  // Serial.begin(9600);
}

void loop()
{
  doSound();
  doLight();
  doSpray();
  // timer interval is set at 300ms
  delay(300);
}

Credits

Dumi Loghin
3 projects • 6 followers
I am a researcher in Computer Science passionate about building electronics and systems.
Contact

Comments

Please log in or sign up to comment.