Hello All,
Noob here. Last year I created a wooden box that used an Uno to run some programs. It was centered around an ultrasonic sensor providing distance from the box.
Basic Operation:
When nobody was near, 4 Edison lightbulbs (through a relay) would be illuminated. When you came near, Edisions would extinguish and water vapor (relay again) would descend over a laser trip wire (deflected around by mirrors). When you tripped the Laser target, it would flicker the Edison lights like lightning. After, an LED "Big Dipper/Polaris" Constellation would light up in the throat of the "cave" to the candy.
This year I am having trouble with incorporating a new feature into my Halloween box. I wanted to add sound to go along with all of the different features. I am using the DFPlayer Mini to provide input to a 50W Amp ( 24V power supply) to run bigger sound over some 15-20W Speakers. My problem seems to be coming in my programing. It seems that the DFPlayer needs a delay built into the code to run the complete MP3. This is creating my issue. I want music to be playing continuously until somebody approaches. If I delay the loop it isn't checking the distance to set off the next phase of the box. I hope I was able to communicate this so it makes sense. I will post the code below. Thanks for any guidance in advance.
//Halloween 2023 Version
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define Dip 2 //Star
#define Polaris 3 //Star
#define S25 4 //Star
#define S71 5 //Star
#define LAZ 6 //Laser
#define TGT 7 //Tareget for Laser
#define Edison 8 //Relay for Edison lights
#define Fog 9 //Relay for Fog machine
#define MPRX 10 //MP3 RX hookup
#define MPTX 11 //MP3 TX Hookup
#define Echo 12 //Ultrasonic sensor
#define Trig 13 //Ultrasonic Sensor
long duration, distance; //for UltraSonic
unsigned long refTime;
unsigned long trippoint;
const long threshold = 700;
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup() {
mySoftwareSerial.begin(9600);
Serial.begin(9600);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
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."));
myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
myDFPlayer.volume(30);
pinMode (Trig, OUTPUT);
pinMode (Echo, INPUT);
pinMode (Dip, OUTPUT);
pinMode (S25, OUTPUT);
pinMode (S71, OUTPUT);
pinMode (Polaris, OUTPUT);
pinMode (LAZ, OUTPUT);
pinMode (TGT, INPUT);
pinMode (Edison, OUTPUT);
pinMode (Fog, OUTPUT);
digitalWrite (Dip, LOW);
digitalWrite (S25, LOW);
digitalWrite (S71, LOW);
digitalWrite (Polaris, LOW);
}
void loop() {
digitalWrite (Trig, LOW);
delayMicroseconds (5);
digitalWrite (Trig, HIGH);
delayMicroseconds (10);
digitalWrite (Trig, LOW);
duration = pulseIn (Echo, HIGH);
distance = (duration / 2) / 29.1;
Serial.print (distance);
Serial.println ("cm");
if (distance < 140) {
digitalWrite (Fog, HIGH);
digitalWrite (Edison, LOW);
digitalWrite (LAZ, HIGH);
myDFPlayer.play (4); //"Who disturbs my Slumber?"
delay (7000);
}
else {
digitalWrite (Fog, LOW);
digitalWrite (Edison, HIGH);
digitalWrite (LAZ, LOW);
myDFPlayer.play (1); //Play 20's Music till approached
delay (5000);
}
if ((distance < 140) && (digitalRead(TGT) == 0)) {
myDFPlayer.play (5); //Thunder
digitalWrite (Edison, HIGH); //Lightning Sequence
delay (150);
digitalWrite (Edison, LOW);
delay (70);
digitalWrite (Edison, HIGH);
delay (50);
digitalWrite (Edison, LOW);
delay (150);
digitalWrite (Edison, HIGH);
delay (40);
digitalWrite (Edison, LOW);
delay (25);
digitalWrite (Edison, HIGH);
delay (300);
digitalWrite (Edison, LOW);
delay (1500);
myDFPlayer.pause();
delay (500);
digitalWrite (Dip, HIGH);
digitalWrite (S25, HIGH);
digitalWrite (S71, HIGH);
delay (1000);
for (int i = 0; i < 255; i++) {
analogWrite (Polaris, i);
delay (5);
}
for (int i = 255; i > 0; i--) { //Pulsing North Star
analogWrite (Polaris, i);
delay (5);
}
digitalWrite (Dip, LOW);
digitalWrite (S25, LOW);
digitalWrite (S71, LOW);
myDFPlayer.play (6);
delay (500);
for (int i = 0; i < 255; i++) {
analogWrite (Polaris, i);
delay (5);
}
for (int i = 255; i > 0; i--) {
analogWrite (Polaris, i);
delay (5);
}
digitalWrite (Polaris, LOW);
delay (5000);
}
}