Conflict with Interupts

Hi All
I am having trouble joining an interrupt based sketch with a wireless sketch. both sketches work perfectly by themselves but when i join them there is no wireless info received. It appears to be blocked.

The wireless module is the NRF24L01 and the interrupt sketch is a cut down version of the “Mojo Engine Sim”. As I said before both work separately.

//MOJO ENGINE SIM SETUP***
pinMode(SPEAKER, OUTPUT);
//startPlayback();

With the startPlayback(); line commented out the wireless signals are correctly received but with it active only the engine sim works.

This joining is key to the completion of the project so any help solving this would be appreciated…Sketch attached. (zip file…too many characters to post sketch)
thanks
Doug

Looking for more info I came across a site that says the interrupt method used for the Mojo sketch can affect the PWM outputs, the NRF24L01 is attached (required) to pins 4,10,11,12,13 (10&11) are PWM, is this the cause of the problem??? 10 can be another pin, but that still leaves 11 which is fixed.

Train_Controller_RC_RECEIVER_V2.zip (3.53 KB)

It is a lot more convenient if you attach longer programs as a .ino file rather than a .zip file

I never find it easy to follow code that uses the Atmega registers - even when I write it myself. You have a lot of register stuff. Maybe you can provide a brief explanation of what it does.

How often does the sound interrupt need to work?
What sound quality are you using?

My guess is that your interrupts are interfering with SPI communication with the nRF24. It would be interesting to put a little code at the start and end of the Radio() function to temporarily halt and restart the sound interrupts. (I do realize that may interfere with the sound quality - but we need to start the diagnosis somewhere).

Also, in the Radio() function I would replace WHILE with IF - no other change is needed. An nRF24 either has the full message or nothing.

...R
PS. I am interested in this problem - i.e. could I add sound to my Arduino controlled battery-powered radio controlled locomotives.

Hi Robin
If you can write interrupt sketches you are way ahead of me in that area.

Will change the While statement to an IF...thanks for that.

I tend to agree with you on the interference with SPI, I have since found other references pointing to that provability, Damn!!!!.

But, there is light at the end of the tunnel, the Library I mention above has given me a really good loop ability, I just need to be able to get it to use the TX RX pins, not digital alternatives...any thoughts on this or maybe another library with loop ability. HEX leaves me COLD.

As to your question, YES! it is possible, in fact that is exactly what I am doing. The multi function transmitter is finished and works very well with good range and reliability, I just have to finalize the code for the receiver side to incorporate running sound.

I run On30 (1:48)

thanks
Doug

PS: Happy New Year

WL888:
But, there is light at the end of the tunnel, the Library I mention above has given me a really good loop ability, I just need to be able to get it to use the TX RX pins, not digital alternatives...any thoughts on this or maybe another library with loop ability.

I haven't seen any mention of a library?

And I don't understand the rest of your question?

Quite apart from any possible impact on your problem I am interested in answers to my 2 question in Reply #1

How often does the sound interrupt need to work?
What sound quality are you using?

...R

Robin
sorry for the delay in getting back to you, didn't have a working answer at the time, but i think i have now.

I have been fiddling with the DFplayer and can now produce looping sounds with no gap on changeover between sounds, this is ideal for engine sound for your trains as it will be for mine.

I haven't tried inserting whistle sounds yet but i don't see any problem there.

???do you have a DFPlayer???

If so I can post the code and test sound files for you.
and some instructions on circuit.

Doug

WL888:
???do you have a DFPlayer???

Perhaps the following is a sufficient answer :slight_smile:

What is a DFPlayer?

However I would certainly appreciate it if you would post your code etc. Maybe I will feel the need to get a DFPlayer when I know what it is.

...R

