# play audio from SD  and run servo

**URL:** <https://forum.arduino.cc/t/play-audio-from-sd-and-run-servo/503244>\
**Category:** Programming\
**Created:** [January 20, 2018, 6:51am UTC](https://forum.arduino.cc/t/play-audio-from-sd-and-run-servo/503244 "2018-01-20T06:51:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![crimzon16](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@crimzon16](https://forum.arduino.cc/u/crimzon16)\
**Post date:** [January 20, 2018, 6:51am UTC](https://forum.arduino.cc/t/play-audio-from-sd-and-run-servo/503244/1 "2018-01-20T06:51:05Z")

</div>

i'm trying to run the servo motor while the audio is playing... if i run servo only.. its working fine.. but if i combine the codes.. audio is playing but servo is not working... here is my code

#include \<SD.h\> // need to include the SD library  
#include \<TMRpcm.h\> // also need to include this library...  
#include \<SPI.h\>  
#include \<Servo.h\>

#define SD\_ChipSelectPin 10 //using digital pin 4 on arduino nano 328, can use other pins  
#define SPEAKER\_PIN 9  
#define LED\_PIN 2  
#define SERVO\_PIN 4

TMRpcm tmrpcm; // create an object for use in this sketch  
Servo myservo;

int cnt;  
void setup(){  
myservo.attach(SERVO\_PIN);  
myservo.write(0);  
delay(1000);  
tmrpcm.speakerPin = SPEAKER\_PIN; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc

pinMode(LED\_PIN,OUTPUT);  
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  
}

}

void loop(){

tmrpcm.play("engine1.wav");

for (cnt=1;cnt\<=180;cnt++)  
{  
myservo.write(cnt);  
delay(15);  
}  
delay(3000);  
tmrpcm.play("eye\_li~1.wav");  
digitalWrite(LED\_PIN,HIGH);  
delay(1000);  
digitalWrite(LED\_PIN,LOW);  
myservo.write(0);  
delay(2000);

}

can anyone help me

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 20, 2018, 10:40am UTC](https://forum.arduino.cc/t/play-audio-from-sd-and-run-servo/503244/2 "2018-01-20T10:40:35Z")

</div>

If you look at the [TMRpcm github](https://github.com/TMRh20/TMRpcm) they say

> Features
> 
> PCM/WAV playback direct from SD card  
> Main formats: WAV files, 8-bit, 8-32khz Sample Rate, mono. See the wiki for other options.  
> Asynchronous Playback: Allows code in main loop to run while audio playback occurs.  
> **Single timer operation: TIMER1 (Uno,Mega) or TIMER3,4 or 5 (Mega)**  
> Complimentary output or dual speakers  
> 2x Oversampling  
> Supported devices: Arduino Uno, Nano, Mega, etc.

the Servo library also uses Timer1 (see [original mention](https://playground.arduino.cc/ComponentLib/Servotimer1))

If you work from a mega you can change timers, so give each function it’s own timer, if you are on a uno then look for a different servo library not using Timer1 (for example [ServoTimer2](https://github.com/nabontra/ServoTimer2))

=====  
Please correct your post above and add **code tags** around your code:  
`[code]`` [color=blue]// your code is here[/color] ``[/code]`.

It should look like this:`// your code is here`  
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

---

<div class="post-metadata">

**Author:** ![piing123](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@piing123](https://forum.arduino.cc/u/piing123)\
**Post date:** [September 21, 2019, 2:08pm UTC](https://forum.arduino.cc/t/play-audio-from-sd-and-run-servo/503244/3 "2019-09-21T14:08:33Z")

</div>

Hi, i have the same problem too. For my project, i wanted to play a wav file after a force sensor detected a force and then move a servo motor. I have tried the ServoTimer2 library to avoid the conflict between the servo and tmrpcm libraries where they use timer 1 as default. But, the wav file is able to play the first time and the servo is able to move. But, after the first time, only the servo motor is able to move without playing the wav file. The below is my code. Hope anyone can solve my problem. Tyvm.

#include"ServoTimer2.h"  
#include \<SD.h\> // need to include the SD library  
#define SD\_ChipSelectPin 53 //example uses hardware SS pin 53 on Mega2560  
//#define SD\_ChipSelectPin 4 //using digital pin 4 on arduino nano 328, can use other pins  
#include \<TMRpcm.h\> // also need to include this library...  
#include \<SPI.h\>

TMRpcm tmrpcm; // create an object for use in this sketch  
ServoTimer2 myServo1;

void setup(void) {  
tmrpcm.speakerPin = 6; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc  
tmrpcm.setVolume(5);  
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  
}

fsrReading1 = analogRead(fsrPin1); //force sensor to take reading  
Serial.print("Analog reading1 = ");  
Serial.println(fsrReading1);

void loop(void)  
{  
while (true) // FSR 1 (head)  
{  
if ( fsrReading1 \<= 10)  
{  
Serial.println("nothing");  
break;  
}  
else if (fsrReading1 \< 200)  
{  
Serial.println("neutral/sad"); // neutral or sad emotion  
tmrpcm.play("sad.wav");  
delay(4000);  
left\_go(); //(left hand move)  
left\_go(); //(left hand move back)  
break;  
}  
else  
{  
Serial.println("nothing");  
break;  
}  
}  
}  
void left\_go()  
{  
delay(1000);  
myServo1.attach(5);  
for (int i =750; i \<= 1250; i++) // degree (0-60)  
{  
myServo1.write(i);  
delay(3);  
}  
delay(500);  
myServo1.detach();  
delay(1000);  
}  
void left\_return()  
{  
delay(1000);  
myServo1.attach(5);  
for (int i =1250; i \>= 750; i--) //degree (60-0)  
{  
myServo1.write(i);  
delay(3);  
}  
delay(500);  
myServo1.detach();  
delay(1000);  
}

---

<div class="post-metadata">

**Author:** ![jimLee](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jimlee/32/214865_2.png) [@jimLee](https://forum.arduino.cc/u/jimLee)\
**Post date:** [September 21, 2019, 9:52pm UTC](https://forum.arduino.cc/t/play-audio-from-sd-and-run-servo/503244/4 "2019-09-21T21:52:45Z")

</div>

You can run a servo without using interrupts. You only need to give it a 1..2 ms pulse every 20ms or less. This way it'll free the sound code to do its thing with the hardware.

-jim lee

---

<div class="post-metadata">

**Author:** ![piing123](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@piing123](https://forum.arduino.cc/u/piing123)\
**Post date:** [September 24, 2019, 2:54am UTC](https://forum.arduino.cc/t/play-audio-from-sd-and-run-servo/503244/5 "2019-09-24T02:54:24Z")

</div>

Am I using interrupt for servo? I didn't know, can you point out which part should be corrected? Your effort is much appreciated. Thanks

---

<div class="post-metadata">

**Author:** ![jimLee](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jimlee/32/214865_2.png) [@jimLee](https://forum.arduino.cc/u/jimLee)\
**Post date:** [September 24, 2019, 4:21am UTC](https://forum.arduino.cc/t/play-audio-from-sd-and-run-servo/503244/6 "2019-09-24T04:21:15Z")

</div>

The standard servo library uses interrupts. It works fine as long as nothing else wants to use them. Then there's the possibility of them not getting along together.

Set up a timer that fires every.. Say, 15 ms. Then, in your loop(), do your stuff while checking that timer. When the timer goes off, reset it and send out one pulse. 1..2 ms 1.5 ms is center 1 ms is one way, 2 ms is the other way. Map what you want from 0-180 deg -\> 1-2 ms.

-jim lee

---

<div class="post-metadata">

**Author:** ![piing123](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@piing123](https://forum.arduino.cc/u/piing123)\
**Post date:** [September 24, 2019, 5:28am UTC](https://forum.arduino.cc/t/play-audio-from-sd-and-run-servo/503244/7 "2019-09-24T05:28:50Z")

</div>

Ya, I noticed that people saying standard servo library is using timer 1 same as the tmrpcm library. And that is why they have conflict with each other so they recommend the ServoTimer2 library as this library is using timer 2 (like what they said) which is different from tmrpcm library which is using timer 1. Even after using ServoTimer2 library, does this library still using interrupt? And about the timer, as i am not very clear on how to use it as i am still a newbie at this and is the timer to be used for the tmrpcm or the ServoTimer2 library? And is it only possible to move the servo using the mapping function where it turns the servo 0 - 180 degree based on the 1-2ms? Can't i just use the delay function for the servo angle movement?

---

<div class="post-metadata">

**Author:** ![jimLee](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jimlee/32/214865_2.png) [@jimLee](https://forum.arduino.cc/u/jimLee)\
**Post date:** [September 24, 2019, 7:27pm UTC](https://forum.arduino.cc/t/play-audio-from-sd-and-run-servo/503244/8 "2019-09-24T19:27:05Z")

</div>

delay() is death. Keep away from it. Its like a 5,000 year long cigarette for the processor.

You can make your own timer thing by reading micros(); Basically save when you started, do that math for what it'll read when you finish. Read it each time through your loop and do what you need to do when the reading shows you finished.

If you roll all that into a class you can "spawn" as many timers as you need.

-jim lee

---

<div class="post-metadata">

**Author:** ![piing123](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@piing123](https://forum.arduino.cc/u/piing123)\
**Post date:** [September 25, 2019, 3:01pm UTC](https://forum.arduino.cc/t/play-audio-from-sd-and-run-servo/503244/9 "2019-09-25T15:01:16Z")

</div>

Thanks for your info. But i would like to ask if is it possible to use millis function as timer to move the servo without conflicting the tmrpcm library? However, i have searched and read about the millis function when moving the servo back and forth. But the code i have found from forum is that the code can only move the servo back and forth once but not continuously even though it is in the main loop. Plus, even i changed the servointerval which represents the time interval for the servo angle movement from 15ms to like say 500ms, it still shows the same results as 15ms. The code is shown as below:

#include \<Servo.h\>

Servo myservo;

unsigned long Starttimer;  
unsigned long current\_time;  
unsigned long elapsed\_time;  
unsigned long servoMillis = 0;  
unsigned long servointerval = 15;  
unsigned long interval = 1000;

int pos = 0;

void setup() {  
myservo.attach(9);  
}

void loop() {  
for(pos = 0; pos \<= 90; pos+=1) { //opens the arm 60 degrees  
if(millis() - servoMillis \> servointerval) //created to delay 15 milliseconds before next pulse  
{  
servoMillis = millis(); //starts timer  
myservo.write(pos); //moves 5 degrees every 15 milliseconds  
}}

Starttimer=millis(); //starts one second delay timer?  
do {  
current\_time = millis();  
elapsed\_time = current\_time - Starttimer;  
}  
while(elapsed\_time \< interval); //leaves loop when 1 second has passed

for(pos = 90; pos \>=0; pos-=1) { //closes the arm back to 0 degrees  
if(millis() - servoMillis \> servointerval)  
{  
servoMillis = millis();  
myservo.write(pos);  
}}  
}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 10:57pm UTC](https://forum.arduino.cc/t/play-audio-from-sd-and-run-servo/503244/10 "2021-05-05T22:57:28Z")

</div>

Closed on May 5, 2021, 10:57pm UTC


