millis and lowPower ruin the functionality

Hey everyone :slight_smile: I am making a project with DFPlaer which will run on batteries. The Arduino should be shut down after ~5 minutes in which no file was played, which is the reason why I try to add millis and lowPower to the sketch. For some reason they mess everything up :

with lowPower (no millis) : the blue led keeps blinking when volumeUp is pressed even after the volume is 30 (the max). Volume down works just fine. Without lowPower it works just fine.

with millis (with lowPower) : the blue led doesn’t light after 30 anymore but the sound changes way slower (I change it by long pressing the button, when long pressed without millis the sound changes faster) and the quality of the sound is reduced.

What’s important to me is to know why the change is slower with millis. I am also curious though to know why the light doesn’t work in the first case so if someone knows I’ll appreciate it very much.

The code is long, sorry, I couldn’t shorten it without removind important parts that might hep to find the solution. Sorry and thank you in advance :slight_smile:

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include "LowPower.h"



SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;


int wakeUpPin = 2;


int switchFolderButton = 12 ;

int buttonStart = 13 ;
int buttonStop = 4 ;

int redLightPin = 5;
int greenLightPin = 6;
int blueLightPin = 7 ;


int volumeDown = 8 ;
int volumeUp = 9 ;


boolean sleeping = true ;


int randomNumber ;

boolean playShort = true ;

int numberFilesLongFolder  ;
int numberFilesShortFolder  ;



//start
void playRandom (int folder, int randomNumber) {
  myDFPlayer.playFolder(folder, randomNumber);  //Play file
  green() ;

}

//switch directory short file -> long file or long -> short
void switchDirectory () {
  playShort = !playShort ;
  blue () ;
}

//lights

void green () {
  digitalWrite (greenLightPin, HIGH);
  delay (100) ;
  digitalWrite (greenLightPin, LOW);
}
void yellow () {
  analogWrite(greenLightPin, 60);
  analogWrite(redLightPin, 255);
  delay (100) ;
  analogWrite (redLightPin, 0);
  analogWrite (greenLightPin, 0);
}
void blue () {
  digitalWrite (blueLightPin, HIGH);
  delay (100) ;
  digitalWrite (blueLightPin, LOW);
}
void red () {
  digitalWrite (redLightPin, HIGH);
  delay (100) ;
  digitalWrite (redLightPin, LOW);
}


void wakeUp()
{
}

void setup()
{


  mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  myDFPlayer.begin (mySoftwareSerial) ;


    pinMode(wakeUpPin, INPUT);   
digitalWrite (wakeUpPin, HIGH) ;


 pinMode (buttonStart, INPUT) ;
  digitalWrite (buttonStart, HIGH);

  pinMode (buttonStop, INPUT) ;
  digitalWrite (buttonStop, HIGH);

  pinMode (volumeUp, INPUT) ;
  digitalWrite (volumeUp, HIGH);

  pinMode (volumeDown, INPUT) ;
  digitalWrite (volumeDown, HIGH);

  pinMode (switchFolderButton, INPUT) ;
  digitalWrite (switchFolderButton, HIGH);

  pinMode (redLightPin, OUTPUT) ;
  pinMode (greenLightPin, OUTPUT) ;
  pinMode (blueLightPin, OUTPUT) ;


  myDFPlayer.volume(22);  //Set volume value. From 0 to 30

numberFilesLongFolder = myDFPlayer.readFileCountsInFolder(1) ;
  numberFilesShortFolder = myDFPlayer.readFileCountsInFolder(2) ;
    
}

