Halloween Skull - Code
23/10/2021 | Views: 1358 | Robotics | by: ELECTRONOOBS      


Get the code from below together with the needed libraries for the DFplayer and the WS2812 LEDs. Install the libraries and then compile the code. The code is more than easy. We detect if the PIR sensor is active. If yes we activate a “movement detected” variable. While this is active, we make a loop where we play a scary sound and we blink the eyes LEDs with red color. We also move the jaw with the servo. When 9 seconds have passed, we reset the “movement detected” variable and it will wait again to detect movement. That’s it for the code. To move the jaw we must connect the servo to the plastic part. For that I will use some metal wire. Bend it and connect it to the servo in such a way that it will push and pull the jaw. The scary skull is ready for the first test, so let me close the top part and give it a run. By the way I’ve also 3D printed a support, so the skull will stay streight and not touch the table with the jaw.







///////////////////////////////WS2812 LED Strip Library///////////////////////////////
#include <Adafruit_NeoPixel.h>   //Download here: https://www.electronoobs.com/eng_arduino_Adafruit_NeoPixel.php
#ifdef __AVR__
  #include <avr/power.h>
#endif
#define PIN            10         //WS2812 strip is connected on pin D10
#define NUMPIXELS      3          //We have 3 LEDs
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


////////////////////////////////Servo Motor Library///////////////////////////////////
#include <Servo.h>
Servo JAW_SERVO;


////////////////////////////////DFplayer mp3 module///////////////////////////////////
//Mp3 DFplayer libraries
#include <SoftwareSerial.h>
#include <DFMiniMp3.h>    //Downlaod it here:  https://www.electronoobs.com/eng_arduino_DFPlayer_Mini_Mp3_by_Makuna.php

class Mp3Notify
{
public:
  static void OnError(uint16_t errorCode) {}
  static void OnPlayFinished(uint16_t globalTrack){}
  static void OnCardOnline(uint16_t code){}
  static void OnCardInserted(uint16_t code){}
  static void OnCardRemoved(uint16_t code){}
};

DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial);
//////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////Input & Output pins///////////////////////////////////
int SMOKE_PIN = 6;
int SERVO_PIN = 5;
int PIR_PIR = 2;


/////////////////////////////////////VARIABLES////////////////////////////////////////
bool PIR_STATE = false;
bool movement_detected = false;
unsigned int Millis = 0;
unsigned int Millis_previous = 0;
int SERVO_CLOSE = 70;
int SERVO_OPEN = 110;


void setup() {
  pinMode(SMOKE_PIN, OUTPUT);                             //Set smoke machine pin to OUTPUT
  digitalWrite(SMOKE_PIN, LOW);                           //Set smoke machine MOSFETs to OFF
  pinMode(PIR_PIR, INPUT);                                //Set PIR module pin as input

  JAW_SERVO.attach(SERVO_PIN);                            //Attatch servo on pin D9
  JAW_SERVO.write(SERVO_CLOSE);                           //Set servo on closed jaw position
  pixels.begin();                                         //This initializes the NeoPixel library.

  //Mp3 DFplayer module setup
  Serial.begin(9600);                                     //Start serial port
  mp3.begin();                                            //Start DFPlayer communciation
  mp3.setVolume(30);                                      //Set Volume to 30 which is maximum 
  delay(100); 
  mp3.playMp3FolderTrack(2);                              //Play 0002.mp3 file from the SD card (robotic welcome)
  pixels.setPixelColor(0, pixels.Color(255,255,255));     //Turn WHITE left eye LED
  pixels.setPixelColor(1, pixels.Color(255,255,255));     //Turn WHITE right eye LED
  pixels.show();                                          //Send LED data to the strip
  delay(4000);                                            //Small delay so the sound could end  
  pixels.setPixelColor(0, pixels.Color(255,0,0));         //Turn RED left eye LED
  pixels.setPixelColor(1, pixels.Color(255,0,0));         //Turn RED right eye LED
  pixels.show();                                          //Send LED data to the strip
  delay(1000);
  pixels.setPixelColor(0, pixels.Color(0,0,0));           //Turn off left eye LED
  pixels.setPixelColor(1, pixels.Color(0,0,0));           //Turn off right eye LED
  pixels.show();                                          //Send LED data to the strip
  Millis = millis();                                      //Store actual time in milliseconds
  Millis_previous = Millis;                               //Previous time equal to actual time in milliseconds
}

void loop() {
  //Detect when PIR sensor us active (HIGH)
  if(digitalRead(PIR_PIR) && !PIR_STATE){
    PIR_STATE = true;                                     //Set PIR_STATE to true
    movement_detected = true;                             //Set movement_detected to TRUE
    digitalWrite(SMOKE_PIN, HIGH);                        //Turn OFF the smoke machine MOSFETs  
    mp3.playMp3FolderTrack(1);                            //Play 0001.mp3 file from the SD card (evil laugh)  
    Millis_previous =  millis(); ;                        //Get previous time in milliseconds
  }
  //Reset state when PIR is LOW
  else if(!digitalRead(PIR_PIR) && PIR_STATE){
    PIR_STATE = false;                                    //Reset PIR module state
  }

  //If movement was detected we run the loop below
  if(movement_detected){    
    Millis = millis();                                    //Get actual time in milliseconds
    
    //We turn OFF the smoke MOSFETs after 3000 milliseconds
    if(Millis - Millis_previous > 3000){
      digitalWrite(SMOKE_PIN, LOW);                       //Turn OFF smoke machine MSOFETs
    }
    
    //We reset the movement_detected varfiable after 9000 milliseconds
    if(Millis - Millis_previous > 9000){
      movement_detected = false;                          //Reset movement_detected variable
      pixels.setPixelColor(0, pixels.Color(0,0,0));       //Turn off left eye LED
      pixels.setPixelColor(1, pixels.Color(0,0,0));       //Turn off right eye LED
      pixels.setPixelColor(2, pixels.Color(0,0,0));       //Turn OFF jaw LED 
      pixels.show();
    }
    
    int random_number = random(0,8);                      //We add a random delay in order to move the jaw more real
    random_number = random_number * 10;
    pixels.setPixelColor(0, pixels.Color(255,0,0));       //Turn on red color for left eye LED
    pixels.setPixelColor(1, pixels.Color(255,0,0));       //Turn on red color for right eye LED 
    pixels.setPixelColor(2, pixels.Color(255,255,255));   //Turn WHITE jaw LED 
    pixels.show();                                        //Send LED data to the strip
    JAW_SERVO.write(SERVO_OPEN);                          //Turn servo at 110 degrees
    delay(100+random_number);                             //Add the delay
    
    pixels.setPixelColor(0, pixels.Color(0,0,0));         //Turn off left eye LED
    pixels.setPixelColor(1, pixels.Color(0,0,0));         //Turn off right eye LED
    pixels.setPixelColor(2, pixels.Color(255,255,255));   //Turn WHITE jaw LED 
    pixels.show();                                        //Send LED data to the strip
    JAW_SERVO.write(SERVO_CLOSE);                         //Turn servo at 70 degrees
    delay(100+random_number);                             //Add the delay         
  }//end of movement_detected
  
  
}//end of void loop













Last tutorials

Powerful Coilgun PCB + multiple stages
Halloween 3D Printed Skull with Smoke Machine
Arduino Thermistor Temperature read
Homemade Reflow Hot Plate V2 - Better?
Arduino Bluetooth Nunchuch - Balancing Robot or anything else

ADVERTISERS



ADVERTISERS

PCBWAY PCB service

PCBWAY PCB service





Curso Arduino Online nivel Intermedio