Help with Boolean operator and mp3 module

So, I'm coding a robot that's meant to have two different triggers for the same mp3 module, playing different songs. In one, a distance sensor causes mp3 1 to play, and another a photoresistor causes mp3 2 to play. But I don't want them to play at the same time, so I'm trying to make it so that if mp3 is false, then whichever sensor is activated first can play, making mp3 true. So that the other sensor can't play it until it is false.

I'm very new to coding, and my professor was helping me and ran out of time. The last thing he did to the coding, made it so that the mp3 1 automatically played once when the arduino starts up, and then not again, without even being triggered by either sensor.
Again, I'm new, but I feel like it's because "mp3" isn't actually connected to the mp3 module in any way, so how does the coding know that mp3 = false means that the mp3 module isn't playing? I tried to ask this to my professor and he simply said "it doesn't" and continued on messing with my coding without addressing this. How does boolean know what it's making false or true?
This is before the setup/loop

Just a note, I have a millis delay because without it, the mp3 associated with the sensor would cut itself off and keep repeating over and over and over while the sensor is activated, so I have it delayed by 5 minutes, and the triggers also affect a single LED.

#include <SoftwareSerial.h>

#define ARDUINO_RX 5//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6//connect to RX of the module

#define trigPin 13//for the distance module
#define echoPin 12

int ldr=A5;//Set A0(Analog Input) for LDR.
int value=0;
int red_light_pin= 10;
int green_light_pin = 9;
int blue_light_pin = 3;
long pushTime;
boolean mp3 = false;

SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);

static int8_t Send_buf[8] = {0} ;

#define NEXT_SONG 0X01 
#define PREV_SONG 0X02 

#define CMD_PLAY_W_INDEX 0X03 //DATA IS REQUIRED (number of song)


#define CMD_SET_VOLUME 0X00//DATA IS REQUIRED (number of volume from 0 up to 30(0x1E))
#define SET_DAC 0X17
#define CMD_PLAY_WITHVOLUME 0X00 //data is needed  0x7E 06 22 00 xx yy EF;(xx volume)(yy number of song)

#define CMD_SEL_DEV 0X09 //SELECT STORAGE DEVICE, DATA IS REQUIRED
                #define DEV_TF 0X02 //HELLO,IM THE DATA REQUIRED
                
#define SLEEP_MODE_START 0X0A
#define SLEEP_MODE_WAKEUP 0X0B

#define CMD_RESET 0X0C//CHIP RESET
#define CMD_PLAY 0X0D //RESUME PLAYBACK
#define CMD_PAUSE 0X0E //PLAYBACK IS PAUSED

#define CMD_PLAY_WITHFOLDER 0X0F//DATA IS NEEDED, 0x7E 06 0F 00 01 02 EF;(play the song with the directory \01\002xxxxxx.mp3

#define STOP_PLAY 0X16

#define PLAY_FOLDER 0X17// data is needed 0x7E 06 17 00 01 XX EF;(play the 01 folder)(value xx we dont care)

#define SET_CYCLEPLAY 0X19//data is needed 00 start; 01 close

#define SET_DAC 0X17//data is needed 00 start DAC OUTPUT;01 DAC no output

void setup()
{
  Serial.begin(9600);//Start our Serial coms for serial monitor in our pc
mySerial.begin(9600);//Start our Serial coms for THE MP3
delay(500);//Wait chip initialization is complete
   sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card  
delay(200);//wait for 200ms
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(red_light_pin, OUTPUT);
pinMode(green_light_pin, OUTPUT);
pinMode(blue_light_pin, OUTPUT);
}

void loop()
{{
value=analogRead(trigPin);//Reads the Value of distance i hope.
Serial.println(" cm");
value=analogRead(ldr);//Reads the Value of LDR(light).
Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
Serial.println(value);
if(measureDistance(trigPin,echoPin)<50 || value>900)
  {
  digitalWrite(blue_light_pin, HIGH);
  digitalWrite(red_light_pin, HIGH);
  digitalWrite(green_light_pin, HIGH);
  }
  else
  {
  digitalWrite(blue_light_pin, LOW);
  digitalWrite(red_light_pin, LOW);
  digitalWrite(green_light_pin, LOW);
}
  if(measureDistance(trigPin,echoPin)<50 && mp3 == false){
  if (pushTime < millis()) {   // was the button pushed more than 5 minutes ago
      sendCommand(CMD_PLAY_WITHFOLDER, 0X0101);
      mp3 = true;//exp play the third song of the second folder
      pushTime = millis() + 300000;      // remember the time when the button was pushed
  }}
 {
  if(value>900 && mp3 == false){    // button being pushed
 }
  if (pushTime < millis()) {   // was the button pushed more than 5 minutes ago
    sendCommand(CMD_PLAY_WITHFOLDER, 0X0202);
    mp3 = true;//play the third song of the second folder
    pushTime = millis() + 300000;      // remember the time when the button was pushed
  }
}
delay(300);
}
  }
