Hi,
I need help to get my project running.
My project is playing a mp3 with DFPlayer Mini after detecting acceleration. I use arduino uno. Volume is controlled by an poti. In order to fit all into a box a protoshield is used.
Error is following
11:06:24.451 -> ADXL345 is connected!
11:06:24.451 ->
11:06:24.451 -> DFRobot DFPlayer Mini Demo
11:06:24.483 -> Initializing DFPlayer ... (May take 3~5 seconds)
11:06:25.190 ->
11:06:25.190 -> 1.Please recheck the connection!
11:06:25.222 -> 2.Please insert the SD card!
DFPlayer is not defect. I tested it separately.
// Ausgabe an serielle Konsole
#define verbose 1
// ADXL345 Accelerator
#include<Wire.h>
#include<ADXL345_WE.h>
#define ADXL345_I2CADDR 0x53
/* There are several ways to create your ADXL345 object:
* ADXL345_WE myAcc = ADXL345_WE() -> uses Wire / I2C Address = 0x53
* ADXL345_WE myAcc = ADXL345_WE(ADXL345_I2CADDR) -> uses Wire / ADXL345_I2CADDR
* ADXL345_WE myAcc = ADXL345_WE(&wire2) -> uses the TwoWire object wire2 / ADXL345_I2CADDR
* ADXL345_WE myAcc = ADXL345_WE(&wire2, ADXL345_I2CADDR) -> all together
*/
ADXL345_WE myAcc = ADXL345_WE(ADXL345_I2CADDR);
int isMoving();
// DFPlayer - A Mini MP3 Player For Arduino
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define busyPin 10
SoftwareSerial mySoftwareSerial(12, 13); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
int isplaying = 0;
int ismoving = 0;
int numsong;
int AnzahlDateien;
int isPlaying();
// constants won't change:
// interval at which to play without acc (milliseconds)
const long interval = 20000;
// Lautstärke über Potentiometer
int Poti = A0;
int laut_raw;
int laut_new;
int laut = 20;
void setVolume();
///////////////////////////////////////////////////////////////
void setup()
{
mySoftwareSerial.begin(9600);
Serial.begin(9600);
delay(500);
// ADXL345
Wire.begin();
Serial.println();
if(!myAcc.init())
{
Serial.println("ADXL345 not connected!");
while(1);
}
else
{
Serial.println("ADXL345 is connected!");
isMoving();
}
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println();
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true);
}
Serial.println(F("DFPlayer Mini online."));
// Nummer des Ordners, der Ordner muss Daten enthalten
int Ordner = 1;
// Anzahl der Dateien im Ordner feststellen und anzeigen
AnzahlDateien = myDFPlayer.readFileCountsInFolder(Ordner);
// wenn sich im Ordner keine Daten befinden
if (AnzahlDateien == -1)
{
Serial.println("Der Ordner enth\u00e4lt keine Musikdateien!");
Serial.println("Das Programm wird beendet!");
while(1);
}
else
{
Serial.print(String(AnzahlDateien) + " Dateien im Ordner " + String(Ordner));
}
// DFPLayer starten und pausieren
pinMode(busyPin, INPUT);
// Zusätzlich LED verwenden
// LED ist an, wenn istplaying
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
///////////////////////////////////////////////////////////////
void loop()
{
unsigned long lastacc;
// Warten
delay(100);
ismoving = isMoving();
isplaying = isPlaying();
if (verbose)
{
int static tmp1 = -1;
int static tmp2 = -1;
if (ismoving != tmp1 || isplaying != tmp2)
{
Serial.print(" ismoving ");
Serial.print(ismoving);
Serial.print(" isplaying ");
Serial.println(isplaying);
tmp1 = ismoving;
tmp2 = isplaying;
}
}
// Sensor bewegt, Zeitstempel setzen
if (ismoving){ lastacc = millis(); }
// Sensor bewegt, dann Musik
if (ismoving && !isplaying){
numsong = random(1, AnzahlDateien);
if (verbose)
{
Serial.print(" numsong ");
Serial.println(numsong);
}
myDFPlayer.play(numsong);
lastacc = millis();
}
// es spielt, dann Lautstärke setzen
if (isplaying)
{
setVolume();
// myDFPlayer.volume(laut);
digitalWrite(LED_BUILTIN, HIGH);
}
// Sensor ruht und spielt
if (!ismoving && isplaying)
{
if (millis() - lastacc >= interval)
{
FadingOut();
}
}
}
int isPlaying()
{
static unsigned long lastcheck = -1;
int isplaying;
int intervall = 200;
lastcheck = millis();
if (millis() - lastcheck < intervall)
{
isplaying = !digitalRead(busyPin);
lastcheck = millis();
}
return isplaying;
}
int isMoving()
{
xyzFloat raw;
int diff;
int diffx;
int diffy;
int diffz;
static int oldx = 0;
static int oldy = 0;
static int oldz = 0;
// Status ADXL345
raw = myAcc.getRawValues();
diffx = oldx - raw.x;
diffy = oldy - raw.y;
diffz = oldz - raw.z;
oldx = raw.x;
oldy = raw.y;
oldz = raw.z;
diff = sqrt(diffx * diffx + diffy + diffy + diffz * diffz);
if (diff >= 20)
{ return 1; }
else
{ return 0; }
}
void setVolume() {
const int laut_max = 30;
static int potval_old = -1;
int potval, laut;
// ständiges Setzen der Lautstärke verursacht
// Knacken im Lautsprecher; Ursache Verstärkung
// serieller TX/RX durch DFPlayer Amp, was stört
potval = analogRead(Poti);
delay(200);
// Wert von laut kann flackern
// daher nur Änderungen > 1
if (abs(potval - potval_old) > 10)
{
laut = map(potval, 0, 1023, 0, laut_max);
myDFPlayer.volume(laut);
potval_old = potval;
if (verbose)
{
Serial.print("Wert vom Poti : ");
Serial.println(potval);
Serial.print("Lautstaerke : ");
Serial.println(laut);
}
}
}
void FadingOut() {
const int laut_max = 30;
const int steps = 5;
const int wait = 200;
int potval, laut;
// ständiges Setzen der Lautstärke verursacht
// Knacken im Lautsprecher; Ursache Verstärkung
// serieller TX/RX durch DFPlayer Amp, was stört
potval = analogRead(Poti);
laut = map(potval, 0, 1023, 0, laut_max);
Serial.println("Fading out : ");
for (int i = laut; i >= 0; i -= laut / steps)
{
Serial.print("Lautstaerke : ");
Serial.println(i);
myDFPlayer.volume(i);
delay(wait);
}
myDFPlayer.volume(0);
myDFPlayer.stop();
myDFPlayer.volume(laut);
digitalWrite(LED_BUILTIN, LOW);
}
Wiring and a picture of setup
Any idea? Wrong wiring?


