Halloween 2022 - Der Süßigkeitenwächter - AZ-Delivery

Halloween is coming up soon. We decorate our houses. Dress us with masks so as not to be recognized and scare away evil spirits. Put candles or pumpkins to attract good spirits and, above all, we have to decide whether we give sweets or acids.

After we in the First Halloween contribution for this year by Bernd Albrecht Played with similar components creepy sounds played and a dummy "flying", we show another idea here to make your Halloween evening a little scary.

With this project we will develop a gadget that sells evil spirits and at the same time has a little surprise for all the brave people who want to take some sweets with them. You have to overcome your fear of the guard of sweets that will try to hypnotize her with his eyes.

When we approach the sweets, the interior of the coffin lights up, and music sounds. This should not continue to warn us. When we don't listen to it, the lid and the guard wakes up, his eyes light up. When we stretch out our hands to take some sweets, the guard gets up and tries to hypnotize us with his eyes.

Required components and materials

1

Nano V3.0 ATMEGA328 CH340 Finished


Jumper Wire cable 3 x 40 pcs. 20 cm M2M / F2M / F2F each

2

SG90 Micro Servo Motor 9G (alternative here) or MG90S micro servo engine

1

Mini Breadboard 400 PIN

1

HC-SR04 Ultrasound Module Remanders (alternative here)

1

Ky-006 Passiver Piezo Buzzer Alarm Module

2

Red LEDs

1

White LED

3

220 ohm resistors


External power supply (5V)


3mm plywood panels

Alternatively: 3D printer


Required software

Arduino IDE

skeleton_guardian_candies.ino

Servomotor library (integrated Servo.h)

Circuit and description of the modules used

Download of the circuit diagram

As we can see in the component plan, we use the distance to measure the distance Ultrasonic sensor HC-SR04. For sound reproduction we will use the Passive buzzer module , the two Servomotors SG-90 (or alternatively MG90S) are used to open the coffin lid and to raise the skeleton. With the White LED Let's illuminate the interior of the coffin. The two Red LEDs We use for the eyes of the skeleton. The Pre -resistances with 220 ohms do not forget. We control all of this with that Nano V3. 0 CH340 microcontroller. As an external power supply, I chose two 5-volt batteries switched into series for the microcontroller. One of them provides the other modules. The VIN input of the Nanos should be fed with more than 5V because a voltage controller is installed there.

3D printing instead of wooden panels

If you have a 3D printer, you can also print the housing. The user Pontifex has one Model via Thingiverse made available. Thanks at this point!

Image source: https://www.thingiverse.com/thing:5584885

YouTube video: https://www.youtube.com/shorts/7WrrfCwW81I

Source: Pontifex Maximus

Description of the program flow and the source code

The ultrasonic sensor HC-SR04 sends impulses with a duration of 10 microseconds and a waiting time between pulse and pulse of 5 microseconds. If the impulse is reflected on and returned to a person or object, the sensor recognizes the received signal. The microcontroller carries out the calculations based on the WEG-Zeit law. Since we know the speed of sound, we get the distance. As long as the distance is larger or 150 centimeters, the coffin does not make any changes. If an obstacle between 149 and 101 centimeters is recognized, the white LED lights up inside the coffin and a melody sounds. If you approach and position yourself at a distance between 100 and 31 centimeters, the coffin opens with the help of one of the servomotors, and the melody stops playing and wakes up the skeleton. His red eyes shine with the help of the two LEDs of the same color. If we try to lift a candy at a distance of 30 centimeters or less, the skeleton will get up with the other servo motor, stretch out its arms, and start pulsating its eyes. All of these steps are carried out continuously when we remain at the appropriate distance. Let's take a look at the sketch.

To use the servo engine, we have to add his library at the beginning of the sketch. In this project, we will use the ultrasonic sensor without the library. We will see that we can determine the distance to the objects with a simple mathematical operation so that we only add the library for the servomotors. This is already included in the Arduino IDE.

#include <Servo.H>

In order to use the servo engines, we are instilling a servo object for each servo motor.

Servo servo_cover;
Servo servo_skeleton;

We declare two variables in the next two lines of the sketch. The first variable contains PIN 5 of the microcontroller. The second variable is used for the eye effect. There the PWM value of the pin to which the red LEDs are connected is saved.

intimately LED_EYES = 5;
intimately LED_Untensity = 0;

