Servo and playing sound at the same time

Hello friends

I am making a project using Arduino that has servo to rotate and play sound simultaneously in one sketch. I have done it separately and now I want to combine them, I have done a lot of efforts but It didn't work at all when I combine them together. The project is that I have 3 file on my SDD card and the it is parrot sound and a servo on the mouth of the parrot now when the part opens the mouth I want it to play one sound file and then the other and soon. Can someone give me a hint or help me please, thanks.

Here are my codes:

Servo code:

#include <Servo.h>

Servo myservo; // create servo object to control a servo

void setup() {
myservo.attach(6); // attaches the servo on pin 6 to the servo object
}

void loop() {
for (pos = 165; pos >=65; pos -=1) { // goes from 180 degrees to 0 degrees
myservo.write(pos);
delay(15);

}
for (pos = 65; pos <= 165; pos +=1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos);
delay(20);
}
}

and sound code

/*
Example: Control a WTV020-SD-16P module to play voices from an Arduino board.
Created by Diego J. Arevalo, August 6th, 2012.
Released into the public domain.
*/

#include <Wtv020sd16p.h>

int resetPin = 2; // The pin number of the reset pin.
int clockPin = 3; // The pin number of the clock pin.
int dataPin = 4; // The pin number of the data pin.
int busyPin = 5; // The pin number of the busy pin.

Wtv020sd16p wtv020sd16p(resetPin,clockPin,dataPin,busyPin);

void setup() {
//Initializes the module.
wtv020sd16p.reset();
}

void loop() {
//Plays synchronously an audio file. Busy pin is used for this method.
wtv020sd16p.playVoice(0);
//Plays asynchronously an audio file.
wtv020sd16p.asyncPlayVoice(1);
//Plays audio file number 1 during 2 seconds.
delay(2000);
//Pauses audio file number 1 during 2 seconds.
wtv020sd16p.pauseVoice();
delay(2000);
//Resumes audio file number 1 during 2 seconds.
wtv020sd16p.pauseVoice();
delay(1000);
//Stops current audio file playing.
wtv020sd16p.stopVoice();
//Plays synchronously an audio file. Busy pin is used for this method.
wtv020sd16p.asyncPlayVoice(2);
delay(2000);
//Mutes audio file number 2 during 2 seconds.
// wtv020sd16p.mute();
// delay(1000);
// //Unmutes audio file number 2 during 2 seconds.
// wtv020sd16p.unmute();
// delay(2000);
//Stops current audio file playing.
wtv020sd16p.pauseVoice();
delay(2000);
//Resumes audio file number 1 during 2 seconds.
wtv020sd16p.pauseVoice();
delay(1000);
wtv020sd16p.stopVoice();
wtv020sd16p.asyncPlayVoice(3);
delay(2000);
wtv020sd16p.pauseVoice();
delay(2000);
//Resumes audio file number 1 during 2 seconds.
wtv020sd16p.pauseVoice();
delay(1000);
wtv020sd16p.stopVoice();
}

sound_WTV.ino (1.74 KB)

I can't see how you would sync the music to the beak, but if you just want it to open, then move the mouth servo, play the music, close the mouth.

If the comments in your code are accurate, it looks like you would want to begin playing the sound asynchronously and then do your servo move. If you want the second sound to begin playing as soon as the first one is finished, you could possibly combine them into one file. If that is not an option, you will have to figure out when the first sound is over (does the library provide a method for this? Something like a busy() function?) and then begin playing the second sound asynchronously.

Thanks for the reply guys,

the module has a busy function it can play the enxt sound after the beak is closed. My question is how do I combine the codes can you help me a technique that I have to use to combine the both of them? thanks again.

I told you how in reply #2. Start playing the sound asynchronously, and then do you servo move.

Start with that. When you get it working, figure out how to play the second sound.

If you get stuck, post your code and ask for help.

Don't ask people to write your code for you.

Hello blh64

Thanks for your help,

I have done as you told me and the servo is shaking now it doesn't even move as it was supposed to and the sound is not playing as intended there is something wrong with that.
The thing is when I combined them they both stopped working properly but there is a little hope look at how I did my code on the

  1. first code I did the sound code at first and the servo code later but there is no movement and no sound

#include <Servo.h>
#include<Wtv020sd16p.h>

int pos = 165; // variable to store the servo position
int resetPin = 2; // The pin number of the reset pin.
int clockPin = 3; // The pin number of the clock pin.
int dataPin = 4; // The pin number of the data pin.
int busyPin = 5; // The pin number of the busy pin.

Wtv020sd16p wtv020sd16p(resetPin,clockPin,dataPin,busyPin);
Servo myservo; // create servo object to control a servo

void setup() {
wtv020sd16p.reset();
myservo.attach(6); // attaches the servo on pin 6 to the servo object
}

