Go Down

Topic: Trouble shooting code mp3 shield error 1 (Read 178 times) previous topic - next topic

dark1577

I am trying to get the mp3 shield to play when the piezo element senses vibration and this code does that but only for a quick second rather than for 1min 30 seconds like its supposed to.  It shows error code 1. Can anyone help?

/**
   Sketch assumes you have MP3 files with filenames like "track001.mp3",
   "track002.mp3", etc on an SD card loaded into the shield.
*/

#include <SPI.h>

//Add the SdFat Libraries
#include <SdFat.h>
#include <FreeStack.h>

//and the MP3 Shield Library
#include <SFEMP3Shield.h>

// Below is not needed if interrupt driven. Safe to remove if not using.
#if defined(USE_MP3_REFILL_MEANS) && USE_MP3_REFILL_MEANS == USE_MP3_Timer1
#include <TimerOne.h>
#elif defined(USE_MP3_REFILL_MEANS) && USE_MP3_REFILL_MEANS == USE_MP3_SimpleTimer
#include <SimpleTimer.h>
#endif


SdFat sd; //SD object

SFEMP3Shield MP3player; //Shield object

unsigned long startPlay; //This stores the last millisecond since we had a vibration
//const unsigned int playTime = 90000;//seconds * 1000 length of play in milliseconds
const long playTime = 90000;//seconds * 1000 length of play in milliseconds
int vibration = -1;
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 100;  // threshold value to decide when the detected sound is a knock or not

// these variables will change:
int sensorReading = 0;      // variable to store the value read from the sensor pin
char trackName[] = "track001.mp3";

uint8_t result; //result code from some function as to be tested at later time.
void setup() {

  Serial.begin(115200);

  Serial.print(F("F_CPU = "));
  Serial.println(F_CPU);
  Serial.print(F("Free RAM = "));
  Serial.print(FreeStack(), DEC);  // FreeStack() is provided by SdFat
  Serial.println(F(" Should be a base line of 1028, on ATmega328 when using INTx"));


  //Initialize the SdCard.
  if (!sd.begin(SD_SEL, SPI_FULL_SPEED)) sd.initErrorHalt();

  if (!sd.chdir("/")) sd.errorHalt("sd.chdir");

  //Initialize the MP3 Player Shield
  result = MP3player.begin();
  //check result, see readme for error codes.
  if (result != 0) {
    Serial.print(F("Error code: "));
    Serial.print(result);
    Serial.println(F(" when trying to start MP3 player"));
    if ( result == 6 ) {
      Serial.println(F("Warning: patch file not found, skipping.")); // can be removed for space, if needed.
      Serial.println(F("Use the \"d\" command to verify SdCard can be read")); // can be removed for space, if needed.
    }
  }
}


void loop() {
  // Below is only needed if not interrupt driven. Safe to remove if not using.
#if defined(USE_MP3_REFILL_MEANS) \
    && ( (USE_MP3_REFILL_MEANS == USE_MP3_SimpleTimer) \
    ||   (USE_MP3_REFILL_MEANS == USE_MP3_Polled)      )

  MP3player.available();
#endif

  while (vibration == -1)
  {
    sensorReading = analogRead(knockSensor);

    // if the sensor reading is greater than the threshold:
    if (sensorReading >= threshold) {
      startPlay = millis();
      playAction(1);
      vibration = 1;
    }
  }
  //check if sufficient
  while (vibration == 1) {
    if (millis() - startPlay >   playTime) {
      sensorReading = analogRead(knockSensor);
      /*
         there is still vibration,restart playback and reset timer
      */
      if (sensorReading >= threshold) {
        startPlay = millis();
        playAction(1);
      }
    }
    /*
       no vibration, pause playing, reset sentinel
    */
    else  {
      vibration = -1;
      playAction(2);

    }
    delay(100);
  }
}
/**
   \1 to play 2 to pause
*/
void playAction(int command) {
  if (command == 1) {
    //create a string with the filename

    //tell the MP3 Shield to play that file
    result = MP3player.playMP3(trackName);
    //check result, see readme for error codes.
    if (result != 0) {
      Serial.print(F("Error code: "));
      Serial.print(result);
      Serial.println(F(" when trying to play track"));
    }

    /* Display the file on the SdCard */
  } else {
    if ( MP3player.getState() == playback) {
      MP3player.pauseMusic();
      Serial.println(F("Pausing"));
    }  else {
      Serial.println(F("Not Playing!"));
    }
  }

Go Up