Go Down

Topic: DFPlayer unwanted autostart (Read 1 time) previous topic - next topic

Trace84

Hi everyone.

I wanted to add sound to some lighteffects I found online. The light-sketch works great. Also playing music with the DFPlayer triggered by Arduino-nano-clone works great.

But when i combine the lightsketch with serial commands for the DFPlayer mini, the programm starts the first light and soundeffect as soon as its powered. After that, it works fine and only triggers the effect when the button is pressed or hold.

When I delete the soundcommand from the loop, the behavior is the same, just without sound. And again, after the first effect beeing triggered without pushing the button, the sketch works fine (without sound of course, due to deleting the soundcommand).

So I think there is something wrong with the serial setup or the variables?! Or maybe my soundcommand is in the wrong place and it triggers something to activate the first lines when powered?

There is a similar topic, but he found out that some strands of soldering short-circuited one IO Pin. That´s not the case on my boards.


Code: [Select]
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);


// ===================== Variables =============================================================//
// -------  Declaring Pins ----------------------------------------------//
static byte disruptorPin = 9;
static byte enginePin = 6;
static byte pushButtonPin = 12;
static byte torpPin[] = { 2, 3 };

// -------  User Inputs ------------------------------------------------//
static unsigned long torpUpTime = 1750;
static unsigned long torpFlashTime = 200;
static unsigned long torpDownTime = 150;
static long double torpIdleBrightness = 10;
static long double torpMaxBrightness = 255;
static unsigned long engineFadePeriod = 3000;
static unsigned long disruptorFiringTime = 2600;

// -------  For the Button Pushing -------------------------------------//
unsigned long debounce = 30;
unsigned long holdTime = 1500;

// -------  Bookeeping ------------------------------------------------//
unsigned long currentMillis;
boolean buttonValue = false;
boolean buttonLast = false;
boolean ignoreUp = false;
unsigned long buttonUpTime;
unsigned long buttonDownTime;
unsigned int torpedoPushes = 0;
unsigned int disruptorPushes = 0;
unsigned long torpedoTime;
unsigned long disruptorTime;
unsigned long torpedoMillis;
unsigned long disruptorMillis;
unsigned long disruptorFlashTime;
boolean disruptorState = 0;
// ==================== End Variables ========================================================= //


// ===================== Setup ================================================================ //
void setup(){
  pinMode(disruptorPin,OUTPUT);
  pinMode(enginePin,OUTPUT);
  pinMode(pushButtonPin,INPUT_PULLUP);     // Enables internal pull-up resistor
  pinMode(torpPin[0],OUTPUT);
  pinMode(torpPin[1],OUTPUT);
 
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    while(true);
  }
  myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
  myDFPlayer.volume(10);  //Set volume value (0~30).
  myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
  myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
 
}


// ===================== End Setup ============================================================ //



// ===================== Main Loop ============================================================ //
void loop() {
  currentMillis = millis();         // get current time
 
 // ---------- Push Buttoon Code --------------------------------------------------------//

  buttonValue = digitalRead(pushButtonPin);
 
    // Record Time of Push
  if (buttonValue == LOW && buttonLast == HIGH && (millis() - buttonUpTime) > debounce){
    buttonDownTime = millis();
  }
 
  // Record Time of Release
  if (buttonValue == HIGH && buttonLast == LOW && (millis() - buttonDownTime) > debounce){
    buttonUpTime = millis();
    if (ignoreUp == false){
     disruptorTime = millis();        // If released early, fire disruptors!
     disruptorPushes++;
     myDFPlayer.playMp3Folder(2);
     }
     else{
       ignoreUp = false;
      }
   }

  // If push time is longer than threshold, fire Torpedos!
  if (buttonValue == LOW && (millis() - buttonDownTime) > long(holdTime)){
    buttonDownTime = millis();
    torpedoTime = millis();
    torpedoPushes++;
    ignoreUp = true;
    myDFPlayer.playMp3Folder(1);
   }
 
  // Record the button state for comparison in the next cycle
  buttonLast = buttonValue;   
 // -------------------------------------------------------------------------------------//
 
 
 
 // ---------- Torpedo Firing ----------------------------------------------------------//
 // Ramp Up Red LED, Flash White LED, then a Ramp Down of Red LED
  // Ramp Up
 
  if ((currentMillis-torpedoTime) < torpUpTime && torpedoPushes > 0){
    analogWrite(torpPin[1],torpIdleBrightness+(torpMaxBrightness-torpIdleBrightness)/torpUpTime*(currentMillis-torpedoTime));
    digitalWrite(torpPin[0], LOW);
   
  } 
  // Flash
  else if (((currentMillis-torpedoTime) > torpUpTime) && ((currentMillis-torpedoTime) < (torpUpTime+torpFlashTime)) && torpedoPushes > 0){
    analogWrite(torpPin[1], 0);
    digitalWrite(torpPin[0], HIGH);
   
  }
  // Ramp Down
  else if (((currentMillis-torpedoTime) > (torpUpTime+torpFlashTime)) && ((currentMillis-torpedoTime) < (torpUpTime+torpFlashTime+torpDownTime)) && torpedoPushes > 0){
    analogWrite(torpPin[1],torpMaxBrightness+(torpIdleBrightness-torpMaxBrightness)/torpDownTime*((currentMillis-torpedoTime)-(torpUpTime+torpFlashTime)));
    digitalWrite(torpPin[0], LOW);
   
  } 
  // Idle
  else{
    analogWrite(torpPin[1], torpIdleBrightness);
    digitalWrite(torpPin[0], LOW);
   
  }

 
 // ---------- Engine Fading ----------------------------------------------------------- //
  // Slow Up and Down Fade with Shorter Pusations
  int fadeValue = int(127.0+127.0/2.0*(1+(0.75*sin(2*PI*currentMillis/engineFadePeriod)+0.25*cos(14*PI*currentMillis/engineFadePeriod))));
  analogWrite(enginePin,fadeValue);

 
 // ---------- Disruptor Firing -------------------------------------------------------- //
  // Get Random On or Off Time Using getRandom() Function Below
  disruptorFlashTime = fmod(getRandom(), 4000UL) + 60UL;
  if (((currentMillis - disruptorTime) < disruptorFiringTime) && (disruptorPushes > 0)){
    if(currentMillis - disruptorMillis > disruptorFlashTime) {
      disruptorMillis = currentMillis; 
      digitalWrite(disruptorPin, disruptorState = !disruptorState);
      }
   }
   else{
     analogWrite(disruptorPin,5);
 
    }
 // ------------------------------------------------------------------------------------ //


}
// ===================== End Main Loop ======================================================== //




// ===================== Pseduo-Random Generator ============================================== //
// Found This Online. I Take No Credit for It.
unsigned long m_w = 1;
unsigned long m_z = 2;

unsigned long getRandom()
{
    m_z = 36969L * (m_z & 65535L) + (m_z >> 16);
    m_w = 18000L * (m_w & 65535L) + (m_w >> 16);
    return (m_z << 16) + m_w;  /* 32-bit result */
}
// ===================== End Pseduo-Random Generator ========================================== //

Go Up