In the following line of the code, declare and define the variable for PIN 7 of the microcontroller to which the white LED is connected.

intimately White_Led = 7;

To use the buzzer module, we have to declare and define a variable with pin 6.

intimately tonepin = 6;

The last four lines before the Setup () method are devoted to the ultrasound module. The first two are the variables with the names of the pins, trigpin (signal transmitter), and echopin (signal receiver) of the sensor with the number of pins 4 or 2 of the microcontroller. In the other two variables, we each store the value of the time in which the sensor recognizes the signal that has bounced off an object since it has been sent out, and the value of the distance that we calculate from the recognized time.

intimately trigpin = 4;
intimately echopin = 2;
long ultrasonic_time_measurement;
long spacer;

Now the following follows set up()-Method. With the first two lines, we initialize the serial monitor and indicate a text message.

Serial.Begin (9600);
Serial.print ("Halloween 2022");

In the next four lines, we configure the servo motors for the coffin lid and the skeleton. First, the PIN 9 of the microcontroller, to which we connect the signal of the servo motor of the lid. In the second line, we configure the starting position in degrees. The box is closed at 80. In the following lines, we configure the servo motor of the skeleton. We connect the signal of the servo engine to PIN 10 of the microcontroller and we configure it as a starting position to 90 degrees so that the skeleton is below.

servo_cover.Attach(9);
servo_cover.write(80);
servo_skeleton.Attach(10);
servo_skeleton.write(90);

In the following lines we configure the pins for the red and white LEDs as an output and switch off.

pin mode (LED_EYES, OUTPUT);
Analogwrite(LED_EYES, 0);
pin mode (White_Led, OUTPUT);
digital (White_Led, Low);

In the last two lines of the setup ()-We configure the ultrasonic sensor. The trigpin Is an output that echopin is an entrance spin. The trigger signal is sent out, the Echo signal is received.

pin mode (trigpin, OUTPUT);
pin mode (echopin, Input);

So much to set up()-Method, now we start with the next method that is continuously executed, Loop (). The first line is a call to the method Measure_Distance (). This calculates the removal and stores it in the associated variables.

Measure_Distance();

Four IF blocks follow as a condition. They carry out various actions, depending on the distance in which there is an obstacle recognized by the sensor. The first block is carried out when the distance variable has a value larger than 150 centimeters. The commands that are carried out are:

  • Place the microcontroller pin with the white LED on a low state
  • Set the PWM signal of the microcontroller pins of the red LEDs to 0 so that they are also off
  • position the servo engine of the skeleton at 90 degrees (lying down)
  • position the servo engine of the lid in 80 degrees (closed)

I installed a delay of 200 milliseconds between the two servomotors so that the movement of the skeleton does not collide with that of the coffin lid.

IF (spacer >= 150) { digital (White_Led, Low);
                       Analogwrite(LED_EYES, 0);
                       servo_skeleton.write(90);
                       delay(200);
                       servo_cover.write(80);
                     }

The following conditional block is carried out when the distance stored in the distance variables is between 149 and 101 centimeters. In this case, the following functions are carried out:

  • The PWM signal of the red LED pin is left to 0 (completely switched off)
  • The servomotors are left as in the previous state
  • The skeleton lies (servo engine to 90 degrees)
  • The lid is closed (servomotor to 80 degrees)

The difference to the previous block is the white luminous LED. We rely on high. We also lead that Play_Music ()-S method to play a melody. This method is described later.

IF (spacer < 150 && spacer > 100) { Analogwrite(LED_EYES, 0);
                                 servo_skeleton.write(90);             
                                 servo_cover.write(80);                                                                   
                                 digital (White_Led, HIGH);
                                 Play_Music();
                                      }

If the distance variable has a value between 100 and 31 centimeters, the third block described below is executed. The white LED continues in this block, the servo motor of the skeleton remains 90 degrees so that it remains. The servomotor of the coffin lid is positioned at 180 degrees and thus opened. With the last line we switch on the red LEDs by handing over the PWM value to 150.

IF (spacer <= 100 && spacer > 30) { digital (White_Led, HIGH);
                                        servo_skeleton.write(90);            
                                        servo_cover.write(180);
                                        Analogwrite (LED_EYES, 150);          
                                      }