void loop() 
{
    
    // Allow wake up pin to trigger interrupt on low.
    attachInterrupt(0, wakeUp, LOW);
    
    // Enter power down state with ADC and BOD module disabled.
    // Wake up when wake up pin is low.
    LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 
    
    // Disable external pin interrupt on wake up pin.
   detachInterrupt(0); 

   

if (!digitalRead (wakeUpPin) && sleeping) {
  delay (100) ;
  if (!digitalRead (wakeUpPin) && sleeping) {

digitalWrite (wakeUpPin, LOW) ;
sleeping = false ;

 if (playShort) {
        randomNumber = random (1, numberFilesShortFolder) ;
        playRandom(2, randomNumber) ;
      }
      else {
        randomNumber = random (1, numberFilesLongFolder) ;
        playRandom(1, randomNumber);  //Play file
      }

      delay (500) ;
  }
}


unsigned long timer ;

//buttonStart and wakeUpButton gonna be the same one, that's why I need boolean sleeping

 if (!digitalRead(buttonStart) ) {
    delay(100);
    if (!digitalRead(buttonStart) ) {

      if (playShort) {
        randomNumber = random (1, numberFilesShortFolder) ;
        playRandom(2, randomNumber) ;
      }
      else {
        randomNumber = random (1, numberFilesLongFolder) ;
        playRandom(1, randomNumber);  //Play file
      }

      delay (500) ;
    }
  }



//Pause & continue
  //Pause
  //readState = 513 --> playing
  if (!digitalRead(buttonStop) && myDFPlayer.readState () == 513 ) {
    delay(100);
    if (!digitalRead(buttonStop) && myDFPlayer.readState () == 513 ) {
      myDFPlayer.pause();
      yellow () ;
      delay (500);

    }
  }
  
   //Continue
    //readState = 514 --> stopped

  if (!digitalRead(buttonStop) && myDFPlayer.readState () == 514 ) {
    delay(100);
    if (!digitalRead(buttonStop) &&  myDFPlayer.readState () == 514 ) {
      myDFPlayer.start();
      green () ;
      delay (500);
    }
  }

  
  //switch folder
  if (!digitalRead(switchFolderButton) ) {
    delay (100) ;
    if (!digitalRead(switchFolderButton) ) {
      switchDirectory () ;
      delay (800) ;
      }
    }


   //Volume :
  //Volume Up
  if (!digitalRead(volumeUp) && myDFPlayer.readVolume () < 30) {
    delay(100);
    if (!digitalRead(volumeUp) && myDFPlayer.readVolume () < 30 ) {
      if (myDFPlayer.readVolume() == 0) {
        myDFPlayer.volume(14);
        blue () ;
      } else {
          myDFPlayer.volumeUp();
          blue () ;
        }
      }
    }

  //Volume Down
  if (!digitalRead(volumeDown) && myDFPlayer.readVolume() >= 14) {
    delay(100);
    if (!digitalRead(volumeDown) && myDFPlayer.readVolume() >= 14 ) {
      if (myDFPlayer.readVolume() <= 14) {
        myDFPlayer.volume(0);
        blue () ;
      } else {
          myDFPlayer.volumeDown();
          blue () ;
        }
      }
    }

if ((myDFPlayer.readState () == 512 || myDFPlayer.readState () == 514) && turnOff == false ) {
  timer = millis () ;
  turnOff = true ;
}


 // I still didn't make the code to reset the arduino after 5 minutes when no file is played, I want to fix the problems which came before that, the code is therefore still unfinished


}

Tidy up your code fist. The IDE has help for that: Tools -> AutoFormat!

with lowPower (no millis) : the blue led keeps blinking when volumeUp is pressed even after the volume is 30 (the max). Volume down works just fine. Without lowPower it works just fine.

I doubt that given above description of your hardware is complete. Your sketch will just do nothing because it never wakes up from sleep. Maybe you have some circuit to wake it but we don't know of it. Post complete wiring diagram of your setup!

with millis (with lowPower) : the blue led doesn't light after 30 anymore but the sound changes way slower (I change it by long pressing the button, when long pressed without millis the sound changes faster) and the quality of the sound is reduced.

If you change your program to use millis() you have to eliminate all calls of delay(). Of course this means you have to restructure your complete sketch but believe me, it's worth the time.