How to block sensor input while program is running?

I am working on very simple code that makes Arduino to play single mp3 file when cap. sensor is touched. So far all is well, but if the sensor is touched again during the play the play starts over from the beginning. And that is not what I need to happen. I need the file play to the end and then wait for next touch.
I have not done any coding for a year or more and seem to forgot a lot.

BTW: It has been asked on forums how to use very cheap mp3 shield SV5W with Arduino and I found excellent library that takes care of this done by SnijderC. Thank you, Chris.

#include <Arduino.h>
#include "DYPlayerArduino.h"
DY::Player player;

// constants won't change. They're used here to set pin numbers:
const int SENSOR_PIN = 8; // the Arduino's input pin that connects to the sensor's SIGNAL pin 

// Variables will change:
int lastState = LOW;      // the previous state from the input pin
int currentState;         // the current reading from the input pin

void setup() {
   player.begin();
  player.setVolume(10); // 0-50 Volume
   player.setCycleMode(DY::PlayMode::Sequence); // play all and stop
  
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // initialize the Arduino's pin as aninput
  pinMode(SENSOR_PIN, INPUT);
}

void loop() {
  // read the state of the the input pin:
  currentState = digitalRead(SENSOR_PIN);

  if(lastState == LOW && currentState == HIGH)
    player.play();

  // save the the last state
  lastState = currentState;
}

You can stop reading the input during play.

OK, the SV5W provides “busy” pin. Am I right to assume that Arduino could digitalRead it as HIGH while in play and LOW when stopped. Unfortunately there is no clear(for me at least) explanations about this mp3 player.

use a variable to indicate the playing state: ex:

bool isPlaying = false;

And then when plays:

if(isPlaying == false) {
   player.play();
   sPlaying = true;
}

Thanks!
The code I have now starts to play when touched, doesn’t react to touch while playing…but will not start again when file is played. Something I miss - probably I need to turn isPlaying to false again…or…

#include <Arduino.h>
#include "DYPlayerArduino.h"
DY::Player player;

// constants won't change. They're used here to set pin numbers:
const int SENSOR_PIN = 8; // the Arduino's input pin that connects to the sensor's SIGNAL pin
const int SENSOR_PIN1 = 12;

// Variables will change:
int lastState = LOW;      // the previous state from the input pin
int currentState;    // the current reading from the input pin
bool isPlaying = false;
void setup() {
  player.begin();
  player.setVolume(15); // 0-50 Volume
  player.setCycleMode(DY::PlayMode::Sequence);


  // initialize the Arduino's pin as aninput
  pinMode(SENSOR_PIN, INPUT);
  pinMode(SENSOR_PIN1, INPUT);
}

void loop() {
  // read the state of the the input pin "touch":
  currentState = digitalRead(SENSOR_PIN);

  if (lastState == LOW && currentState == HIGH)

   if(isPlaying == false) {
   player.play();
   isPlaying = true;
}

  // save the the last state
  lastState = currentState;

}

The variable "isPlaying" is never reset.

But the API has a method (checkPlayState) which you can use instead of your own variable.

I figured that is happening.
It seems to work now:

#include <Arduino.h>
#include "DYPlayerArduino.h"
DY::Player player;

// constants 
const int SENSOR_PIN = 8; // the Arduino's input pin that connects to the sensor's SIGNAL/IO pin


// variables
int lastState = LOW;      // the previous state from the input pin
int currentState;    // the current reading from the input pin

bool isPlaying = false;


void setup() {
  player.begin();
  player.setVolume(25); // 0-30 Volume
  player.setCycleMode(DY::PlayMode::Sequence);


  // initialize the Arduino's pin as aninput
  pinMode(SENSOR_PIN, INPUT);
}

void loop() {
  // read the state of the the input pin "touch":
  currentState = digitalRead(SENSOR_PIN);

  if (lastState == LOW && currentState == HIGH)

   if(isPlaying == false) {
   player.play();
   isPlaying = true;
 
   
}

  // save the the last state
  lastState = currentState;


isPlaying == false; //resetting the variable "isPlaying"
return;
   
}

Yes I saw that in the API. However I was not able to find/figure how to call DY::DYPlayer::checkPlayState(…) and get the state DY::PlayMode::Playing.
I feel getting lost here.

You can try this:

    if (player.checkPlayState() != DY::PlayState::Playing) {
      player.play();
    }

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