Play simple Audio Sample + Servo

Hello everybody,

i am having a little bit of problem and am not quite sure how to solve it:
I want to have a servo that moves in one direction and when it returns slownly to its original position having a sound played.
I didn’t get that far into the syncronisation as I cannot really make the 2 work together.

Because when playing the Audio Sample I always got an error on the Servo Library about the Vector 11, I am now using the ServoTimer2.

Still when combining the two codes together, either there is only the audio playing, or only the servo moving.

here is my code:

#include <PCM.h>
#include <ServoTimer2.h>


#define pitchPin 2

ServoTimer2 servoPitch;


const unsigned char sample[] PROGMEM = {
126, 127, 128, 128, 128, 128, 127, 127, 127, 127, 127, 127, 128, 128, 128, 128, 128, 128, 128, 127, 126, 126, 126, 126, 126, 127, 127, 127, 127, 127, 128, 129, 130, 130, 129, 129, 129, 129, 129, 129, 128, 128, 127, 127, 126, 125, 125, 124, 125, 125, 125, 125, 126, 126, 127, 128, 129, 130, 130, 130, 129, 129, 129, 129, 129, 129, 129, 129, 130, 130, 130, 130, 130, 130, 130, 130, 130, 129, 127, 126, 125, 126, 126, 125, 124, 124, 124, 124, 124, 124, 124, 125, 125, 126, 126, 126, 127, 128, 129, 130, 130, 131, 131, 130, 129, 129, 129, 129, 128, 126, 125, 124, 124, 125, 125, 125, 125, 125, 125, 125, 125, 126, 126, 127, 128, 128, 129, 129, 129, 129, 130, 130, 129, 129, 130, 131, 132, 132, 130, 129, 128, 129, 129, 130, 130, 129, 129, 129, 129, 128, 127, 127, 126, 127, 127, 127, 128, 128, 128, 128 };
// that is just a very small part of the audio code

void setup()
{
  startPlayback(sample, sizeof(sample));
  servoPitch.attach(pitchPin);
}

int incPulse(int val, int inc){
  if( val + inc  > 2000 )
     return 1000 ;
  else
      return val + inc;  
}

void loop()
{
int val;

  val =  incPulse( servoPitch.read(), 2);
  servoPitch.write(val);
 
  delay(10);  
}

I would very much appreciate any suggestions and help.

Thank you in advance!

These are two very demanding tasks and I am not sure you can do them at the same time with libraries. You might be a able to do them if you write your own code but I don't know.

I would get some hardware help for one of the tasks, like an external chip to drive the servo or external board to produce the sound.

In this project I used a cheap sound effects generator to create sounds but you are limited to the sounds you can get in this sort of device.

http://www.thebox.myzen.co.uk/Hardware/Crazy_Golf.html

Hello Mike,

thanks a lot for sharing your project. Cool stuff!

the sound quality and lenght is not that important for the project. the 4 sec. that I have in the sketch are enough. But making the two work together seems maybe not possible without some extra-hardware.

Am now thinking of using an "commercial" mp3 player for the sound and then sync it with the servo... but not that sure how to programm this though..

but not that sure how to programm this though..

It depends on the actual unit, but normally it is done by sending either serial data or raising or lowering a logic level pin connected to the unit.