Logic in Geeetech shield example


I am using on an Arduino Uno R3 the Geeetech VS1053 Mp3-Shield.
Datasheet: http://www.vlsi.fi/fileadmin/datasheets/vs1053.pdf
Library: GitHub - miguelmoreto/Sparkfun-MP3-Player-Shield-Arduino-Library: Modified SFEMP3Shield Lib, support OGG and Bass/Treble setting. Support Documentation can be found at the below github page for this project

I got the example “RecorderDemo” they have in the library to work, so I can record one track and play it back. But I wanted to change the inputs from buttons, to specific weights and also wanted to record multiple tracks, so I changed the code a littlebit.

This is what I changed it to:

#include <SPI.h>
#include "HX711.h"
#include <string.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

SdFat sd;
SFEMP3Shield MP3player;

#define DOUT 4
#define CLK  3

HX711 scale(DOUT, CLK);

// Using SDFatLib, limit the filenames to 8.3 format.
char trackname[] = "REC0.ogg";
char toplay[10] = {0};
char pluginfilename[] = "e44k1q05.vs"; // HiFi Voice Profile number 5: typical 87kbps

float calibration_factor = 2125;
int weight = 0;
int weightasecago = 0;
int saved_weights[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

int n=0;

unsigned long previousMillis = 0;
const long interval = 2000;
boolean found = false;
bool searched = false;
uint8_t ledstate = 0;

void setup() {
  scale.tare();  //Reset the scale to 0
  long zero_factor = scale.read_average(); //Get a baseline reading

 //Initialize the SdCard.
 if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();
 if(!sd.chdir("/")) sd.errorHalt("sd.chdir");


 // Binds a user defined function as a handler that is
 // executed at the end of recording.


void loop()
 uint16_t result = 0;
  unsigned long currentMillis = millis();

  //reading weight every second
  if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      weightasecago = weight;
      weight = scale.get_units(), 10;
      if (weight < 0) {
          weight = 0.00;
      //if weight is dropping suddenly (weight is removed), stop recording or playback
      if (weight - weightasecago <= -3) {

  //if weight is over a minimum, start checking if it is inside the list of weights, that have been previously read
  if (weight > 0 && searched == false) {
      Serial.println("searching weights...");
      for (int x = 0; x < 10; x++) {
            //if weight exists, trigger playback of the track connected to the weight
            if (saved_weights[x] == weight){
                //change int x to char y
                char y[2];
                String str;

                //build "toplay" char array
                char one[] = "REC";
                char two[] = ".ogg";
                strcat(toplay, one);
                strcat(toplay, y);
                strcat(toplay, two);
                Serial.print ("track to play ");
                found = true;
      //if weight doesn't exist, trigger recording
      if (found == false) {
      searched = true;
   // If recorder is active, read data blocks from VS1053b chipset and
   // write them to track file.
   if (MP3player.isRecording()){
        result = MP3player.doRecordOgg();
        if (result > 0){ // Toogle rec led when one or more datablock (512B) are written.
             ledstate = ~ledstate;
        //Serial.println(result); // Print the number of databloks written.
  if(Serial.available()) {
    parse_menu(Serial.read()); // get command from serial input


 * MP3 Player begin function. Restart VS1053 and load the firmware update.
void StartMP3(){  
 uint8_t result; //result code,
 result = MP3player.begin();
 //check result, see readme for error codes.
 if(result != 0) {
 Serial.print(F("Error code: "));
 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.

 * \brief Decode the Menu.
 * Parses through the characters of the users input, executing corresponding
 * MP3player library functions and features then displaying a brief menu and
 * prompting for next input command.
void parse_menu(byte key_command) {

  static uint8_t gain = 0;

  uint8_t result; // result code from some function as to be tested at later time.

  Serial.print(F("Received command: "));
  Serial.println(F(" "));

  //if s, stop the playing or recording the current track
  if(key_command == 's') {
         searched = false;
         found = false;
       Serial.println("Player stopped.");

   if (MP3player.isRecording()){
       Serial.println("Stopping recording...");
         searched = false;
         found = false;
       MP3player.stopRecording(); // Command to finish recording. 
         trackname[3] = trackname[3] + 1; //adjust trackname for next recording
  } // 's' command
  else if(key_command == 'r'){ // If 'r', start recording.
     Serial.println("Start recording");
     MP3player.stopTrack(); // Stop playing if applicable.
     saved_weights[n] = weight;
     Serial.print("  at index  ");
     Serial.print("  with weight  ");
     n = n+1; //adjust index of saved weights for next weight to be saved
   result = MP3player.startRecordOgg(trackname, pluginfilename); // Start recording
   if (result != 0){
     Serial.print("Error starting recorder: ");
  } // 'r' command
  else if (key_command == 'p'){ // If 'p', play recorded track.
     Serial.println("Restarting VS10xx..."); 
   StartMP3(); // Restart MP3 in playing mode with latest patch.
     toplay[10] = {0};
   result = MP3player.playMP3(toplay);
     searched = false;
     found = false;
   if (result != 0){
    Serial.print("Error playing track: ");
  } // 'p' command
  else if(key_command == 'G') { // Help
   if (gain < 63){
     Serial.println("Not recording.");
     Serial.print("Gain setted to: ");

  else if(key_command == 'g') { // Help
   if (gain > 0){
     Serial.println("Not recording.");
     Serial.print("Gain setted to: ");

void endRecordingHandler(void) 
 Serial.println("Record finished");

The problem is that for some reason I cannot record after playing a track anymore…
I tried it with the example code and it works, so it’s not an hardware issue, but not with the changed one.
It doesn’t even return “searching weights…” in the if (weight > 0) loop anymore, if I want to record something after I played back a track, so it doesn’t even get to run through that loop anymore.

I really have no clue how to fix this… I’m guessing there is a problem in the logic how I am triggering the parse-menu? Or is it the strcat-function? I read that there can be memory problems with that…

Please help! I need to finish this project soon!

Eternal gratitude!! :slight_smile:

btw if you think this should rather go in the audio-section, let me know! I just thought since it’s not an hardware problem, but in the trigger-logic, it’s better here

I just found out that to trigger the recording using keyboard commands works as usual before or after playing. So the problem should be somewhere in the way how I trigger rec/playback with the changing weight.

weight = scale.get_units(), 10;

The ", 10" does nothing. What did you intend for that 10 to do?!?