1

I'm struggling that my Arduino doesn't play music. Here is my circuit.enter image description here It proforms normally when I only connect this circuit to Arduino UNO. But when I change the whole circuit to Arduino Mega the music trues into noise. I've followed the tutorial to change the SD card adapter pin to the suggested pin.

Is there any possibility that the circuit is obstructed by servo motor which also connected to this Arduino mega? Here's my code.
#include <PS2X_lib.h>
#include <Servo.h>

#include "SD.h" //Lib to read SD card
#include "TMRpcm.h" //Lib to play auido
#include "SPI.h" //SPI lib for SD card

#define SD_ChipSelectPin 53 //Chip select is pin number 53
TMRpcm music; //Lib object is named "music"

Servo MG996;
Servo MG90B;
Servo MG90F;
PS2X ps2x; 
int error;

const int enA = 7;
const int In1 = 39;
const int In2 = 41;
const int enB = 6;
const int In3 = 43;      
const int In4 = 45;


int MG90B_pos = 90;
int MG90F_pos = 90;

void setup() {
  //music.speakerPin = 4; //Auido out on pin 4
  Serial.begin(9600);

  do { 
    //GamePad(clock, command, attention, data, Pressures?, Rumble?)
    error = ps2x.config_gamepad(11, 9, 10, 8, true, true); 
    if (error == 0) { Serial.print("Asian Big Cannon is activated!");break; } 
    else { delay(100); } 
  } while (1); 

  pinMode(enA, OUTPUT);
  pinMode(In1, OUTPUT);
  pinMode(In2, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(In3, OUTPUT);
  pinMode(In4, OUTPUT); 

  MG996.attach(32);
  MG90B.attach(31);
  MG90F.attach(30);

  MG996.write(90);
  MG90B.write(MG90B_pos);
  MG90F.write(MG90F_pos);

 /* 
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
music.setVolume(3);   //   0 to 7. Set volume level
music.quality(2);
*/
}

void mfront(){
  digitalWrite(In1, HIGH);
  digitalWrite(In2, LOW);
  digitalWrite(In3, HIGH);
  digitalWrite(In4, LOW);
  Serial.println("front");
}
void mstop(){
  digitalWrite(In1, LOW);
  digitalWrite(In2, LOW);
  digitalWrite(In3, LOW);
  digitalWrite(In4, LOW);
  //Serial.println("stop");
}
void mback(){
  digitalWrite(In1, LOW);
  digitalWrite(In2, HIGH);
  digitalWrite(In3, LOW);
  digitalWrite(In4, HIGH);
  Serial.println("back");
}
void mright(){
  digitalWrite(In1, HIGH);
  digitalWrite(In2, LOW);
  digitalWrite(In3, LOW);
  digitalWrite(In4, HIGH);
  Serial.println("right");
}
void mleft(){
  digitalWrite(In1, LOW);
  digitalWrite(In2, HIGH);
  digitalWrite(In3, HIGH);
  digitalWrite(In4, LOW);
  Serial.println("left");
  }


void loop(){

  music.play("JC.wav");
  Serial.println("playing...."); 
  delay(10000); 
}



5
  • 1. Please include your full code or a working minimal example, that shows your problem. This code wouldn't even compile. 2. Yes, the servo can interfere with the library, if it also uses Timer1 as the TMRpcm library as default on the Mega
    – chrisl
    Jan 1 '20 at 15:27
  • I've updated my code. This is a machine controlled by the PS2 handle. I delete some codes that refer to servo motor control. If it is the TMRpcm library problem, how can I solve it? Thanks! Jan 1 '20 at 15:57
  • Where's the circuit? It doesn't show up.
    – CrossRoads
    Jan 1 '20 at 16:38
  • It's not clear whether your arduino UNO is actually plugged into the breadboard, or not. If it is, then you are shorting all of the pins - not a good idea!
    – MichaelT
    Jan 1 '20 at 17:43
  • and do you wire it to SPI pins 50, 51, 52 on Mega?
    – Juraj
    Jan 2 '20 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.