HELP! Error Compiling to NANO!

Hello,

I found this code for an Arduino to play MP3 files triggered by a PIR. The problem i'm having is that it doesn't compile for an Arduino Nano.
How do I get this to work on my Arduino Nano?
Thanks.

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10,11); //RX,TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

#define SENSORPIN 9
#define PAUSETME 20000
 
void setup() {
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  pinMode(SENSORPIN, INPUT);

  Serial.println();
  Serial.println(F("Initializing DFPlayer..."));

  //Use softwareSerial to communicate with MP3
  if (!myDFPlayer.begin(mySoftwareSerial)) {
    Serial.println(F("Unable to begin:"));
     Serial.println(F("1.Please recheck connections!"));
      Serial.println(F("2.Please insert the SD card!"));
      while(true);
  }
      Serial.println(F("DFPlayer Mini online"));

      //Set volume from 0-30
      myDFPlayer.volume(25);

}

void sensorActivated() {
  int pirSensor=digitalRead(SENSORPIN);
  if(pirSensor==HIGH)
  {
     Serial.println(F("Sensor Activated"));
     Serial.println(F("DFPlayer Working..."));
     myDFPlayer.play(1);
  }
      return;
}

Please post the full error message copied using the "Copy error messages" button in the IDE

Ok, so I found and downloaded a Serial MP3 Player Library with an example on it. It successfully complied to the Arduino Nano, but I want the mp3 file/files to trigger only when the PIR sensor detects something.

My question now is how do I do this? How do I fuse my previous code (above-first post), with this code (below)?

Thanks in advance.

#include "SerialMP3Player.h"

#define TX 11
#define RX 10

SerialMP3Player mp3(RX,TX);


void setup() {
  mp3.showDebug(1);       // print what we are sending to the mp3 board.

  Serial.begin(9600);     // start serial interface
  mp3.begin(9600);        // start mp3-communication
  delay(500);             // wait for init

  mp3.sendCommand(CMD_SEL_DEV, 0, 2);   //select sd-card
  delay(500);             // wait for init

  menu('?',0); // print the menu options.
}

 char c;  // char from Serial
 char cmd=' ';
 char cmd1=' ';


// the loop function runs over and over again forever
void loop() {

  if (Serial.available()){
    c = Serial.read();
    decode_c(); // Decode c.
  }
  // Check for the answer.
  if (mp3.available()){
    Serial.println(mp3.decodeMP3Answer()); // print decoded answers from mp3
  }
}

void menu(char op, int nval){
  // Menu
  switch (op){
    case '?':
    case 'h':
        Serial.println("SerialMP3Player Basic Commands:");
        Serial.println(" ? - Display Menu options. ");
        Serial.println(" P01 - Play 01 file");
        Serial.println(" F01 - Play 01 folder");
        Serial.println(" S01 - Play 01 file in loop");
        Serial.println(" V01 - Play 01 file, volume 30");
        Serial.println(" p - Play");
        Serial.println(" a - pause");
        Serial.println(" s - stop ");
        Serial.println(" > - Next");
        Serial.println(" < - Previous");
        Serial.println(" + - Volume UP");
        Serial.println(" - - Volume DOWN");
        Serial.println(" v15 - Set Volume to 15");
        Serial.println(" c - Query current file");
        Serial.println(" q - Query status");
        Serial.println(" x - Query folder count");
        Serial.println(" t - Query total file count");
        Serial.println(" r - Reset");
        Serial.println(" e - Sleep");
        Serial.println(" w - Wake up");
        break;

    case 'P':
        Serial.println("Play");
        mp3.play(nval);
        break;

    case 'F':
        Serial.println("Play Folder");
        mp3.playF(nval);
        break;

    case 'S':
        Serial.println("Play loop");
        mp3.playSL(nval);
        break;

    case 'V':
        Serial.println("Play file at 30 volume");
        mp3.play(nval,30);
        break;


    case 'p':
        Serial.println("Play");
        mp3.play();
        break;

    case 'a':
        Serial.println("Pause");
        mp3.pause();
        break;

    case 's':
        Serial.println("Stop");
        mp3.stop();
        break;

    case '>':
        Serial.println("Next");
        mp3.playNext();
        break;

    case '<':
        Serial.println("Previous");
        mp3.playPrevious();
        break;

    case '+':
        Serial.println("Volume UP");
        mp3.volUp();
        break;

    case '-':
        Serial.println("Volume Down");
        mp3.volDown();
        break;

    case 'v':
        Serial.println("Set to Volume");
          mp3.setVol(nval);
          mp3.qVol();
        break;

    case 'c':
        Serial.println("Query current file");
        mp3.qPlaying();
        break;

    case 'q':
        Serial.println("Query status");
        mp3.qStatus();
        break;

    case 'x':
        Serial.println("Query folder count");
        mp3.qTFolders();
        break;

    case 't':
        Serial.println("Query total file count");
        mp3.qTTracks();
        break;

    case 'r':
        Serial.println("Reset");
        mp3.reset();
        break;

    case 'e':
        Serial.println("Sleep");
        mp3.sleep();
        break;

    case 'w':
        Serial.println("Wake up");
        mp3.wakeup();
        break;
  }
}

