Control audio from micro SD with LDR

Hey everyone,

I’m working on an interactive installation and I need some help with the coding part.

I just bought vs1053 shield, which is connected to my Arduino Uno as it is written in this tutorial.
I used player_simple code from their library and add the LDR part. The idea is to take the value from the LDR and check if it is smaller than 150. If it is > play track 1, and if it is not > pause track 1. The problem is that when the sensor got smaller value and the audio starts, the sensor stop reading the next values… and the audio is played until the track is over and after that it continues to read the next values.

I want to find a way while the audio plays, the sensor still reads values and in the moment it got higher value to pause the audio.

This is what I got so far… maybe the if statement is not the one that I need to use (I’m not sure). I also tried while and the do…while but still nothing:

void loop() {

//CHECK LDR value
  val1 = analogRead(ldr1);            
  Serial.print("sensor1 = " );                       
  Serial.println(val1);
  delay(1000); 
  
 //IF 
  if (val1 < 150){
  Serial.println(F("Playing track 001"));
  musicPlayer.playFullFile("TRACK0~1.MP3");
  //Play another file in the background, REQUIRES interrupts!
  //Serial.println(F("Playing track 002"));
  //musicPlayer.startPlayingFile("TRACK0~2.MP3"); 
       
  }else{
    musicPlayer.pausePlaying(true);
}
 
  delay(100);
}

Can you suggest something?

What if you replace

musicPlayer.playFullFile("TRACK0~1.MP3");

that blocks until the track is played, with

musicPlayer.startPlayingFile("TRACK0~1.MP3");

See the 'player_interrupts.ino' demo to see how.

Hi Riva,
thank you for the suggestion… so I tried this code and now the audio can be paused, but when it starts it plays 1 second (because of the delay for the ldr) and then it starts again and again and it can’t play more than a second. Maybe all this cause a confusion and after awhile the audio dispersers completely, only the Serial monitor is printing. Do you have other suggestions?

void loop() {

//CHECK LDR value
  val1 = analogRead(ldr1);            
  Serial.print("sensor1 = " );                       
  Serial.println(val1);
  delay(1000); 
  
 //IF 
  if (val1 < 150){
  Serial.println(F("Playing track 001"));
  musicPlayer.startPlayingFile("TRACK0~1.MP3");

    while (musicPlayer.startPlayingFile("TRACK0~1.MP3")) {   
      val1 = analogRead(ldr1);            
      Serial.print("sensor1 value while playing = " );                       
      Serial.println(val1);
      delay(1000);       
      } 
  }else{
    musicPlayer.pausePlaying(true);
}
 
  delay(100);
}

I would suggest you abandon your current code, load the player_interrupts.ino example supplied in the library and try adding your LDR code to that.
What happens when the track ends, do you load it up again? If so then you might need to know if a track is still playing (I have deleted the library now an am working on other stuff so cannot look now).

LDR < 150?
Is track playing? If not then play it.

LDR >= 150
Is track paused? If not then pause

Hey Riva. You gave very nice suggestion. Thanks for the help.I also have some problem and not getting solution but here i found solution.

Thank you again… Tried that as well, but no result. If the value is < 150 it starts the audio, and after a second it starts it again and again while the value is < 150. But when it gets > 150 the track is pause as it should be.

milena-p:
Thank you again… Tried that as well, but no result. If the value is < 150 it starts the audio, and after a second it starts it again and again while the value is < 150. But when it gets > 150 the track is pause as it should be.

POST YOUR CODE!!!

As Grump_Mike says, post your current code.
The point from " Is track playing? If not then play it." is you need to set a flag when you start the track playing so the next time it tries to play the file it checks the flag and does not start it again if it is already playing.