Playing soundbite with servo movement

I am trying to make a Halloween decoration, it is a skeleton that lifts the right arm when someone walks through the door and plays a sound.

Basically:

  • Photoresistor and laser pointer to set up a makeshift trip sensor
  • Servo moves the arm up and down when the laser is interrupted
  • Sound plays when the laser is interrupted

So far, I have the laser sensor and arm movement working fine, when I interrupt the laser path the arm moves up and down. However when I try to integrate the speaker code, the servo does not move.

I have narrowed down the issue to the line that actually calls for the sound to be played:
"tmrpcm.play("WFM.wav");"

When I remove that line the servo moves as expected, but adding that line prevents the servo from moving. It does not matter if the speaker/SD are connected to the circuit.

The SD card code is something I found online, I have used it successfully on other projects but never when calling it from an if statement.

I'm guessing I have the line in question in the wrong place, but I'm not sure.

Here is the code:


#include <Servo.h>

#include <SD.h> // need to include the SD library
#define SD_ChipSelectPin 4 //connect pin 4 of arduino to cs pin of sd card
#include <TMRpcm.h> //Arduino library for asynchronous playback of PCM/WAV files
#include <SPI.h> 

Servo myservo; 

 TMRpcm tmrpcm; 
int temp=1;

int pos = 0;    
int UpPos = 140;
int DownPos = 10;
int MidPos = 70;

int PhotoThresh = 600;
int PhotoLevel = 0;

int CycleCount = 0;

void setup() {
  myservo.attach(7); 

 tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
 Serial.begin(9600);
 if (!SD.begin(SD_ChipSelectPin)) // returns 1 if the card is present
 {
  Serial.println("SD fail");
  return;
 }

  Serial.begin(9600);

  tmrpcm.setVolume(5);
}

void loop() {

  int PhotoLevel = analogRead(A0);
  Serial.print("PhotoLevel: ");
  Serial.println(PhotoLevel);

  if(PhotoLevel < PhotoThresh){  //Trigger if laser is not hitting the photoresistor

  Serial.println("Start Move");

  tmrpcm.play("WFM.wav"); //This line prevents the servo from moving, intended to play WFM.wav from SD card

 Serial.println("Play Song");
    
  for (pos = DownPos; pos <= UpPos; pos += 1) { 
    myservo.write(pos);  
    Serial.println(pos);
    delay(15); 
    
  }

  Serial.print("UpPos ");
  Serial.println(pos);
  
  for (pos = UpPos; pos >= MidPos; pos -= 1) { 
    myservo.write(pos); 
    Serial.println(pos);
    delay(15);   
  }

  Serial.print("MidPos ");
  Serial.println(pos);

  
   for (pos = MidPos; pos <= UpPos; pos += 1) { 
    // in steps of 1 degree
    myservo.write(pos);  
    Serial.println(pos);
    delay(15); 
  }

  Serial.print("UpPos ");
  Serial.println(pos);

    for (pos = UpPos; pos >= MidPos; pos -= 1) { 
    myservo.write(pos); 
    Serial.println(pos);
    delay(15);   
  }

  Serial.print("MidPos ");
  Serial.println(pos);

  
   for (pos = MidPos; pos <= UpPos; pos += 1) { 
    myservo.write(pos);  
    Serial.println(pos);
    delay(15); 
   }

  Serial.print("UpPos ");
  Serial.println(pos);

  for (pos = UpPos; pos >= DownPos; pos -= 1) { 
    myservo.write(pos); 
    Serial.println(pos);
    delay(15);   
  }

  Serial.print("DownPos ");
  Serial.println(pos);

  CycleCount = CycleCount + 1;
  Serial.print("Cycle count ");
  Serial.println(CycleCount);
  delay(1000);
  }


  
}

TMRpcm and the servo library are probably trying to use the same timer (Timer1).

If you have an Uno, Nano, Mega or similar, try the ServoTimer2 library.

That was the problem, thanks!

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