Arduino sound module on RC car

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)

What is your level of understanding the code? I ask because you say you got the code elsewhere, and I believe it might help others address the problem..
I do not have the attention span to mentally go through it all :). Some things that raised an eyebrow for me are...
1) I see numerous printouts. Can you run the sketch with the car connected to the computer so you can see what is going on in the serial monitor? That would help. Try to trigger each of the printouts and see which does not respond correctly.

  1. bool bPlayStart = false, bPlayStop = false, bFinished = false, bPlayNormal = false;

Looks funny to me, I have never seen variables declared like this, but I still have a lot to learn, so it may be alright. I did however find this link How can I declare and define multiple variables in one line using C++? which suggests it might not be kosher.

   while ( bFinished ) {
      // Never ending loop - will be executed when the stop sound has played.
    }

May be fine. But it looks like a dead end in the sketch. How I read the sketch is if the current throttle drops back to 0, nothing else will ever happen. If declaring those Boolean variables like mentioned above did not actually set "bFinished" to false, it looks like the sketch would get lost in the while loop on the first iteration. Try commenting this out.

//Read throttel value
  ++numLoop;
  if (numLoop == 30000) {
    noInterrupts();
    duration = pulseIn(rc_pin, HIGH);
    interrupts();

I am guessing the interrupts being enabled and disabled are from a library, or possibly a missing interrupt routine to break out of the while loop is missing.

WAIT!!!! STOP EEEEERRRRRRRRRCCCCKKKK! (tires sequeling :slight_smile: )

Since I went to the trouble of typing everything above, I'm going to leave it there. I just read from where you got the code.

Quote from https://www.rcgroups.com/forums/showpost.php?p=36070567

"I have not tested the code with speakers as I've not got it hooked up at the moment, but I believe it should work."

IF the code is functional (try to find out from the guy that made it) did you try the adjustments he explained.

Old topic I know but I have found the start wav file will play until it is finished and no rc input will be handled until this time is elapsed.
Op should have included whatever wav files they were using to get some idea of the start wav file length.

 Serial.println("start.wav");
          while (tmrpcm.isPlaying()) {}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.