void decode_c(){
  // Decode c looking for a specific command or a digit

  // if c is a 'v', 'P', 'F', 'S' or 'V' wait for the number XX
  if (c=='v' || c=='P' || c=='F' || c=='S' || c=='V'){
    cmd=c;
  }else{
    // maybe c is part of XX number
    if(c>='0' && c<='9'){
      // if c is a digit
      if(cmd1==' '){
        // if cmd1 is empty then c is the first digit
        cmd1 = c;
      }else{
        // if cmd1 is not empty c is the second digit
        menu(cmd, ((cmd1-'0')*10)+(c-'0'));
        cmd = ' ';
        cmd1 = ' ';
      }
    }else{
      // c is not a digit nor 'v', 'P', 'F' or 'S' so just call menu(c, nval);
      menu(c, 0);
    }
  }
}
undefined reference to `loop'

Seems to be the key takeaway point.

What do you mean
"(undefined reference to `loop')"

It's there, in your error messages, which you appear to have now deleted.

How do I fuse my previous code (above-first post)

You mean the code that

doesn't compile for an Arduino Nano.

See reply #1

I want to have this code but instead of serial commands triggering it, I want a PIR sensor to trigger it.:

#include "SerialMP3Player.h"

#define TX 11
#define RX 10

SerialMP3Player mp3(RX,TX);


void setup() {
  mp3.showDebug(1);       // print what we are sending to the mp3 board.

  Serial.begin(9600);     // start serial interface
  mp3.begin(9600);        // start mp3-communication
  delay(500);             // wait for init

  mp3.sendCommand(CMD_SEL_DEV, 0, 2);   //select sd-card
  delay(500);             // wait for init

  menu('?',0); // print the menu options.
}

 char c;  // char from Serial
 char cmd=' ';
 char cmd1=' ';


// the loop function runs over and over again forever
void loop() {

  if (Serial.available()){
    c = Serial.read();
    decode_c(); // Decode c.
  }
  // Check for the answer.
  if (mp3.available()){
    Serial.println(mp3.decodeMP3Answer()); // print decoded answers from mp3
  }
}

