TMRpcm isPlaying function not working as expected

Hello Forum.

I've been working on a project for my anniversary, I'm trying to make a music Box. This music box is supposed to be displaying a little animation while the music sounds. I have achieved to play music using the TMRpcm library. But i can't get to display the animation. The animation works independently when in it own sketch, but somehow is failing while trying to do it simultaneously. I think it could be something about the audio.isPlaying function as the library example doesn't work either. Any suggestions on how to improve my code would be awesome as i'm a newbie. I'm using an Arduino Uno.

#include <SPI.h>
#include <SD.h>
#include <TMRpcm.h>
#include <LedControl.h>
LedControl lc = LedControl(11, 13, 8, 1);
#define SD_ChipSelectPin 10
TMRpcm audio;
int i = 0;
int numeroCancion;
char archivo[20];
unsigned long time = 0;


void setup() {

  Serial.begin(9600);
  audio.speakerPin = 9;
  lc.shutdown(0, false);
  lc.setIntensity(0, 8);
  lc.clearDisplay(0);
  randomSeed(analogRead(0));
  numeroCancion = random(43);
  sprintf(archivo, "%d.wav", numeroCancion);
  if (!SD.begin(SD_ChipSelectPin)) {
    Serial.println("SD fail");
    return;
  }
 else {
  Serial.println("SD ok");   
  Serial.println (archivo);
  audio.setVolume(6);
  audio.play(archivo);
}
}




void loop() {
  unsigned long tiempovivo = millis();
  byte a = B00011000;
  byte b[3] = {B00111100, B00111100, B00111100};
  byte c[6] = {B00100100, B01111110, B01111110, B01111110, B00111100, B00011000};
  byte d[7] = {B01100110, B11111111, B11111111, B01111110, B01111110, B00111100, B00011000};
  if ( audio.isPlaying() && tiempovivo - time > 250 ) {
    switch (i) {
      case 0:
        lc.clearDisplay(0);
        lc.setRow(0, 3, a);
        i = 1;
        break;
      case 1:
        lc.clearDisplay(0);
        lc.setRow(0, 2, b[0]);
        lc.setRow(0, 3, b[1]);
        lc.setRow(0, 4, b[2]);
        i = 2;
        break;
      case 2:
        lc.clearDisplay(0);
        lc.setRow(0, 1, c[0]);
        lc.setRow(0, 2, c[1]);
        lc.setRow(0, 3, c[2]);
        lc.setRow(0, 4, c[3]);
        lc.setRow(0, 5, c[4]);
        lc.setRow(0, 6, c[5]);
        i = 3;
        break;
      case 3:
        lc.clearDisplay(0);
        lc.setRow(0, 1, d[0]);
        lc.setRow(0, 2, d[1]);
        lc.setRow(0, 3, d[2]);
        lc.setRow(0, 4, d[3]);
        lc.setRow(0, 5, d[4]);
        lc.setRow(0, 6, d[5]);
        lc.setRow(0, 7, d[6]);
        i = 4;
        break;
      case 4:
        lc.clearDisplay(0);
        lc.setRow(0, 1, c[0]);
        lc.setRow(0, 2, c[1]);
        lc.setRow(0, 3, c[2]);
        lc.setRow(0, 4, c[3]);
        lc.setRow(0, 5, c[4]);
        lc.setRow(0, 6, c[5]);
        i = 5;
        break;
      case 5:
        lc.clearDisplay(0);
        lc.setRow(0, 2, b[0]);
        lc.setRow(0, 3, b[1]);
        lc.setRow(0, 4, b[2]);
        i = 6;
        break;
      case 6:
        lc.clearDisplay(0);
        lc.setRow(0, 3, a);
        i = 7;
        break;
      case 7:
        lc.clearDisplay(0);
        i = 0;
        break;
    }
    time = tiempovivo;
  
  }
  }

Check the TMRpcm and LedControl libraries for resource conflicts (timers, interrupts turned off, etc.).

Looks like you may be using the same pins (SPI) for the SD card and the LEDs, too. It is always wise to mention which Arduino you have.

