Servo write after .wav stops playing - TMRpcm lbrary

hey guys

I'm currently having a coding problem with this library, I don't think it's a library problem but a problem in my code.

My scheme is supposed to do the following:

A LDR gets low light and triggers and action, this action starts by doing a servo.write and then plays a .wav file using TMRpcm library. I have it working till here.

But then I want to the servo.write to go back to his original position and a led to go HIGH, but I'm not able to call an end to the file and perform the next actions.

I've already called the function audio.isPlaying() in order to get the servo.write to the original position when the function gives me 0 but no luck.

Can anybody help me?

#include <Servo.h> 
#include <SD.h>                      // need to include the SD library
#define SD_ChipSelectPin 10   //using digital pin 4 on arduino nano 328
#include <TMRpcm.h>           //  also need to include this library...

TMRpcm som;   // create an object for use in this sketch

Servo myservo;

int ledPin = 5;

int servo = 6;

int tAbertura = 1000; // tAbertura = tempo que a porta demora a abrir

// Variables will change:
//int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated

unsigned long currentMillis = millis();
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

int ledState = LOW;


void setup() {
  
som.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc

 
Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    Serial.println("SD fail");  
    return;   // don't do anything more if not
  }
   
//pinMode(laserPin, OUTPUT);
pinMode(ledPin, OUTPUT);

myservo.attach(servo); 



}

void loop(){
  
//digitalWrite(laserPin, HIGH);

Serial.print(" 'Luz:      '" );
Serial.println(analogRead(0));
Serial.print(" 'SOM:      '" );
Serial.println(som.isPlaying());

// check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
  
  if( analogRead(0) <= 650){ //ajustar valor ao ambiente
      
    if( currentMillis - previousMillis > interval){
      
    previousMillis = currentMillis;
    
   myservo.write(180);
   delay(1000);
   
    som.setVolume(4);
    som.play("heman2.wav");
     
  switch(som.isPlaying()){
    
    case 0:
    som.disable();
    myservo.write(70);
    break;
    
    case 1:
    break;
      }
  
    }
  }
   
   else{ 
     myservo.write(70);
     

  } 

}

but I'm not able to call an end to the file

Call an end to what file? Do you want to make the music stop?

myservo.attach(servo);

You are attaching a servo to a servo? Meaningful names ARE important. I like to see Pin somewhere in the name of a variable that refers to a pin number. Something like serPinvo. Or maybe at the end...

    som.setVolume(4);
    som.play("heman2.wav");
     
  switch(som.isPlaying()){

As soon as you start the music playing, see if the music is playing. Well, of course it will be. Or it won't be, but the answer will always be the same.

PaulS:
Call an end to what file? Do you want to make the music stop?

myservo.attach(servo);

You are attaching a servo to a servo? Meaningful names ARE important. I like to see Pin somewhere in the name of a variable that refers to a pin number. Something like serPinvo. Or maybe at the end...

    som.setVolume(4);

som.play("heman2.wav");
   
  switch(som.isPlaying()){



As soon as you start the music playing, see if the music is playing. Well, of course it will be. Or it won't be, but the answer will always be the same.

Hey PaulS,

thanks for your answer.

I will try your sugestion about the names of the variables.

What happens is that, after writing in the code:

som.play("heman2.wav");

I'm not able to execute any code after the file played. Not even a LED nor a servo and that's why I have that switch function.

After the files starts to play, after its duration it will stop playing but it seems that the function continues to happen because no code line written afterwards runs.

What I need is some function that finishes the som.play("heman2.wav"), so I can run other functions after it.

The layout of your code is very raggedy so I wonder if even you know what it is supposed to do. For example, which IF is the final ELSE supposed to belong to?

Then there is a lot of confusing stuff

What is analogRead() reading? What is it intended to signify?
Why are you printing one value of analogRead() and basing your IF on another value?

Why are you printing things in every iteration of loop() - surely that is much too often?
What has the blink interval (if that is what it is) got to do with the start of play and the movement of the servo? They seem to be all mixed up together.
Why are you using an interval of 1000 and a delay() of 1000 - won't they clash?
Why are you using delay() at all, considering that you are also using millis()

I'm sure there is more ...

...R

Robin2:
The layout of your code is very raggedy so I wonder if even you know what it is supposed to do. For example, which IF is the final ELSE supposed to belong to?

Then there is a lot of confusing stuff

What is analogRead() reading? What is it intended to signify?
Why are you printing one value of analogRead() and basing your IF on another value?

Why are you printing things in every iteration of loop() - surely that is much too often?
What has the blink interval (if that is what it is) got to do with the start of play and the movement of the servo? They seem to be all mixed up together.
Why are you using an interval of 1000 and a delay() of 1000 - won't they clash?
Why are you using delay() at all, considering that you are also using millis()

I'm sure there is more ...

...R

Robin,

I've cleaned up my code and I'll try to explain you what I'm doing.

It's a laser trip wire.

I have a laser emitting to a LDR.

whenever the LDR receives a count lower than X, then it's supposed to do a servo.write, then play a song and after that do another servo.write.

Physically, what happens is, you interrupt the laser, a small door in a birdhouse opens, plays a song and then the doors closes.

The problems was that I wasn't able to get the door closed after the sound file stop playing.

The TMRpcm library and servo library both use Timer1. You can't use them in the same sketch. There is a library called ServoTimer2 that uses Timer2 instead so it should be compatible with the TMRpcm library.

rfranco:
I've cleaned up my code

Would be a big help if you post the cleaned up version.

...R

johnwasser:
The TMRpcm library and servo library both use Timer1. You can't use them in the same sketch. There is a library called ServoTimer2 that uses Timer2 instead so it should be compatible with the TMRpcm library.

Thanks John!

I've managed to get it working with that library!