I trying to make a Board game that when the player goes for a space in the board it activate a reed switch with small magnet from the bottom and it plays a sound from DFplayer.
I test it with the code bellow with a button, but as the player part with the magnet will close the circuit, the dfplayer plays the file on and on
If I put the part of the code in the void setup, it will not play the mp3 for the other players.
Could someone help me, please
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
int serial1 = 10;
int serial2 = 11;
char buf;
int pausa = 0;
int equalizacao = 0;
const int buttonPin = 2;
int buttonState = 0;
SoftwareSerial mySoftwareSerial(serial1, serial2); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup()
{
pinMode(serial1, INPUT);
pinMode(serial2, OUTPUT);
pinMode(buttonPin, INPUT);
//Comu serial modulo
mySoftwareSerial.begin(9600);
//Init Arduino serial
Serial.begin(115200);
if (!myDFPlayer.begin(mySoftwareSerial))
//Definicoes iniciais
myDFPlayer.setTimeOut(500); //Timeout serial 500ms
myDFPlayer.volume(25); //Volume 5
myDFPlayer.EQ(0); //Equalizacao normal
}
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
myDFPlayer.play(1); //Play the first mp3
delay(1000);
} else {
myDFPlayer.pause(); //pause the mp3
delay(1000);
}
buttonState = LOW;
}