I've already checked. Only found this

Boards like Uno only have one 16-bit timer. #define USE_TIMER2 can be uncommented in pcmConfig.h if TIMER1 is required for something else.

When I uncommented the SD card read Fails.

I've been looking for info everywhere but found nothing. Also as I said not even the examples were working as expected.

LedControl lc = LedControl(11, 13, 8, 1);

Pins 11 and 13 on the Uno are SPI pins, used by the SD card. Are you certain that using them with the LedControl library is not a conflict?

For that matter, what SD card module do you have, and how have you wired it to the Uno? Does it have the required 5V to 3.3V level shifters?

as the library example doesn't work either

Which library example? Post the code you tried to run, post a wiring diagram, describe what you expected to happen and what happened instead.

Yes, I'm fairly certain, as the LedControl library also uses them as SPI. Plus the audio playback is working fine is the LED controlling that's failing.

#include <SD.h>                      // need to include the SD library
//#define SD_ChipSelectPin 53  //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin  10 /
#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;

void setup(){

  tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
  //Complimentary Output or Dual Speakers:
  //pinMode(10,OUTPUT); Pin pairs: 9,10 Mega: 5-2,6-7,11-12,46-45 
  
  Serial.begin(115200);
  pinMode(13,OUTPUT); //LED Connected to analog pin 0
  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");   
  }
  tmrpcm.play("music"); //the sound file "music" will play each time the arduino powers up, or is reset
}



void loop(){  

  //blink the LED manually to demonstrate music playback is independant of main loop
  if(tmrpcm.isPlaying() && millis() - time > 50 ) {      
      digitalWrite(13,!digitalRead(13));
      time = millis();    
  }else
  if(millis() - time > 500){     
    digitalWrite(13,!digitalRead(13)); 
    time = millis(); 
  }


  if(Serial.available()){    
    switch(Serial.read()){
    case 'd': tmrpcm.play("music"); break;
    case 'P': tmrpcm.play("temple"); break;
    case 't': tmrpcm.play("catfish"); break;
    case 'p': tmrpcm.pause(); break;
    case '?': if(tmrpcm.isPlaying()){ Serial.println("A wav file is being played");} break;
    case 'S': tmrpcm.stopPlayback(); break;
    case '=': tmrpcm.volume(1); break;
    case '-': tmrpcm.volume(0); break;
    case '0': tmrpcm.quality(0); break;
    case '1': tmrpcm.quality(1); break;
    default: break;
    }
  }

I expected for the Builtin-LED to blink and for the audio playback to be controlable via the Serial Monitor. I only got playback from the file I renamed music on the SD and nothing else. No blink and no response of any of the serial reads, but the RX LED blinked. Data was being sent and being received.

Well, I don't really know as it doesn't include any reference, just says MicroSD card adapter and was sold to me as Arduino compatible. But I don't think the module is the problem, as it's reading the SD card and is able to retrieve the files inside. A problem I'm also having is when reset button pressed the SD read fails, but when plugging and unplugging it works flawlessly.

Sure, with a 3.3V Arduino.

That is the problem. You need level shifters for the SD card to work with a 5V Arduino, or you will eventually destroy the Arduino, the card, or both.

Okay, but how does the 3.3v affects the isPlaying function? Why is it unable to detect if it's playing?

Possibly because you have destroyed the SD card, by not using level shifters. At the very least it will malfunction.

I thought that may happen, but the SD card is working okay on my PC.

Perhaps if you wire the setup properly, it will work. I recommend this level converter or similar.

Or buy an SD module with built in level converters, like this one.

If the 3.3v was such a problem why is it able to playback the wav files?

since the TMRpcm uses timer playing files in async mode

I'd turn off timer ISRs at the start of switch block, and set them back at the end

switch (i) {  // start switch
 cli();
 digitalWrite(SD_ChipSelectPin,HIGH); // explicitly de-selects the SD card adapter
 
 // case statements go here
 
 
 sei();
 }  // end switch

I tried to implement it, but it isn't working, it just stays the same, as I've told before, not even the example is working properly, could it be my arduino?

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