Sure thing
Details [https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299](http://Wiki link)
Purchase http://www.ebay.com.au/itm/TF-Card-U-Disk-Mini-MP3-Player-Audio-Voice-Module-Arduino-DFPlayer-Mini-Board-GM-/262136735106?hash=item3d08912582:g:o90AAOSwXshWrvjt

My code:-

/***************************************************
 DFPlayer - A Mini MP3 Player For Arduino
 <https://www.dfrobot.com/index.php?route=product/product&product_id=1121>
 
 ***************************************************
 This example shows the all the function of library for DFPlayer.
 
 Created 2016-12-07
 By [Angelo qiao](Angelo.qiao@dfrobot.com)
 
 GNU Lesser General Public License.
 See <http://www.gnu.org/licenses/> for details.
 All above must be included in any redistribution
 ****************************************************/

/***********Notice and Trouble shooting***************
 1.Connection and Diagram can be found here
<https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram>
 2.This code is tested on Arduino Uno, Leonardo, Mega boards.
 ****************************************************/

/***************************************************
 DFPlayer - A Mini MP3 Player For Arduino
 <https://www.dfrobot.com/index.php?route=product/product&product_id=1121>
 
 ***************************************************
 This example shows the all the function of library for DFPlayer.
 
 Created 2016-12-07
 By [Angelo qiao](Angelo.qiao@dfrobot.com)
 
 GNU Lesser General Public License.
 See <http://www.gnu.org/licenses/> for details.
 All above must be included in any redistribution
 ****************************************************/

/***********Notice and Trouble shooting***************
 1.Connection and Diagram can be found here
<https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram>
 2.This code is tested on Arduino Uno, Leonardo, Mega boards.
 ****************************************************/

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
//#define BusyPin 16
int BusyPin = 0;
int throttle = 0;
int THROTTLE = A0;                          // potentiometer centre pin to A0
int Sound = 0;
int oldSound = 0;
int newSound = 0;
bool change;

SoftwareSerial mySoftwareSerial(2, 3); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

void setup()
{
  pinMode(A0,INPUT);                              //Throttle on A0
  pinMode(17,INPUT);                               //input for busy pin on DFPlayer (UNO A3)
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  
  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  
  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true);
  }
  Serial.println(F("DFPlayer Mini online."));

  //myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms   //----Set time out----
  myDFPlayer.volume(23);                                                  //Set volume value (0~30).
  myDFPlayer.EQ(DFPLAYER_EQ_BASS);                                      //----Set different EQ----
  myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);                            //----Set device we use SD as default----
  delay(20);
}

void loop()
{
  Throttle();
  soundSelect();
  play();
  checkPlayState();
  }

void Throttle() {
    throttle = analogRead(THROTTLE);                                          // reads throttle pot and returns value "t"                                          
    throttle=map(throttle,0,1023,0,254);
   }

void soundSelect() {
    if (throttle < 40) {
      Sound = 1;
      }
    else  if ((throttle > 36)&&(throttle<72)) {
      Sound = 2;
    }
    else  if ((throttle > 72)&&(throttle<108)) {
      Sound = 3;
    }
    else  if ((throttle > 108)&&(throttle<144)) {
      Sound = 3;
    }
    else  if ((throttle > 144)&&(throttle<180)) {
      Sound = 4;
    }
    else  if ((throttle > 180)&&(throttle<216)) {
      Sound = 5;
    }
    else  if (throttle >216){
      Sound = 6;
    }
    newSound = Sound;
          Serial.println(throttle);
      Serial.println(Sound);
    if (oldSound != newSound) {
      change = true;

   }
}
void play() {
  if (change) {
    change = false;
    myDFPlayer.play(newSound);  //Play "newSound"
    delay(100);
    oldSound = newSound; 
    Serial.println("play");
  }
}

void checkPlayState() {
    int playState = digitalRead(17);
      if(playState == 1) {                                      
        myDFPlayer.loop(oldSound);  //Loop the selected sound
        delay(100);
        //Serial.println("playstate");
    }
  }

The sound files are edited in Audacity, a FREE and very powerful audio editor, export settings that i found effective are a Project rate of 22050Hz, and a file type of WAV (Microsoft) signed 16-bit PCM (these are options in the export process). Format the micro sim card as FAT32 1024 .

There are many available sound files on the web, just depends what you are after, but these must be re-saved as WAV files in the format shown. DFPlayer is listed as being able to operate on other settings but these are the best combination i have found so far…they work!

Power the DFPlayer with an independent 5VDC power supply (not from the Arduino 5V pin) and you must connect a wire between the negative of BOTH Arduino and DFPlayer(Arduino Std practice)

Plenty of info on DFPlayer on Google.

Now that’s done…what are you using for a Transmitter and Receiver in your setup, I’m using the NRF24L01 transceiver, compact, cheep and great range and reliability, all my components came from Ebay.
Doug

Thanks for that. There is something seriously wrong with your second link - but it does not matter because I don't plan to buy one right now.

It looks like an interesting device as it takes all the load for producing the noise off the Arduino.

I have Audacity and I am using nRF24L01+ modules for my battery powered radio controlled 00 Gauge loco(s).

...R