Servos + Speakers problems

Good evening,

Trying to get an arduino project going that has an arduino uno, SD card board, speaker amp, 3 servos and two speakers. When testing this code separately it works flawlessly, either with sound or motors but not with both. I am using two separate power supplies for the speakers/arduino and the servos to avoid the noise the motors make through the amp/speakers. When uploading this code however I cannot get any sound out, only the servos turn. Does anyone have suggestions?

Thanks.

#include "SD.h"
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"
#include <VarSpeedServo.h>

TMRpcm tmrpcm;

const int NBR_SERVOS = 3;       // the number of servos

VarSpeedServo Servos[NBR_SERVOS];        // servo objects


int servoPins[NBR_SERVOS]        = {3,     5,  6}; // servo pins
int servoSpeeds[NBR_SERVOS]      = {25,    25, 25}; // sweep speed, 1 is slowest, 255 fastest)
int servoMinPosition[NBR_SERVOS] = {55,  90,  55}; // the minumum servo angle
int servoMaxPosition[NBR_SERVOS] = {85, 120, 85}; // the maximum servo angle

void setup()
{
tmrpcm.speakerPin=9;
Serial.begin(9600);
if(!SD.begin(SD_ChipSelectPin))
{
  Serial.println("SD fail");
  return;
}
tmrpcm.setVolume(5);
tmrpcm.play("Rocky.wav");
   for(int i=0; i < NBR_SERVOS; i++) 
   {
     Servos[i].attach(servoPins[i]);       
     Servos[i].slowmove(servoMinPosition[i],servoSpeeds[i]) ; // start sweeping from min position
   }
}

void loop() 
{
  // sweep the servos
  for(int i=0; i < NBR_SERVOS; i++)
  {   
     if( Servos[i].read() == servoMinPosition[i])
       Servos[i].slowmove(servoMaxPosition[i],servoSpeeds[i]) ;         
     else if( Servos[i].read() == servoMaxPosition[i])
       Servos[i].slowmove(servoMinPosition[i],servoSpeeds[i]) ;           
  } 
}

I don't see the declaration of the chipselect pin of the SD as OUTPUT.
tmrpcm.speakerPin=9; also needs to decalred as OUTPUT put I don't find that in the code.

Thanks for your reply. Regardless of setting the pinmode to output it doesn't get the sound working. The pinmode output is set in the TMRPCM library. The addition of the servo code causes the sound to not output, so I'm trying to understand why. I can have one but not both it seems.

OK.
Can You start commenting out the servo code in parts? Comment out the code in loop(). Any change? If not, comment out servo in setup? Any change? if not, there is some conflict in the initial declarations. Pin collision, timer collision.

Have a go.

There is a timer conflict between TMRpcm and VarSpeedServo. Fortunately you can easily modify TMRpcm to use a different timer. In pcmConfig.h uncomment //#define USE_TIMER2 and that should sort it, provided there are no other conflicts.

Steve

Thank you for clarifying on that. I removed the comment marks to use Timer2 but now I get audio, however it’s all static noise from the servos.

Currently running a 6V battery pack for servo power

Completely separate 6V battery pack for:

  • Arduino
  • SD Card Board
  • Audio Amp

No cross tie on negative bus.

Here's a photo of the setup, not my proudest work but quick and dirty. 6V pack on left isn't hooked up but would be connected to left power rail.

briancnc:
No cross tie on negative bus.

Not sure what this means but if you mean you don't have a common ground between all the components then that will certainly be part of the problem.

Steve

Steve, exactly, common ground bus. But with or without the negative bus tie it still produces the same output, a ton of servo static from the speakers and moving servos, but no song playing. Any other ideas?

If the TMRpcm library uses a timer to generate PWM then when you switch it from Timer1 to Timer2 you probably also have to use one of the Timer2 PWM pins for audio.

Thanks John, let me try changing it to pin 3 or 11. I will follow-up.

John,

Set that and it worked, thank you! The volume for setting '4' is too quiet and going to '5' makes it unbearable and super noisy, is there a reason for this spike? Thanks!

briancnc:
The volume for setting '4' is too quiet and going to '5' makes it unbearable and super noisy, is there a reason for this spike?

I don't know. I'd double-check the TMRpcm library documentaation, examples, and sources. Perhaps it has something to do with Timer2 being 8-bit and Timer1 being 16-bit (you might have a smaller dynamic range on Timer2).