void sendCommand(int8_t command, int16_t dat)
{
 delay(20);
 Send_buf[0] = 0x7e; //starting byte
 Send_buf[1] = 0xff; //version
 Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
 Send_buf[3] = command; //
 Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
 Send_buf[5] = (int8_t)(dat >> 8);//datah
 Send_buf[6] = (int8_t)(dat); //datal
 Send_buf[7] = 0xef; //ending byte
 for(uint8_t i=0; i<8; i++)//
 {
   mySerial.write(Send_buf[i]) ;//send bit to serial mp3
   Serial.print(Send_buf[i],HEX);//send bit to serial monitor in pc
 }
 Serial.println();
}

long measureDistance(int trigger,int echo){
   long duration, distance;
  
  digitalWrite(trigger, LOW);  //PULSE ___|---|___
  delayMicroseconds(2); 
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigger, LOW);
  
  duration = pulseIn(echo, HIGH);
  distance = (duration/2) / 29.1;
   Serial.println("distance:");
   Serial.println(distance);
  return distance;

}

rather than sending actual codes, look at some of the DFMini player libraries (if that is what you are using). They have functions to tell if something is playing or not. If not, start the new tune, if so, skip it.

Ah, sorry I should have mentioned I'm using the serial mp3 catalex or MD_YX5300
I'm not entirely sure how to search its library for functions though

Can you step back a bit and try to master the different bits of hardware on their own? Then combining their functions will be WAY WAY WAY easier for you.

And what's this?

void loop()
{{

Just tossing extra brackets in for good measure? There is a lot of code missing in this post and your trouble may very well be in the bit we can't see.

Set up a small sketch that makes the mp3 player go. Get it working so that you are happy with how to run it.

Set up a small sketch for the ultra sonic distance thing. Make sure you are happy about how to make it work.

Same for the LDR.

And by happy I mean NO MORE GUESSING. Once you understand them you will no longer be in the dark and guessing. Then everything becomes easy.

-jim lee

I actually did that way before this, this is my attempt at combing the coding. I had two arduino files, one where the LDR turned on the MP3 and the LED and one where the distance sensor turned on the MP3 and the LED, and separately they both worked great.

Alas, this project is due next week so I'm resorting to cutting corners as much as I can to get it to work in whatever way possible.

And as for the extra brackets... It kept telling me a part of my code was not specified or something and that stopped when I added more brackets.

I wish my professor had actually bothered to explain coding to us for this class, but he basically told us to copy code from wherever we could find it
I'm trying to learn as much as I can from google and this website

OK, can you..

A) post your compete code?

B) Explain in "human" what you want it to do? (End product your looking for.) That would go a long way for getting people to help.

Getting a end product explanation can at times key someone into showing you a different and possibly much more direct approach.

-jim lee

I have edited the original post to reflect my whole code.

The robot is meant to sense when someone walks in front of it, and play a message to them which prompts the person to place their hand over the photoresistor, which triggers another mp3 file.
Also an LED turns on when the sensors are active.

mp3 is what is called a state variable because it holds or indicates state information about the player. However, you have to set it or unset it explicitly when you issue commands to the players (start, stop etc.). But, as @blh64 pointed out, it may be superfluous because the player library may already have such an equivalent variable or method.

I simplified you code some by stripping out stuff that was not used.
I had a shot at sorting the messed up bracket logic in your loop() function.
Your button timing code didn't make a lot of sense to me. I left it alone.
What they said about mp3 boolean is true. You never actually clear it when a song is done. Hence, you will only get one shot.

See what you think and possibly try this out and see if you get any further? Or I may have possibly just made a mess out of what you had.

#include <SoftwareSerial.h>

#define ARDUINO_RX 5    //should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6    //connect to RX of the module

#define trigPin   13      //for the distance module
#define echoPin   12
#define  ldr      A5

int   value             = 0;
int   red_light_pin     = 10;
int   green_light_pin   = 9;
int   blue_light_pin    = 3;
long  pushTime;
bool  mp3               = false;

SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);

