Power_Broker:
I went ahead an reformatted your code so that it is a little more organized. I tried to compile it, but I couldn't find the "BladeHacking.h" library anywhere in GitHub, so you'll have to do the compile-time testing:
#include <DFPlayerMini_Fast.h>
#include <MPU6050.h>
#include <SoftwareSerial.h>
#include "Wire.h"
#include "BladeHacking.h"
#define Idle 1
#define Swing 2
#define Hit 3
#define Opening 4
#define Closing 5
#define SWITCH_PIN 10
SoftwareSerial ss(4,5);
MPU6050 MyGyro(MPU_ADDR);
DFPlayerMini_Fast myDFPlayer;
BladeHacking bladeHack;
const int MPU_ADDR = 0x68;
int16_t accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, temp;
float averageGyroX,averageGyroY,averageGyroZ;
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 setup()
{
pinMode(10,INPUT_PULLUP);
pinMode(6, OUTPUT);
digitalWrite(6, HIGH);
pinMode(8, OUTPUT);
digitalWrite(BLADE_DATA_PIN, SIG_IDLE);
Serial.begin(9600);
ss.begin(9600);
Wire.begin();
bladeHack.send_sig(SIG_CMD_OFF);
delay(1000);
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 = myDFPlayer.begin(ss);
while(!connectedToPlayer)
connectedToPlayer = myDFPlayer.begin(ss);
Serial.println("Connected to player");
myDFPlayer.volume(30);
delay(100);
}
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();
}
}
}
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(axax + ayay + 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(gxgx + gygy + gz*gz);
}
As for the library, I've double checked everything and it all checks out. It might be an issue with how you've saved your files to the SD card. A couple of things to note:
1.) Filenames do not matter - the track numbers with respect to the library are based solely on the order in which the files were saved to the SD card
2.) The folder structure of the SD card matters. Tbh, I forget exactly how it's supposed to be structured (I think you need to save all of your MP3s to a folder named "mp3" in the root directory)
Can you post the exact contents of your SD card in a zip file?
Blade hacking can be comented out with BladeHacking bladeHack; and all it method executions. It dont affect that situation. And problem is not that I cant play anything, but that I cant play anything using your library. Using DFRobot's library it plays like it should. I enden up using code to send commands directly, without any library.