The last block is executed when the variable spacer has a value of 30 or less. In this case, the following actions are carried out:

  • The white LED continues
  • The skeleton is placed in a 20-degree position and is therefore put down
  • The coffin lid remains open at 180 degrees
  • The method of hypnotizing_led_eyes () is carried out, which slowly reduces and increases the luminosity of the red LEDs. This method is described below.
IF (spacer <= 30) { digital (White_Led, HIGH);
                      servo_skeleton.write(20);
                      servo_cover.write(180);
                      Hypnotizing_led_eyes();
                    }

So much for the method Loop (), Now we want to explain the three methods that we call from there.

The first method is the one who measures the time that the thrown signal needs to reach the sensor. The calculations are carried out in order to obtain the distance. In order to measure the distance between an object and the sensor, we simply have to measure the time that passes between sending and receiving a signal. To perform this process, we have to first trigpin of the sensor for 5 milliseconds to put it into low-condition, then the same pin (trigpin) For 10 milliseconds, put it into high state in order to send the signal and then put it back into the low state so as not to send a signal.

If this has happened, we can use the function Pulse in (Echopin, High) Determine the time that has passed since the pin trigpin the signal sent out and the PIN echopin of the sensor received the rebounded signal. Now we know the time that needs the signal in microseconds. The latter is very important and we save it in the variable ultrasonic_time_measurement. To determine the distance, we only have to carry out a mathematical operation. We know that the speed of sound in the air is 343 m/s at 20 degrees Celsius. We have to convert this speed into cm/µs, i.e.:

The time measured by the sensor is the time when the signal goes back to the object and back. The time until the obstacle is only half of the measured time, which is specified in microseconds:

We get the distance to the object in centimeters and is in the variable spacer Stored that we use in the conditional IF blocks to carry out the corresponding block depending on your value. This distance is also displayed by the serial monitor if the microcontroller is connected to the Arduino IDE.

void Measure_Distance() {
      digital(trigpin, Low);
      Delaymicroseconds(5);
      digital(trigpin, HIGH);
      Delaymicroseconds(10);
      digital(trigpin, Low);

      ultrasonic_time_measurement = pulse(echopin, HIGH);

      spacer = (ultrasonic_time_measurement/2)*0.0343;

      Serial.print(spacer);
      Serial.print("Cm");
      Serial.print(); 
} 

The next method Hypnotizing_Led_eyes ()is responsible for the effect of the eyes of the skeleton. This increases and reduces the luminosity of the LEDs. This achieves pulsation of the LEDs. We use nested loops. The first loop is a counter who repeats everything four times. The first loop within the meter begins with a PWM value of 150 and gradually goes back to a value of 10, which reduces the brightness of the LEDs. The next loop performs the opposite by starting with a PWM value of 10 and slowly increasing it up to a value of 150. Pulsating the LEDs blocks the remaining program. In order to program a non-blocking variant, we would have to change the code.

void Hypnotizing_led_eyes() {
       for (intimately n=0; n<4; n++){                                                    
              for (LED_Untensity = 150; LED_Untensity >=10; LED_Untensity --) {         
                     Analogwrite(LED_EYES, LED_Untensity);
                     delay(15);
               }
              for (LED_Untensity = 10; LED_Untensity <=150; LED_Untensity ++) {         
                     Analogwrite(LED_EYES, LED_Untensity);
                     delay(10);    
              }
       }
}

Now all that remains is the last method that is responsible for playing the melody. In this method we only call the commands tone (pin, frequency, duration) and Delay () on. As a parameter, the sound function has the PIN to which we connected the buzzer, the frequency of the tone played, and the time the sound is played.

void Play_Music() {
    tone(tonepin, 261, 117.1875);
    delay(130.208333333);
    delay(376.041666667);
    tone(tonepin, 311, 116.25);
    delay(129.166666667);
    delay(362.5);
    
    ...
    
    tone(tonepin, 293, 87.1875);
    delay(96.875);
    delay(167.708333333);
    
}

Here you can still download the templates for the templates:

Sarges' construction plan

skeleton

We wish you a creepy beautiful Halloween evening.




Für arduinoSensorenSpecials

10 comments

Miguel Torres

Miguel Torres

Thank you very much Thomas Zenk, we are glad to hear that you liked the project.

Thomas Zenk

Thomas Zenk

