I'm struggling that my Arduino doesn't play music. Here is my circuit.
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);
}