Dear,
I have got this code for my rc sound module from
I have uploaded this file and put sound files in SD card. However, the module only plays start sound when I turn it on and there is no changes after that. I have connected Y servo wire on my receiver for ESC and Arduino.
I have tried to change the throttle value but It doesn't seem to make any difference.
I am using traxxas stampede stock 3 channel micro receiver and I have attached schematic below(I have made some changes on power source) . Thank you so much.
#include <SD.h> // need to include the SD library
#define SD_ChipSelectPin 4 //using digital pin 4 on arduino nano 328, can use other pins
#include <TMRpcm.h> // also need to include this library...
#include <SPI.h>
TMRpcm tmrpcm; // create an object for use in this sketch
unsigned long time = 0;
int numLoop = 0;
int throttle = 0;
int prevThrottle = 0;
int currThrottle = 0;
int playingSound = 0;
bool bPlayStart = false, bPlayStop = false, bFinished = false, bPlayNormal = false;
int iPlayShutdownDelay = 15; // Number of seconds to wait before shutdown sound is played.
//RC scale
int rc_pin = 2;
unsigned long duration;
unsigned long prevDuration;
unsigned long timer1;
void setup() {
tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
Serial.begin(115200);
if (!SD.begin(SD_ChipSelectPin)) { // see if the card is present and can be initialized:
Serial.println("SD fail");
return; // don't do anything more if not
}
else {
Serial.println("SD ok");
}
//PLay init file to tell sound version
//Serial.println("beginsound.wav");
//tmrpcm.play("beginsound.wav");
//while(tmrpcm.isPlaying()){
delay(2000);
Serial.println("setup end");
}
void loop() {
//Read throttel value
++numLoop;
if (numLoop == 30000) {
noInterrupts();
duration = pulseIn(rc_pin, HIGH);
interrupts();
numLoop = 0;
Serial.print("Pulse in : ");
Serial.print(duration);
Serial.print(" | currThrottle : ");
Serial.print(currThrottle);
Serial.print(" | Playing : ");
/* Code to handle statup and */
if ( ! bPlayStart ) {
currThrottle = 1; // Play statup sound
bPlayStart = true;
}
while ( bFinished ) {
// Never ending loop - will be executed when the stop sound has played.
}
if ( (millis() - timer1) > (iPlayShutdownDelay * 1000) && currThrottle == 2 ) {
currThrottle = 0;
bPlayNormal = false;
}
/* Max Reverse */
if (duration <= 990 && bPlayNormal) {
currThrottle = 7;
};
if ((duration > 990) && (duration <= 1080) && bPlayNormal) {
currThrottle = 6;
};
if ((duration > 1080) && (duration <= 1170) && bPlayNormal) {
currThrottle = 5;
};
if ((duration > 1170) && (duration <= 1260) && bPlayNormal) {
currThrottle = 4;
};
if ((duration > 1260) && (duration <= 1480) && bPlayNormal) {
currThrottle = 3;
};
/* Throttle stick center - Play idle sound */
if ((duration > 1480) && (duration <= 1520) && bPlayNormal) {
currThrottle = 2;
};
/* Forard throttle */
if ((duration > 1520) && (duration <= 1680) && bPlayNormal) {
currThrottle = 3;
};
if ((duration > 1680) && (duration <= 1770) && bPlayNormal) {
currThrottle = 4;
};
if ((duration > 1770) && (duration <= 1860) && bPlayNormal) {
currThrottle = 5;
};
if ((duration > 1860) && (duration <= 1950) && bPlayNormal) {
currThrottle = 6;
};
/* Max Forward */
if ((duration > 1950) && bPlayNormal) {
currThrottle = 7;
};
//If currThrottle != prevThrottle set start playing new file
if (currThrottle != prevThrottle) {
timer1 = millis();
if ((currThrottle == 0) && (prevThrottle > 0)) {
tmrpcm.play("shut.wav");
Serial.println("shut.wav");
while (tmrpcm.isPlaying()) {}
prevThrottle = currThrottle;
bPlayNormal = false;
bFinished = true;
}
if (currThrottle == 1) {
if (currThrottle > prevThrottle) {
tmrpcm.play("start.wav");
Serial.println("start.wav");
while (tmrpcm.isPlaying()) {}
}
bPlayNormal = true;
playingSound = 1;
prevThrottle = currThrottle;
}
if (currThrottle == 2) {
tmrpcm.play("1.wav");
Serial.println("1.wav");
prevThrottle = currThrottle;
playingSound = 1;
}
if (currThrottle == 3) {
tmrpcm.play("2.wav");
Serial.println("2.wav");
prevThrottle = currThrottle;
playingSound = 2;
}
if (currThrottle == 4) {
tmrpcm.play("3.wav");
Serial.println("3.wav");
prevThrottle = currThrottle;
playingSound = 3;
}
if (currThrottle == 5) {
tmrpcm.play("4.wav");
Serial.println("4.wav");
prevThrottle = currThrottle;
playingSound = 4;
}
if (currThrottle == 6) {
tmrpcm.play("5.wav");
Serial.println("5.wav");
prevThrottle = currThrottle;
playingSound = 5;
}
if (currThrottle == 7) {
tmrpcm.play("6.wav");
Serial.println("6.wav");
prevThrottle = currThrottle;
playingSound = 6;
}
}
// Serial.println(tmrpcm.isPlaying());
if ((currThrottle = prevThrottle) && (tmrpcm.isPlaying() == 0)) {
if (playingSound == 1) {
tmrpcm.play("1.wav");
Serial.println("again 1.wav");
prevThrottle = currThrottle;
}
if (playingSound == 2) {
tmrpcm.play("2.wav");
Serial.println("again 2.wav");
prevThrottle = currThrottle;
}
if (playingSound == 3) {
tmrpcm.play("3.wav");
Serial.println("again 3.wav");
prevThrottle = currThrottle;
}
if (playingSound == 4) {
tmrpcm.play("4.wav");
Serial.println("again 4.wav");
prevThrottle = currThrottle;
}
if (playingSound == 5) {
tmrpcm.play("5.wav");
Serial.println("again 5.wav");
prevThrottle = currThrottle;
}
if (playingSound == 6) {
tmrpcm.play("6.wav");
Serial.println("again 6.wav");
prevThrottle = currThrottle;
}
}
}
}
arduino schematic(1) (1).pdf (287 KB)