void menu(char op, int nval){
  // Menu
  switch (op){
    case '?':
    case 'h':
        Serial.println("SerialMP3Player Basic Commands:");
        Serial.println(" ? - Display Menu options. ");
        Serial.println(" P01 - Play 01 file");
        Serial.println(" F01 - Play 01 folder");
        Serial.println(" S01 - Play 01 file in loop");
        Serial.println(" V01 - Play 01 file, volume 30");
        Serial.println(" p - Play");
        Serial.println(" a - pause");
        Serial.println(" s - stop ");
        Serial.println(" > - Next");
        Serial.println(" < - Previous");
        Serial.println(" + - Volume UP");
        Serial.println(" - - Volume DOWN");
        Serial.println(" v15 - Set Volume to 15");
        Serial.println(" c - Query current file");
        Serial.println(" q - Query status");
        Serial.println(" x - Query folder count");
        Serial.println(" t - Query total file count");
        Serial.println(" r - Reset");
        Serial.println(" e - Sleep");
        Serial.println(" w - Wake up");
        break;

    case 'P':
        Serial.println("Play");
        mp3.play(nval);
        break;

    case 'F':
        Serial.println("Play Folder");
        mp3.playF(nval);
        break;

    case 'S':
        Serial.println("Play loop");
        mp3.playSL(nval);
        break;

    case 'V':
        Serial.println("Play file at 30 volume");
        mp3.play(nval,30);
        break;


    case 'p':
        Serial.println("Play");
        mp3.play();
        break;

    case 'a':
        Serial.println("Pause");
        mp3.pause();
        break;

    case 's':
        Serial.println("Stop");
        mp3.stop();
        break;

    case '>':
        Serial.println("Next");
        mp3.playNext();
        break;

    case '<':
        Serial.println("Previous");
        mp3.playPrevious();
        break;

    case '+':
        Serial.println("Volume UP");
        mp3.volUp();
        break;

    case '-':
        Serial.println("Volume Down");
        mp3.volDown();
        break;

    case 'v':
        Serial.println("Set to Volume");
          mp3.setVol(nval);
          mp3.qVol();
        break;

    case 'c':
        Serial.println("Query current file");
        mp3.qPlaying();
        break;

    case 'q':
        Serial.println("Query status");
        mp3.qStatus();
        break;

    case 'x':
        Serial.println("Query folder count");
        mp3.qTFolders();
        break;

    case 't':
        Serial.println("Query total file count");
        mp3.qTTracks();
        break;

    case 'r':
        Serial.println("Reset");
        mp3.reset();
        break;

    case 'e':
        Serial.println("Sleep");
        mp3.sleep();
        break;

    case 'w':
        Serial.println("Wake up");
        mp3.wakeup();
        break;
  }
}

void decode_c(){
  // Decode c looking for a specific command or a digit

  // if c is a 'v', 'P', 'F', 'S' or 'V' wait for the number XX
  if (c=='v' || c=='P' || c=='F' || c=='S' || c=='V'){
    cmd=c;
  }else{
    // maybe c is part of XX number
    if(c>='0' && c<='9'){
      // if c is a digit
      if(cmd1==' '){
        // if cmd1 is empty then c is the first digit
        cmd1 = c;
      }else{
        // if cmd1 is not empty c is the second digit
        menu(cmd, ((cmd1-'0')*10)+(c-'0'));
        cmd = ' ';
        cmd1 = ' ';
      }
    }else{
      // c is not a digit nor 'v', 'P', 'F' or 'S' so just call menu(c, nval);
      menu(c, 0);
    }
  }
}

I want to have this code but instead of serial commands triggering it, I want a PIR sensor to trigger it.:

In a simpler sketch can you read the PIR sensor and print a message when it is triggered ?

Alright! Thanks for your patience and help! I finally got it To work!
I finally realized I had to delete a line and add void loop.
Once again Thanks.

New working code:

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10,11); //RX,TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

#define SENSORPIN 9
#define PAUSETME 20000
 
void setup() {
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  pinMode(SENSORPIN, INPUT);

  Serial.println();
  Serial.println(F("Initializing DFPlayer..."));

  //Use softwareSerial to communicate with MP3
  if (!myDFPlayer.begin(mySoftwareSerial)) {
    Serial.println(F("Unable to begin:"));
     Serial.println(F("1.Please recheck connections!"));
      Serial.println(F("2.Please insert the SD card!"));
      while(true);
  }
      Serial.println(F("DFPlayer Mini online"));

      //Set volume from 0-30
      myDFPlayer.volume(25);

}

void loop ()
 {
  int pirSensor=digitalRead(SENSORPIN);
  if(pirSensor==HIGH)
  {
     Serial.println(F("Sensor Activated"));
     Serial.println(F("DFPlayer Working..."));
     myDFPlayer.play(1);
  }
      return;
}

But I doesn't work with the PIR. It just plays on a constant loop? If I unplug the input for the PIR it finishes the MP3 file (and gets significantly louder) then stops until I plug the PIR input back in, where it plays on a loop and the volume decreases).
How do fix this?

