Need help with: WAV player + RF communication

Hey guys!
This is my first time here in the forum, and straight to the point:
I’m building a project with some different stuff in: RF communication, two arduinos, a potentiometer, a SD card reader and a speaker.
The first Arduino is the Transmitter: an analog port reads the potentiometer and sends the value to the receiver. If it is around 0-100 sends the letter H, if it is around 300-400 another letter and so on.
The second Arduino is the receiver:
It receives the message, and then should play a WAV file from the SD card depending on the letter it has received.
Well, my goal is to play a WAV file according to my moves in the potentiometer and this was the best way I could think of… but I’m a beginner so I’m sorry if It already has any mistakes or something like.

Unfortunately, It doesn’t play the WAV file… and I think it might be some interference between the audio and the RF, or my code, or I don’t know.
I’ve read a lot over the Internet about both of these technology, but it seems I’m not able to solve it by my own… so I was thinking if any of you could help me with that.

Some stuff you should know:

  • I’ve already converted the audio, and in a less complicated code I can play it easily, so, I think it discards any problems with the SD reader, the audio track or my wiring;
  • I’ve already tried to change the audio output and it doesn’t work if it’s not wired at PWM 9;
  • I read something about the TIMER in advanced library from TMRpcm: we can change the audio thing to TIMER2 if TIMER1 is already in use (maybe for the RF) so I wrote up in the code #define USE_TIMER2, and still didn’t work;

My two codes:
TRANSMITTER

#include <VirtualWire.h>
int potenciometro = 0; // potentiometer
int valor = 0;  //potentiometer value
const char *mar = "h";
const char *son = "e";
const char *pqp = "l";


void setup() {
 Serial.begin(9600);

   // Initialise the IO and ISR
   vw_set_ptt_inverted(true); // Required for DR3100
   vw_setup(2000); // Bits per sec
}

void loop() {
 valor = analogRead(potenciometro); //Recebe o valor do potenciômetro
 Serial.println(valor);
 if(valor > 0){
   if (valor > 500 && valor<600)
   {
    
   digitalWrite(13, true); // Flash a light to show transmitting
   vw_send((uint8_t *)mar, strlen(mar));
   vw_wait_tx(); // Wait until the whole message is gone
   digitalWrite(13, false);
   delay(200);
   
   }
   if (valor > 0 && valor <100)
   {
   
   digitalWrite(13, true); // Flash a light to show transmitting
   vw_send((uint8_t *)son, strlen(son));
   vw_wait_tx(); // Wait until the whole message is gone
   digitalWrite(13, false);
   delay(200);
   
   }
   if (valor > 100 && valor <200)
   {
     
   digitalWrite(13, true); // Flash a light to show transmitting
   vw_send((uint8_t *)pqp, strlen(pqp));
   vw_wait_tx(); // Wait until the whole message is gone
   digitalWrite(13, false);
   delay(200);
   
   }
 }
 
   
 
}

And now RECEIVER:

#include <VirtualWire.h>

#include <SD.h>                    
#define SD_ChipSelectPin 4  

#include <TMRpcm.h>          

int tcc=0;
TMRpcm tmrpcm;  
TMRpcm audio;

void setup()
{
   tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc
   Serial.begin(9600); // Debugging only
   Serial.println("setup");
   
 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
   
   tmrpcm.volume(1);
 }
 
 

 vw_set_ptt_inverted(true);
 vw_setup(2000);
 vw_set_rx_pin(8); 
 vw_rx_start(); 
 

 
}

void loop()
{
 

 
   uint8_t buf[VW_MAX_MESSAGE_LEN];
   uint8_t buflen = VW_MAX_MESSAGE_LEN;
   
   tcc =0;
   if (vw_get_message(buf, &buflen)) // Non-blocking
   {
int i;
       digitalWrite(13, true); // Flash a light to show received good message
Serial.print("Got: "); 
for (i = 0; i < buflen; i++)
{
   Serial.println(buf[i ]);
          if(buf[i ] == 101)
           {
             Serial.println("Mario");
             tcc =1;
           }
            if(buf[i ] == 104)
           {
             Serial.println("Sonia");
             tcc =2;
           }
            if(buf[i ] == 108)
           {
             Serial.println("Pq Piracicaba");
             tcc =3;
           }  
}

audio.setVolume(6);
        
if (tcc ==1){
 tmrpcm.play("MARIOW~1.WAV");
 Serial.println("Mario AUDIO");
 delay(3000);
 audio.disable(); 
}
if (tcc ==2){
 tmrpcm.play("SONIAW~1.WAV");
   Serial.println("Sonia AUDIO");
   delay(3000);
   audio.disable(); 
}
if (tcc ==3){
 tmrpcm.play("PARQUE~1.WAV");
   Serial.println("Pq AUDIO");
   delay(3000);
   audio.disable(); 
}
       
       Serial.println("hh"); //end of loop
       digitalWrite(13, false);        
   }
}

Thanks,
Viviane
(sorry for my english, I’m Brazilian and not fluent)

Can you edit that post ( modify from the down pointing arrow not quick edit ), select the first piece of code and click on the code icon, which is next to the quote icon and looks like a scroll with a <> over it. Then repeat for the second code and then save.

Next can you say what debug output you see on the screen from the receiver when you run the transmitter code.

Basically you need to break it up as much as possible into small steps and test each step.

As I comment I don’t see why you are testing something to set a variable ( tcc ) and then testing that variable to play the file. Why not do it all in one, that is instead of setting tcc just do the code you want to when tcc is that value.

In the code, it prints in the Serial Monitor in two moments:

  • When the message from the transmitter is received;
  • When the function to play the audio is reached;
    And both appears in my Serial when I’m testing. But I can’t hear anything.

I broke the program in small pieces and rebuild it step by step. The audio stops working when I add the loop to read the message from the transmitter:

if (vw_get_message(buf, &buflen)) // Non-blocking
   {
int i;
       digitalWrite(13, true); // Flash a light to show received good message
Serial.print("Got: "); 
for (i = 0; i < buflen; i++)
{
   Serial.println(buf[i ]);
          if(buf[i ] == 101)
           {
             Serial.println("Mario");
             tcc =1;
           }
            if(buf[i ] == 104)
           {
             Serial.println("Sonia");
             tcc =2;
           }
            if(buf[i ] == 108)
           {
             Serial.println("Pq Piracicaba");
             tcc =3;
           }  
}

Grumpy_Mike:
As I comment I don’t see why you are testing something to set a variable ( tcc ) and then testing that variable to play the file. Why not do it all in one, that is instead of setting tcc just do the code you want to when tcc is that value.

Well, in a first moment I used to do it all in one. Got the message, play the audio.
This way, once the first message was received it used to stop receiving for good.
Then, I wrote the audio thing apart from the RF communication, and besides the audio still not workig, at least receiving the information works with no interruptions.

Thank you very much for making those edits.

The audio stops working when I add the loop to read the message from the transmitter:

That points to the fact that both the audio output and the virtual wire libraries use the same processor resource of timer 2. Which is not unreasonable but is bad news for you.

This means you will have to do one of the following:-

  1. Hack one of the libraries to use another timer - not easy for a beginner
  2. Find other libraries that do not use the same resource - I don’t know of any off hand
  3. Write your own code for one or other function
  4. Use an external A/D for the sound output like they do in the Ada Fruit wave shield so as not the need the change in the PWM frequency. This is similar to option 3)

everything is correct, the only thing you need to do is check the sd.begin condition where you first played tmrpcm.play.......