So I have encountered strange behaviour, sometimes if after PLAY command with one file there is a LOOP comand after a short while with another file DFPlayer loops the file it was playing in PLAY command, and not the one that was writen in LOOP command. Not always, but this does happen. Any ideas? Maybe I have missed something?
In my case it happens after SWING sound have been played and IDLE sound must be looped (at the end of the code).
Here is my code:
#include <DFPlayerMini_Fast.h>
#include <MPU6050.h>
#include <SoftwareSerial.h>
#include "Wire.h"
#include "BladeHacking.h"
SoftwareSerial ss(4,5);
const int MPU_ADDR = 0x68;
int16_t accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, temp;
float averageGyroX,averageGyroY,averageGyroZ;
MPU6050 MyGyro(MPU_ADDR);
DFPlayerMini_Fast myDFPlayer;
BladeHacking bladeHack;
#define Idle 1
#define Swing 2
#define Hit 3
#define Opening 4
#define Closing 5
#define SWITCH_PIN 10
void setup() {
pinMode(10,INPUT_PULLUP);
Serial.begin(9600);
ss.begin(9600);
pinMode(6, OUTPUT);
digitalWrite(6, HIGH);
//delay(2000);
pinMode(8, OUTPUT);
digitalWrite(BLADE_DATA_PIN, SIG_IDLE);
bladeHack.send_sig( SIG_CMD_OFF );
delay(1000);
Wire.begin();
MyGyro.initialize();
MyGyro.setFullScaleGyroRange(MPU6050_GYRO_FS_2000);
MyGyro.setFullScaleAccelRange(MPU6050_ACCEL_FS_4);
MyGyro.setAccelerometerPowerOnDelay(3);
MyGyro.setInterruptMode(true);
MyGyro.setInterruptLatch(0);
MyGyro.setIntMotionEnabled(true);
MyGyro.setDHPFMode(1);
MyGyro.setMotionDetectionThreshold(3);
MyGyro.setMotionDetectionDuration(40);
attachInterrupt(0,DetectedMotion, RISING);
bool connectedToPlayer = false;
while(!connectedToPlayer){
connectedToPlayer = myDFPlayer.begin(ss);
}
Serial.println("Connected to player");
myDFPlayer.volume(30);
delay(100);
}
void DetectedMotion(){
Serial.println("Detected motion. Detaching interrupt");
detachInterrupt(0);
}
float getGlobalAccel(){
MyGyro.getAcceleration(&accel_x,&accel_y,&accel_z);
float ax = 1.0*(accel_x+1820)/8196;
float ay = 1.0*(accel_y+4450)/8196;
float az = 1.0*(accel_z+250)/8196;
return abs(sqrt(ax*ax+ay*ay+az*az) - 1.0);
}
float getGlobalRot(){
MyGyro.getRotation(&gyro_x,&gyro_y,&gyro_z);
float gx = 1.0*(gyro_x+45)/16.4;
float gy = 1.0*(gyro_y+55)/16.4;
float gz = 1.0*(gyro_z+40)/16.4;
return sqrt(gx*gx+gy*gy+gz*gz);
}
bool bladeOn = false;
bool isOpening = false;
bool isClosing = false;
bool isSwing = false;
bool isHit = false;
bool isLooping = false;
bool moving=false;
float minD=100000;
float maxD = 0;
float zeroMotionTimer=0;
float soundTimer=0;
float startMillis=0;
int playingSound = Idle;
void loop() {
bladeHack.red_breath();
// rot thres 350
// accel thres 0.3-0.35
if(digitalRead(SWITCH_PIN) == LOW && !bladeOn && playingSound != Opening)
{
//bladeOn=true;
myDFPlayer.play(1);
playingSound = Opening;
soundTimer = 1826;
startMillis = millis();
}
if(digitalRead(SWITCH_PIN) == HIGH && bladeOn)
{
bladeOn=false;
myDFPlayer.play(2);
}
if(playingSound == Opening && millis()-startMillis > soundTimer){
myDFPlayer.loop(3);
playingSound = Idle;
soundTimer = 300;
startMillis = millis();
bladeOn = true;
}
if(bladeOn)
{
float globalAcc = getGlobalAccel();
//Serial.println(globalAcc);
if(globalAcc>3.1 && playingSound != Hit){
myDFPlayer.play(5);
playingSound = Hit;
soundTimer = 543;
startMillis = millis();
}
else if(playingSound == Idle &&(getGlobalRot()>350 || globalAcc>0.33))
{
myDFPlayer.play(4);
playingSound = Swing;
soundTimer = 633;
startMillis = millis();
}
else if(millis()-startMillis > soundTimer && playingSound != Idle)
{
myDFPlayer.loop(3);
playingSound = Idle;
soundTimer = 300;
startMillis = millis();
}
}
}