Hello people!
I'm into doing a halloween project, where I have a HC-SR04 distance sensor to activate this:
First dc motor (connected to L298N) should run for 2 second (sliding a curtain away)
Then a LED light should blink with strobe effect and a scream from sd card play for about 6 seconds (or as long as there is motion in HC-SR04 area)
My first code, with PIR motion sensor, and only strobe blink and scream sound is this and working for 6 seconds:
const byte led = 2; // LED pin
const byte sensor = 9; // PIR sensor pin
bool isActivated = false;
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
const unsigned long pauseTime = 6000; // how long between PIR detections
const unsigned long blinkTime = 100; // how fast to blink led
unsigned long startTime; // time PIR triggered
unsigned long lastTime; // last time we toggled led
void setup() {
pinMode(led, OUTPUT); // set led pin as output
pinMode(sensor, INPUT); // set sensor pin as input
mySoftwareSerial.begin(9600);
Serial.begin(115200);
Serial.println(F("\nInitializing DFPlayer..."));
//Use softwareSerial to communicate with MP3
if (!myDFPlayer.begin(mySoftwareSerial)) {
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true);
}
Serial.println(F("DFPlayer Mini online."));
//Set volume value (From 0 to 30)
myDFPlayer.volume(5);
}
void loop() {
if ( isActivated ) {
// blink led
if ( millis() - lastTime >= blinkTime ) {
lastTime = millis();
digitalWrite(led, !digitalRead(led));
}
// check if we have been active long enough
if ( millis() - startTime >= pauseTime ) {
isActivated = false; // force PIR to trigger again or not
}
}
if ( isActivated == false ) {
// check PIR
int pirSensor = digitalRead(sensor);
if (pirSensor == HIGH) {
isActivated = true;
startTime = millis();
Serial.println("Sensor Activated");
Serial.println("DFPlayer Working...");
myDFPlayer.play(1);
}
}
}
Now I want to replace the PIR sensor with a distance sensor, and add the dc motor to run before the blink led/scream sound, and then the dc motor run reverse after the blink/sound have played.
My dc motor code with distance sensor is this (just need to figure out to reverse the HIGH/LOW)
// controlling dc motor with ultrasonic sensor
int trig = 5;// Initialize pin D5 as HC-SR04 trigger pin
int echo = 4;// Initialize pin D5 as HC-SR04 Echo pin
int motorPin1 = 10; //Initialize pin D10 to drive motor1
int motorPin2 = 11;//Initialize pin D11 to drive motor1
int duration = 0;
int distance = 0;
void setup()
{
pinMode(trig , OUTPUT);
pinMode(echo , INPUT);
pinMode(motorPin1 , OUTPUT);
pinMode(motorPin2 , OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig , HIGH);
delayMicroseconds(1000);
digitalWrite(trig , LOW);
duration = pulseIn(echo , HIGH);
distance = (duration / 2) / 28.5 ;// calculate distance in CM
//Serial.println(distance);
if ( distance < 20 ) {
Serial.print(distance);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2 , LOW);
Serial.println("cm MOTOR STOP");
delay(1000);
}
else {
Serial.print(distance);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2 , LOW);
Serial.println("cm MOTOR START");
delay(1000);
}
}
Any guidance to replace the PIR with distance sensor, and merge the dc motor into the first code?
Thank you!
Ps: My english is not so good, so all the "technical english" can be hard to understand, nut i'm trying ![]()