IRremote and DFPlayer in combination causes crash

Hello there,

I'm trying to make a little "sound board" like thing where each button on an old TV remote plays a sound from SD card.
DFPlayer and IRremote work soleley very well - I can recognize the received buttons, and I can play sounds from SD card.
Platform is UNO and external 5 VDC powers DFPlayer mini board plus IR diode.

But when I press a button to execute the ".play" command to playback an audio, the AVR reboots immediately. It only works when the playback is started BEFORE the IR listens to receiptions, but in this case, the remote commands aren't received anymore.
Looks to me like the play function is blocking the whole thread and this leads to troubles with the interrupts used by the IRremote library?
I do not know a way to utilize some multithreading thing on this UNO.
An option may be to switch to an ESP32.
Library versions are all up to date and IRremote code version 4.x scheme is used.

Thank you for every tip!

Nic

Code:

#include <IRremote.h>
#include "DFRobotDFPlayerMini.h"
#include <SoftwareSerial.h>

#define IR_RECV_PIN 8 // IR receiver diode pin

#define AP_RX 4
#define AP_TX 5

IRrecv irrecv(IR_RECV_PIN);
decode_results results;

DFRobotDFPlayerMini aP; // external audio player (dfplayer mini)

byte rcKeys[] = {0x67, 0x5A, 0x41, 0x61, 0x52, 0x62, 0x54, 0x53, 0x55, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x56, 0x59, 0x57, 0x63, 0x65, 0x64, 0x66, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x42, 0x4C, 0x58};

unsigned long last = millis();

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

  pinMode(IR_RECV_PIN, INPUT);

  Serial.println("boot");
  Serial.println("Available IR Codes:");
  for (int i = 0; i < 33; i++)
  {
    Serial.print("(" + String(i) + ":) ");
    Serial.print(rcKeys[i], HEX);
    Serial.print(" ");
  }

  Serial.println("\nUp and running");
  irrecv.begin(IR_RECV_PIN, ENABLE_LED_FEEDBACK);

  SoftwareSerial AudioSerial (AP_RX, AP_TX);
  pinMode(AP_RX, INPUT);
  pinMode(AP_TX, OUTPUT);
  AudioSerial.begin(9600);

  if (!aP.begin(AudioSerial, true, true)) {  // Use softwareSerial to communicate with mp3.

    Serial.println(aP.readType(), HEX);
    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."));

  aP.setTimeOut(500); //Set serial communictaion time out 500ms
  aP.volume(10);  //Set volume value. From 0 to 30
}

int assignKey(int result) {
  String key = String(result, HEX); // convert to hex
  key = key.substring(2, 4); // only use last two digits bc. dont care about long/short press
  //Serial.println("Received: ");
  //Serial.println(rcp, HEX);

  for (int i = 0; i <= 33; i++)
  {
    if (String(rcKeys[i], HEX) == key)
    {
      Serial.println("Found key: " + String(i + 1));
      aP.play(i+1);
    }
  }
}

void loop() {
  if (irrecv.decode())
  {
    if (millis() - last > 800)
    {
      //Serial.println("RESULTS:");
      assignKey(irrecv.decodedIRData.decodedRawData);

      last = millis();
      irrecv.resume(); // Receive the next value
    }
  }
  //wdt_reset();
}

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