Is anything keeping the sensor pin LOW when it is not triggered or is it floating at an unknown, possibly HIGH level ?

Perhaps do some reading on pulldown resistors and their use

Why the return; at then end of loop() ? It will cause no problem but at the end of loop() the sketch returns to the hidden main() function anyway

I don't know. Let's say the audio file is 10 sec long, it will constantly repat the first 1-2 sec on a loop and when i unplug the PIR sensor pin and wait a bit it will play the full 10 sec

And if I leave the PIR SENSOR pin unplugged the audio file will play automatically after a certain amount of time.

Everything you say leads me to the conclusion that the input is going HIGH or remains HIGH whether or not the PIR is triggered. Try my earlier suggestion and write a small sketch that reads the PIR and prints a message when it is triggered and/or prints its state whether it is triggered or not. Does it behaving as you expect ?

I sort of got it to work. When there's motion it plays the full song but it still repeats/loops the first 1-2 secods before continuing with the rest of the song. But It shuts off after the songs done and when it detects motion it will play.

I ended up calibrating my PIR using this video:

It's calibrated but I still have that looping/repeating problem. Also, if it detects multiple motions it will keep starting the song until all the motion stops completely.

Is there away to lock out the PIR sensor for ____amount of seconds then it unlocks, that way it shouldn't repeat/loop the beginning of the audio file?

So basically what i'm trying to achieve with my serial mp3 (https://www.amazon.ca/Aideepen-YX5300-Control-Serial-Arduino/dp/B01JCI23JG) is when motion is detected it plays the complete audio file properly and then waits a couple of seconds before it can start the cycle again.

notapro101:
So basically what i'm trying to achieve with my serial mp3 (https://www.amazon.ca/Aideepen-YX5300-Control-Serial-Arduino/dp/B01JCI23JG) is when motion is detected it plays the complete audio file properly and then waits a couple of seconds before it can start the cycle again.

Make a 'PIRenabled' variable. Ignore the PIR input when it's clear. Clear it when you start a song, set it when you're finished.

You seem to ignore checking if the repeating is caused by a hardware-problem. It might be that your Input-pin where the PIR-Sensor is connected to "receives" ghost-signals created by the always there electromagnetic noise in the air.
If or if not depends on the hardware-details of your PIR-sensor.

To eliminate this kind of ghost-signals a pull-up or a pull-down-resistor must be added to your hardware.
Can you provide a datasheet of your PIR-sensor?

For the functionality: play MP3-file completely then pause a few seconds then be ready to start again there is no way around learning some basic knowledge about programming.

You are welcomed to ask as many concrete questions as you like. My understanding of this forum is: help understanding how to program.

best regards Stefan

Make a 'PIRenabled' variable. Ignore the PIR input when it's clear. Clear it when you start a song, set it when you're finished.

How would I do that (I'm a beginner at programming)

Hi,

Quote

Make a 'PIRenabled' variable

. Ignore the PIR input when it's clear. Clear it when you start a song, set it when you're finished.

How would I do that (I'm a beginner at programming)

What is your guessing? you could write at least a single line of code what your guessing is how you would

Make a 'PIRenabled' variable

I'm a bit unsure what your knowledge-level is. If I underrate it I apologise.
If you have really no clue how to define a variable on your own my recommendation is to go through this tutorial.

Arduino Programming Course

It is easy to understand and a good mixture between writing about important concepts
and get you going.

When you learned to ride a bike. Did you start in a halfpipe trying do do backloop-jumps?
Surely not. It's the same thing with learning programming. Though it is not as dangerous as having a crash in a half-pipe.

You will moving up the learning-curve much faster if you take 3 or 4 hours time to work through this introductional tutorial.

If you refuse to learn the basics my prediction is: you wil come back every 30 minutes with a new question how to do this or that and waiting some hours for an answer given by some other user.

If you take the time to read this turorial and write small programs your progress will speed up with the knowledge you gain.
If you have concrete questions these questions all all welcomed.

Your question

How would I do that (I'm a beginner at programming)

is unconcrete. It is more or less a hidden request
"can you write the code for me? "

best regards Stefan