void loop () {
wtv020sd16p.playVoice(0);
//Plays asynchronously an audio file.
wtv020sd16p.asyncPlayVoice(1);
//Plays audio file number 1 during 2 seconds.
delay(5000);
//Pauses audio file number 1 during 2 seconds.
wtv020sd16p.pauseVoice();
delay(500);
//Stops current audio file playing.
wtv020sd16p.stopVoice();
delay(500);

while (pos=165){
for (pos = 165; pos >=65; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos);// tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}

for (pos = 65; pos <= 165; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

  1. The second one I did the servo beak open first and then the sound and the close the beak of the parrot, and here the servo is shaking like from half open of the beak down in a very fast speed and no sound or sometimes a little half sound of the original sound file.

#include <Servo.h>
#include<Wtv020sd16p.h>

int pos = 165; // variable to store the servo position
int resetPin = 2; // The pin number of the reset pin.
int clockPin = 3; // The pin number of the clock pin.
int dataPin = 4; // The pin number of the data pin.
int busyPin = 5; // The pin number of the busy pin.

Wtv020sd16p wtv020sd16p(resetPin,clockPin,dataPin,busyPin);
Servo myservo; // create servo object to control a servo

void setup() {
wtv020sd16p.reset();
myservo.attach(6); // attaches the servo on pin 6 to the servo object
}

void loop () {

while (pos=165){
for (pos = 165; pos >=65; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos);// tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
wtv020sd16p.playVoice(0);
//Plays asynchronously an audio file.
wtv020sd16p.asyncPlayVoice(1);
//Plays audio file number 1 during 2 seconds.
delay(5000);
//Pauses audio file number 1 during 2 seconds.
wtv020sd16p.pauseVoice();
delay(500);
//Stops current audio file playing.
wtv020sd16p.stopVoice();
delay(500);

for (pos = 65; pos <= 165; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

Here is all my codes and still struggling to make them work please let me know if there is any changes that I should make thank you.

This is wrong

 while (pos=165){
    for (pos = 165; pos >=65; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);// tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }

You are assigning the value of 165 to the variable 'pos' inside the while(). You should be testing for equality '==' instead.

Actually, you really don't need the while() loop at all. Your for() loop does the entire movement.

Hello good morning mate,

oh yeah I sent u that but I have even tried it without the while () function, the servo doesn't move and so doesn't the sound.
I was thinking may be it is because the delays, I tried it without the delays and no much difference not working. I will try to invest my time today and I will see what difference I make and if you find something let me know. thanks again for your help.

Best regards

Hello blh64

After a long experiment I think the problem is on the libraries or something to so with the interference. What I meant is that I was trying the code one by one and the servo stop working when I include the following line to the servo code I don't know why but can you look at this please?

#include <Servo.h>
#include <Wtv020sd16p.h>

Servo myservo; // create servo object to control a servo
int pos=165;
int pos_dir = 0;
unsigned long interval = 30;
unsigned long previousMillis=0;
int resetPin = 2; // The pin number of the reset pin.
int clockPin = 3; // The pin number of the clock pin.
int dataPin = 4; // The pin number of the data pin.
int busyPin = 5; // The pin number of the busy pin.

Wtv020sd16p wtv020sd16p(resetPin,clockPin,dataPin,busyPin);

void setup() {
interval= random(300);
myservo.attach(6); // attaches the servo on pin 6 to the servo object
wtv020sd16p.reset();
}

the code line before setup is what is making the servo not to work. which is declaring the Wtv020sd pins.
Wtv020sd16p wtv020sd16p(resetPin,clockPin,dataPin,busyPin); this line.

Please use code tags when posting your code. It helps others help you.
I don't know what library you are using because this one doesn't have a reset() function. It was removed

I don't see any obvious conflicts in the library with timers, etc. so this basic code should work

#include <Servo.h>
#include<Wtv020sd16p.h>

int pos = 165;    // variable to store the servo position
const int resetPin = 2;  // The pin number of the reset pin.
const int clockPin = 3;  // The pin number of the clock pin.
const int dataPin = 4;  // The pin number of the data pin.
const int busyPin = 5;  // The pin number of the busy pin.

Wtv020sd16p wtv020sd16p(resetPin, clockPin, dataPin, busyPin);
Servo myservo;  // create servo object to control a servo

void setup() {
  wtv020sd16p.reset();
  myservo.attach(6);  // attaches the servo on pin 6 to the servo object
}

void loop() {

  wtv020sd16p.asyncPlayVoice(1);      //Plays asynchronously an audio file.

  for (pos = 165; pos >= 65; pos--) {
    myservo.write(pos);
    delay(15);
  }

  // maybe start playing second sound here?

  for (pos = 65; pos <= 165; pos++) {
    myservo.write(pos);
    delay(15);
  }
}

Hello mate,

Sorry I didn't let u know yesterday it worked the problem was not on the code actually, the thing is I had an Arduino nano in a real connection but I was testing it using Arduino Uno so I didn't had a common ground for the WVT Sd card and the servo with the Arduino but then when I grounded them to the Arduino they worked.
And now I got a little problem because I had to change to the Nano, and when I changed it to the nano and uploaded the same sketch that worked on Uno, it is not working perfectly as in the Uno. The servo is working as intended but the first sound is not playing only the second sound is playing and I will try to make some changes.
In the mean time if you think there is a difference with the sketches of Uno and Nano please give me some hint. I really appreciate to help and the time you gave me mate, thank you.

Best Regards

Very common mistake. That is why a schematic or picture of a schematic is usually a very useful piece of information to share when asking for help. As for UNO vs nano, they are the same chip so should behave the same. You must select the nano in the IDE as the target for your build (vs. selecting a Uno). Did you do that?

Yes I did that but the servo speed looks different than using the Uno, but it is all good it works good. Thank you for your time.