static int8_t Send_buf[8] = {0} ;


#define CMD_SEL_DEV           0X09     //SELECT STORAGE DEVICE, DATA IS REQUIRED
#define DEV_TF                0X02     //HELLO,IM THE DATA REQUIRED
#define CMD_PLAY_WITHFOLDER   0X0F    //DATA IS NEEDED, 0x7E 06 0F 00 01 02 EF;(play the song with the directory \01\002xxxxxx.mp3

void setup() {
   
  Serial.begin(9600);                  //Start our Serial coms for serial monitor in our pc
  mySerial.begin(9600);                //Start our Serial coms for THE MP3
  delay(500);                          //Wait chip initialization is complete
  sendCommand(CMD_SEL_DEV, DEV_TF);    //select the TF card
  delay(200);                          //wait for 200ms **WHY?**
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(red_light_pin, OUTPUT);
  pinMode(green_light_pin, OUTPUT);
  pinMode(blue_light_pin, OUTPUT);
}


void loop() {
   
   value = analogRead(ldr);                                       //Reads the Value of LDR(light).
   Serial.println("LDR value is :");                              //Prints the value of LDR to Serial Monitor.
   Serial.println(value);
   if (measureDistance(trigPin, echoPin) < 50 || value > 900) {
      digitalWrite(blue_light_pin, HIGH);
      digitalWrite(red_light_pin, HIGH);
      digitalWrite(green_light_pin, HIGH);
   } else {
      digitalWrite(blue_light_pin, LOW);
      digitalWrite(red_light_pin, LOW);
      digitalWrite(green_light_pin, LOW);
   }
   if (measureDistance(trigPin, echoPin) < 50 && mp3 == false) {
      if (pushTime < millis()) {                                  // was the button pushed more than 5 minutes ago
         sendCommand(CMD_PLAY_WITHFOLDER, 0X0101);
         mp3 = true;                                              //exp play the third song of the second folder
         pushTime = millis() + 300000;                            // remember the time when the button was pushed
      }
   }
   if (value > 900 && mp3 == false) {                             // button being pushed
      if (pushTime < millis()) {                                  // was the button pushed more than 5 minutes ago
         sendCommand(CMD_PLAY_WITHFOLDER, 0X0202);
         mp3 = true;                                              //play the third song of the second folder
         pushTime = millis() + 300000;                            // remember the time when the button was pushed
      }
      delay(300);
   }
}


void sendCommand(int8_t command, int16_t dat) {
   
  delay(20);
  Send_buf[0] = 0x7e; //starting byte
  Send_buf[1] = 0xff; //version
  Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
  Send_buf[3] = command; //
  Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
  Send_buf[5] = (int8_t)(dat >> 8);//datah
  Send_buf[6] = (int8_t)(dat); //datal
  Send_buf[7] = 0xef; //ending byte
  for (uint8_t i = 0; i < 8; i++) {
    mySerial.write(Send_buf[i]) ;//send bit to serial mp3
    Serial.print(Send_buf[i], HEX); //send bit to serial monitor in pc
  }
  Serial.println();
}


long measureDistance(int trigger, int echo) {
  
  long duration, distance;

  digitalWrite(trigger, LOW);  //PULSE ___|---|___
  delayMicroseconds(2);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);

  duration = pulseIn(echo, HIGH);
  distance = (duration / 2) / 29.1;
  Serial.println("distance:");
  Serial.println(distance);
  return distance;
}

-jim lee

It still doesn't quite work the way the way I need it to, even with a few more adjustments. I also still had to add another curly bracket to get it to read the code haha! But I do like how organized it looks, so thank you for that. I sat with my teacher for an hour today and we got nowhere with it, but I'm gonna keep trying a little bit more with some of the serial mp3 code I've looked up before falling onto my backup plan.
Thank you!

It compiled fine on my machine. Did you cut and paste what I posted directly and try to compile it?

-jim lee

Inside the IDE, go to Library Manager. Sketch->Include Library->Manage Libraries... (or CTRL+SHIFT+I) and then search for MD_YX5300 and it will show up. Click install. It will install examples as well you can look at and learn from.

Yes

What processor board are you using? IDE version?

-jim lee

Um, not sure what that means entirely. I'm using Arduino Uno

Ok, think about this..

I copile this on an Arduino UNO. It compiles fine.
You compile this on an Arduino UNO and it doesn't compile at all.

What's different, what's changed from the time I posted this to the time you hit compile?

-jim lee

You might have more luck with the library DFPlayerMini_Fast.h: How to install

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.