Hello, I'm trying to create a project where the rainbow rgb lights and mp3 music files start at the same time only when my sound sensor detects my clapping. It works most of the time, but 3 out 5 times I get a "-1" error in my Serial Monitor, the lights do turn on but there's no music playing. I have to clap again for it to move on to the next random song. I can't figure out what is causing this issue. Can anyone help me, please?
I use a Arduino nano for this project.
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
#include <FastLED.h>
#include "SoftwareSerial.h"
#include <SPI.h>
#include <TimeLib.h>
#define NUM_LEDS 11
#define LED_PIN 2
CRGB leds[NUM_LEDS];
//has to be uint8_t so it starts at 0 after it reached 256
uint8_t hue = 0;
unsigned long lastIncrement = 0;
int SOUND_SENSOR = 3;
int RELAY = 2;
int clap = 0;
unsigned long detection_range_start = 0;
unsigned long detection_range = 0;
boolean ledsAreOn = false;
boolean clap_status = true; //true = plays a random mp3 file when arduino booted
int effectSpeed = 1;
int effectWide = 5;
/// mp3 player
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
//int files = 0;
//int durationA;
//int distance;
#define pinDFBusy 12
//unsigned long sonar_time = 0;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
long randNumber;
int result;
//int ultrasonic_flag = 0;
// char fileName[20];
// int fileToPlay;
void setup()
{
pinMode(SOUND_SENSOR, INPUT);
pinMode(RELAY, OUTPUT);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(50);
// Serial.begin(115200);
// randomSeed(analogRead(0));
setupMP3();
// triggerStart();
}
void setupMP3()
{
// pinMode(13, OUTPUT);
pinMode(pinDFBusy, INPUT); // init Busy pin from DFPlayer (lo: file is playing / hi: no file playing)
randomSeed(analogRead(0)); //random
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
currentMillis = millis();
previousMillis = currentMillis;
// attachInterrupt(digitalPinToInterrupt(echoPinA), EchoPinA_ISR, CHANGE); // Pin 2 interrupt on any change
mySoftwareSerial.begin(9600);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
myDFPlayer.setTimeOut(1000); //Set serial communication time out 500ms
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(1000); //Set serial communication time out 500ms
myDFPlayer.volume(10); //Set volume value (0~30).
myDFPlayer.EQ(DFPLAYER_EQ_BASS);
// myDFPlayer.EQ(DFPLAYER_EQ_POP);
// myDFPlayer.EQ(DFPLAYER_EQ_ROCK);
// myDFPlayer.EQ(DFPLAYER_EQ_JAZZ);
// myDFPlayer.EQ(DFPLAYER_EQ_CLASSIC);
// myDFPlayer.EQ(DFPLAYER_EQ_BASS);
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
//----Mp3 control----
// myDFPlayer.sleep(); //sleep
// myDFPlayer.reset(); //Reset the module
myDFPlayer.enableDAC(); //Enable On-chip DAC
// myDFPlayer.disableDAC(); //Disable On-chip DAC
//myDFPlayer.outputSetting(true, 15); //output setting, enable the output and set the gain to 15
//----Read information----
Serial.println(myDFPlayer.readState()); //read mp3 state
Serial.println(myDFPlayer.readVolume()); //read current volume
Serial.println(myDFPlayer.readEQ()); //read EQ setting
Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
//Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
//Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read file counts in folder SD:/03
}
boolean isPlaying = false;
void loop(){
if (digitalRead(pinDFBusy) == LOW && isPlaying == false)
{
// when busy is low, means is playing
isPlaying = true;
}
else if (digitalRead(pinDFBusy) == HIGH && isPlaying == true)
{
// when busy is HIGH means it finished
isPlaying = false;
ledsAreOn = false;
Serial.println(("-- SHOULD TURN OFF LEDS"));
}
checkClap();
playMusic();
//if (tmrpcm.isPlaying() == 1)
//{
//You can change the pattern speed here
if (millis() - lastIncrement > 20 && ledsAreOn)
{
lastIncrement = millis();
hue += effectSpeed;
for (int i = 0; i < NUM_LEDS; ++i) {
leds[i] = CHSV(hue + (i * effectWide), 255, 255);
}
// ledsAreOn = true;
FastLED.show();
}
else
{
if (ledsAreOn == false)
{
ledsAreOn = false;
for (int i = 0; i < NUM_LEDS; ++i)
{
leds[i] = CRGB::Black;
}
FastLED.show();
}
}
if (myDFPlayer.available()) {
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
}
}
void playMusic()
{
if (clap_status == true)
{
clap_status = false;
randNumber = random(1,9);
Serial.println(randNumber);
myDFPlayer.playLargeFolder(1, randNumber); //play specific mp3 in SD:/01/0004.mp3; Folder Name(1~10); File Name(1~1000)
Serial.println(myDFPlayer.readCurrentFileNumber());
// do
//{
//Serial.println("low");
//ledsAreOn = false;
//Serial.println(myDFPlayer.readState());
// ledsAreOn = true;
///}
///while (digitalRead(pinDFBusy) == LOW);
}
}
void triggerStart()
{
// todo choose effects
// fileToPlay = random(1, 10);
Serial.println(("-- SHOULD START MUSIC"));
effectSpeed = random(-10, 10);
effectWide = random(0, 10);
ledsAreOn = true;
clap_status = true;
// todo - choose song
// todo - play song
// sprintf(fileName, "%d.wav", fileToPlay);
// tmrpcm.play(fileName);
}
//void EchoPinA_ISR() {
// static unsigned long startTimeA;
//
// if (digitalRead(2)) // Gone HIGH
// startTimeA = micros();
// else // Gone LOW
// LastPulseTimeA = micros() - startTimeA;
//}
void checkClap()
{
int status_sensor = digitalRead(SOUND_SENSOR);
// todo add check for is playing
if (status_sensor == 0 && digitalRead(pinDFBusy) == HIGH)
// if (status_sensor == 0)
{
Serial.println(("-- MIC ON"));
if (clap == 0)
{
Serial.println(("-- CLAP was 0 ++"));
detection_range_start = detection_range = millis();
clap++;
}
else if (clap > 0 && millis() - detection_range >= 50)
{
Serial.println(("-- clap++ because another clap in range"));
detection_range = millis();
clap++;
}
}
if (millis() - detection_range_start >= 400)
{
if (clap == 2)
{
triggerStart();
}
clap = 0;
}
}
void printDetail(uint8_t type, int value)
{
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerUSBInserted:
Serial.println("USB Inserted!");
break;
case DFPlayerUSBRemoved:
Serial.println("USB Removed!");
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}