Sehr originelle Idee! Das Projekt lässt sich mit Teilen aus den AZ-Delivery Sets adhoc “aus der Bastelkiste” umsetzen. Besonderen Respekt für die Arbeit des Projekt-Teams. Die Mechanik ist super liebevoll und einfallsreich umgesetzt und die IT-Seite wie immer top!! Die 3-D-Druck-Dateien sind noch der Hammer oben drauf. Schon beim Zusammenbasteln hab ich Gänsehaut bekommen. Nur der Ghost-Node in Node Red bindet die Aktoren noch nicht ein. Geht vermutlich erst morgen.

Andreas Wolter

Andreas Wolter

@Pontifex: sieht gut aus. Habe den YT Link mit in den Beitrag eingefügt. Oben in der Teileliste habe ich den MG90S als alternative angegeben. Die kleinen blauen sollten dafür allerdings auch reichen.

Grüße,
Andreas Wolter
AZ-Delivery Blog

Pontifex

Pontifex

Mein Sarg-Projekt ist abgeschlossen. Habe es mit meiner Eigenkonstruktion eines ESP8266 Micro-Boards verdrahtet, einen MP3-Player eingebaut, Batteriebetrieb ermöglicht und unendlich viel dran rumprogrammiert. Langsame Servobewegung fand ich wichtig und sogar eine Fernsteuerung per RemoteXY-Lib ist dabei. Was man nicht so alles in schlaflosen Nächten macht…..
Der Prozessor ist dieser hier:
https://www.az-delivery.de/products/esp8266-12e
Und ich empfehle diese besseren Servos anstatt der elenden blauen asiatischen Schmorkandidaten:
https://www.az-delivery.de/products/mg90s-micro-servomotor
Ah, und der Player:
https://www.az-delivery.de/products/mp3-player-modul

Werbung Ende * :-D

Hier ein Video des Sarges in Action (must see!):
https://www.youtube.com/shorts/7WrrfCwW81I

Auf Thingiverse sind die Files jetzt in endgültiger Version und ein paar Fotos der Verdrahtung sind auch dabei. Wenn Interesse an dem Micro-Board besteht kann ich die Gerber-Files mal auf GitHib posten.
Danke nochmal an euch Blogger für den kontinuierlichen Input! Hat wieder wahnsinnig Spaß gemacht.
Happy Halloween.

Andreas Wolter

Andreas Wolter

@Pontifex: danke, dass Sie das Modell zur Verfügung stellen. Ich habe dafür einen weiteren Absatz eingefügt.

Grüße,
Andreas Wolter
AZ-Delivery Blog

Pontifex

Pontifex

Wunderbar! Genau das Richtige für die Feierabende in dieser Woche (und für meine neue PCB-Konstruktion).
Ich habe das Ganze selbstverständlich als 3D-Druck konstruiert. Wer einen 3D-Drucker hat, wird sich ja kaum auf Sperrholz einlassen.
Die Dateien liegen hier:
https://www.thingiverse.com/thing:5584885
Ich mache den Sarg in Schwarz und das Skelett aus nachleuchtendem Material, die LED wird blau oder Ultraviolett.
Die Konstruktion ist noch nicht ganz getestet. Bitte auf den Text in der Beschreibung achten. Wenn da nichts mehr von “Work in Progress” steht, ist es ausgereift. Müsste aber so gehen wie es da ist. Bilder und Videos folgen bis Ende Oktober.

Ralf Hartmann

Ralf Hartmann

Leider spielen bei beiden Halloween Projekten die Videos nicht ab.
Ansonsten, tolles Projekt!

Andreas Wolter

Andreas Wolter

@Herbert Küstler: in einigen Fällen gibt es PDFs zu den Projekten. Da es aber mittlerweile sehr einfach ist, die Seite aus dem Browser heraus als HTML oder PDF zu speichern, haben wir das in den meisten Fällen nicht mehr gemacht.

Grüße,
Andreas Wolter
AZ-Delivery Blog

Herbert Küstler

Herbert Küstler

Ist es möglich den kompletten Blog als *.pdf zur Verfügung zu stellen?

Philipp Landhäußer

Philipp Landhäußer

Super Projekt und danke für die tolle Anleitung.
Werde das mit meinen Neffen nachbauen.

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. ESP32 jetzt über den Boardverwalter installieren - AZ-Delivery
  2. Internet-Radio mit dem ESP32 - UPDATE - AZ-Delivery
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1 - AZ-Delivery
  4. ESP32 - das Multitalent - AZ-Delivery