Hi I'm new here and not sure where to ask this, but I need help with my project.
My problem is that after uploading my code to my nano it just keeps looping. When using serial monitor it just keeps saying that it's rebooting after playing the sound and flashing the light.
Could someone help look over the code I'm using?
Thanks
// ######### sound initialise ###########
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
// ######### Ultrasonic initialise #######
#define trigPin 8 //Trig pin
#define echoPin 9 //Echp pin
// ###################
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 20000; // run time of light to match sound
int led = 5; // LED pin
int Mail_1 = LOW; // Mail detected
int brightness = 0; // initial brightness of LED
int fadeAmount = 4; // speed of LED fade
int playonce = LOW; // Prevent retrigger
void setup() {
mySoftwareSerial.begin(9600);
Serial.begin(115200);
// ####### sound card checks #######
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){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(30); //Set volume value. From 0 to 30
// ##### Ultrasonic set up #######
pinMode(trigPin, OUTPUT); //Trigger ia an output pin
pinMode(echoPin, INPUT); //Echo is an input pin
}
void loop() {
// ####### measure distance
long duration, distance;
digitalWrite(trigPin, LOW); // Trig low
delayMicroseconds(2); // wait 2us
digitalWrite(trigPin, HIGH); // Trig High
delayMicroseconds(10); // wait 10us
digitalWrite(trigPin, LOW); // Trig low
duration = pulseIn(echoPin, HIGH); //wait for echo pin high
//distance = (duration/2) / 29.1; //calculate distance CM
distance = duration*0.034/2;
Serial.print(distance);
if (distance < 2) { // Mail detectected cm
Mail_1= HIGH;
//digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
//digitalWrite(led2,LOW);
}
else {
Mail_1 = LOW;
playonce = LOW;
}
//Serial.print(distance);
//Serial.println(" cm");
delay(100); // short delay
// ######### trigger sound and LED
if (Mail_1 == HIGH && playonce == LOW) {
startMillis =millis(); //initial start time
playonce = HIGH; // prevent retrigger of sound
myDFPlayer.next(); //Play mp3
}
// Run LED flash for set time
while(millis() - startMillis < period)
{
// set the brightness of pin 3:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 250) {
fadeAmount = -fadeAmount ;
}
//LED off for 1 second at end of even cycle
if (brightness==0){delay(1000);
}
delay(20);
} // end of LED flash while loop
// Detect new mail
// playonce = LOW;
// turn LED off at end of sequence
analogWrite(led,0);
}