Implementing a pause if electrode is touched again

Hi guys, I’ve run into a brick wall in terms of implementing a pause if the electrode is touched again. So instead of stopped and start from the beginning - I want to just pause it instead… I’ll post the code below - any help would be great!

// compiler error handling
#include “Compiler_Errors.h”

// touch includes
#include <MPR121.h>
#include <Wire.h>
#define MPR121_ADDR 0x5C
#define MPR121_INT 4

// mp3 includes
#include <SPI.h>
#include <SdFat.h>
#include <FreeStack.h>
#include <SFEMP3Shield.h>

// mp3 variables
SFEMP3Shield MP3player;
byte result;
int lastPlayed = 0;

// mp3 behaviour defines
#define REPLAY_MODE TRUE // By default, touching an electrode repeatedly will
// play the track again from the start each time.
//
// If you set this to FALSE, repeatedly touching an
// electrode will stop the track if it is already
// playing, or play it from the start if it is not.

// touch behaviour definitions
#define firstPin 0
#define lastPin 11

// sd card instantiation
SdFat sd;

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

pinMode(LED_BUILTIN, OUTPUT);

//while (!Serial) ; {} //uncomment when using the serial monitor
Serial.println(“Bare Conductive Touch MP3 player”);

if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();

if(!MPR121.begin(MPR121_ADDR)) Serial.println(“error setting up MPR121”);
MPR121.setInterruptPin(MPR121_INT);

MPR121.setTouchThreshold(40);
MPR121.setReleaseThreshold(20);

result = MP3player.begin();
MP3player.setVolume(10,10);

if(result != 0) {
Serial.print(“Error code: “);
Serial.print(result);
Serial.println(” when trying to start MP3 player”);
}

}

void loop(){
readTouchInputs();
}

void readTouchInputs(){
if(MPR121.touchStatusChanged()){

MPR121.updateTouchData();

// only make an action if we have one or fewer pins touched
// ignore multiple touches

if(MPR121.getNumTouches()<=1){
for (int i=0; i < 12; i++){ // Check which electrodes were pressed
if(MPR121.isNewTouch(i)){

//pin i was just touched
Serial.print(“pin “);
Serial.print(i);
Serial.println(” was just touched”);
digitalWrite(LED_BUILTIN, HIGH);

if(i<=lastPin && i>=firstPin){
if(MP3player.isPlaying()){
if(lastPlayed==i && !REPLAY_MODE){
// if we’re already playing the requested track, stop it
// (but only if we’re not in REPLAY_MODE)
MP3player.stopTrack();
Serial.print("stopping track ");
Serial.println(i-firstPin);
} else {
// if we’re already playing a different track (or we’re in
// REPLAY_MODE), stop and play the newly requested one
MP3player.stopTrack();
MP3player.playTrack(i-firstPin);
Serial.print("playing track ");
Serial.println(i-firstPin);

// don’t forget to update lastPlayed - without it we don’t
// have a history
lastPlayed = i;
}
} else {
// if we’re playing nothing, play the requested track
// and update lastplayed
MP3player.playTrack(i-firstPin);
Serial.print("playing track ");
Serial.println(i-firstPin);
lastPlayed = i;
}
}
}else{
if(MPR121.isNewRelease(i)){
Serial.print(“pin “);
Serial.print(i);
Serial.println(” is no longer being touched”);
digitalWrite(LED_BUILTIN, LOW);
}
}
}
}
}
}

If Sparkfun-MP3-Player-Shield-Arduino-Library/SFEMP3Shield at master · madsci1016/Sparkfun-MP3-Player-Shield-Arduino-Library · GitHub is the library that you're using, did you try the pausMusic() and resumeMusic() methods? If so, what were the results?

Note: no experience with the library so I don't know if they do what you want to achieve.

PS
1)
Please read How to use this forum - please read. - Installation & Troubleshooting - Arduino Forum, specifically point #7 about posting code.
2)
Please provide links to non-standard libraries so we don't have to guess which ones you're using.

Hi sterretje,

Thanks for the advice! I got the library from here; GitHub - BareConductive/touch-mp3: Bare Conductive MPR121 Touch-to-MP3 Example Code

I will try the pausmusic () and hope it works