Issues with Single Pushbutton dual triggering DF Player with MEGA

Hello !!!!!!

#include <DFPlayerMini_Fast.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial1(15, 14); // RX, TX
DFPlayerMini_Fast player1;
int IN22 = 22;
int IN48 = 48;
void setup() {
  // put your setup code here, to run once:
  mySerial1.begin(9600);
  if(player1.begin(mySerial1, false))
  {
  player1.volume(22);
  }
  pinMode(22, INPUT_PULLUP);
  
  pinMode(48, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(!digitalRead(22))
  {
    player1.play(1);
    while(!digitalRead(22));
  }

  if(!digitalRead(48))
  {
    player1.stop();
    while(!digitalRead(48));
  }

}

With this above simple code for playing an audio file with a single and simple Push button from a DF Player module with the help of DF Player mini Fast library

I am having this one and only issue :-

When I press the button the audio starts playing immediately with no issues and no time delay
But on releasing the button , the audio file starts again from the starting point

Means 2 time it triggers a file

Please help in solving . . Thanks !!!!

Clue: De-bounce (De-bouncing)

1 Like

twice.... You're lucky. It could have been more.
Set up an oscilloscope and monitor how switches/buttons make contact. They jump up and down plenty of times.

Kk le .e check

If we dont mention pins then how it could recognize the connectivity

What ll haopen if we change serial1 to serial 3

Serial - Arduino Reference

image

I see . . I did but result I posted !!!

OK OK Now i got it . .. .Its the latest one I did

#include <DFPlayerMini_Fast.h>
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial3(15, 14); // RX, TX
DFPlayerMini_Fast player1;
int IN22 = 22;
int LOCK = 47;
int IN48 = 48;
void setup() {
  // put your setup code here, to run once:
  Serial3.begin(9600);
  if(player1.begin(Serial3, false))
  {
  player1.volume(22);
  }
// while (!Serial3) {
//    ; // wait for serial port to connect. Needed for native USB
//  }
  pinMode(22, INPUT_PULLUP);
  
  pinMode(48, INPUT_PULLUP);

  pinMode(LOCK, OUTPUT);
  digitalWrite(LOCK,LOW);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(!digitalRead(22))
  {
    player1.play(1);
    while(!digitalRead(22));
  }

  if(!digitalRead(48))
  {
    player1.stop();
    while(!digitalRead(48));
  }

}

But still having same result !!!!!!!
Dual triggering !!!!!!

Are you reading the replies?

You need to debounce switches, usually.

You need to react to changes in the button value read, not levels, usually.

See Examples/02.Digital/StateChangeDetection in the IDE.

a7

Ya I was just thinking about the first reply of de bounce that it would get the result in real !!

Ok le me check with that !!

I did it with this one from state change detection example

#include <DFPlayerMini_Fast.h>
DFPlayerMini_Fast player1;
int count;
// this constant won't change:
const byte  buttonPin = 22;    // the pin that the pushbutton is attached to
const byte ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
boolean buttonState = 0;         // current state of the button
boolean lastButtonState = 0;     // previous state of the button
const byte LockInd = 47;   // Button Lock
const byte Stopper1 = 48;
void setup()
{
  Serial3.begin(9600);
  if(player1.begin(Serial3, false))
  {
  player1.volume(22);
  }
  pinMode (LockInd, OUTPUT);
  digitalWrite(LockInd, LOW);

   pinMode(buttonPin, INPUT_PULLUP);
   pinMode(Stopper1, INPUT_PULLUP);
   pinMode(ledPin, OUTPUT);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 100;
   if (millis() - timer >= interval)
   {
      timer = millis();
      
      buttonState = digitalRead(buttonPin);

      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            buttonPushCounter++;
         }
          lastButtonState = buttonState;
      }
  }

   if (buttonPushCounter % 2 == 0)// && buttonPushCounter > 0)
    {
      digitalWrite(ledPin, HIGH);
    }
   else
    {
      digitalWrite(ledPin, HIGH);
      player1.play(1);
    }

    if(!digitalRead(Stopper1))
    {
      player1.stop();
    }
}

Result :-

Ya its one for the first press it starts the file but the playing is completely abnormal !!

It is like , If a person Press and Release the Button with a speed of some 44 Khz then how ll be the sound get produce => Drrrrrrrrrr or Jrrrrrrrrr or Grrrrrrrr . .
(Depending on the starting point of the audio file)

Means an oscillation type thing is going on there after last ' else ' part

Then again after pressing the push button
The file starts playing normally within the ' if ' area

You are basing your reaction on the fact that the button has been pressed an odd number of times.

Which when true will be true until you press the button again.

I'm going to hand you this idea and I am then leaving you to it, I am sorry but I've followed along and I am exhausted by your progress.

I suggest you study this carefully and see where it differs, and why. But I know that is aksing quite a bit if you.

If this doesn't work, figure it out. I will entertain no protracted discussion of this, I'm about to go to the beach anyway.

#include <DFPlayerMini_Fast.h>
DFPlayerMini_Fast player1;
int count;
// this constant won't change:
const byte  buttonPin = 22;    // the pin that the pushbutton is attached to
const byte ledPin = 13;       // the pin that the LED is attached to

// Variables will change:

int buttonGotPushed;  // flag for noticing a button push 

boolean buttonState = 0;         // current state of the button
boolean lastButtonState = 0;     // previous state of the button
const byte LockInd = 47;   // Button Lock
const byte Stopper1 = 48;
void setup()
{
  Serial3.begin(9600);
  if(player1.begin(Serial3, false))
  {
  player1.volume(22);
  }
  pinMode (LockInd, OUTPUT);
  digitalWrite(LockInd, LOW);

   pinMode(buttonPin, INPUT_PULLUP);
   pinMode(Stopper1, INPUT_PULLUP);
   pinMode(ledPin, OUTPUT);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 100;
   if (millis() - timer >= interval)
   {
      timer = millis();
      
      buttonState = digitalRead(buttonPin);

      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            buttonGotPushed = 1;  // set the flag. we take action on it elsewhere 
         }
          lastButtonState = buttonState;
      }
  }

   if (buttonPushGotPushed)
    {
      player1.play(1);
      buttonGotPushed = 0;   // reset the flag
    }

    if(!digitalRead(Stopper1))
    {
      player1.stop();
    }
}

I can't test it for you.

alto_777_exits. Good luck my friend.

Yeah Yeah Please Enjoy , Have a nice time !!!! :blush:

I applied and got this

"buttonGotPushed"

Now time to learn this . . Thanks !!!

Thanks !
I did this for just 2 inputs in this way

#include <DFPlayerMini_Fast.h>
DFPlayerMini_Fast player1;
int count;

const byte  buttonPin1 = 22;    // the pin that the pushbutton is attached to
const byte  buttonPin2 = 23;
const byte ledPin = 13;       // the pin that the LED is attached to

int buttonGotPushed1;  // flag for noticing a button push
int buttonGotPushed2;


boolean buttonState1 = 0;         // current state of the button
boolean buttonState2 = 0;

boolean lastButtonState1 = 0;     // previous state of the button
boolean lastButtonState2 = 0;

const byte LockInd = 47;   // Button Lock
const byte Stopper1 = 48;
void setup()
{
  Serial3.begin(9600);
  if(player1.begin(Serial3, false))
  {
  player1.volume(22);
  }
    pinMode (LockInd, OUTPUT);
    digitalWrite(LockInd, LOW);

   pinMode(buttonPin1, INPUT_PULLUP);
   pinMode(buttonPin2, INPUT_PULLUP);
   
   pinMode(Stopper1, INPUT_PULLUP);
   pinMode(ledPin, OUTPUT);
}

void loop()
{
   StateDetectionUnit1();
   StateDetectionUnit2();

   
   if (buttonGotPushed1)
    {
      player1.play(1);
      buttonGotPushed1 = 0;   // reset the flag
    }
    if (buttonGotPushed2)
    {
      player1.play(2);
      buttonGotPushed2 = 0;   // reset the flag
    }


    if(!digitalRead(Stopper1))
    {
      player1.stop();
    }
}

void StateDetectionUnit1()
{
  static unsigned long timer1 = 0;
   unsigned long interval1 = 100;
   if (millis() - timer1 >= interval1)
   {
      timer1 = millis();
      
      buttonState1 = digitalRead(buttonPin1);

      if (buttonState1 != lastButtonState1)
      {
         if (buttonState1 == LOW)
         {
            buttonGotPushed1 = 1;  // set the flag. we take action on it elsewhere 
         }
          lastButtonState1 = buttonState1;
      }
  }
}

void StateDetectionUnit2()
{
  static unsigned long timer2 = 0;
   unsigned long interval2 = 100;
   if (millis() - timer2 >= interval2)
   {
      timer2 = millis();
      
      buttonState2 = digitalRead(buttonPin2);

      if (buttonState2 != lastButtonState2)
      {
         if (buttonState2 == LOW)
         {
            buttonGotPushed2 = 1;  // set the flag. we take action on it elsewhere 
         }
          lastButtonState2 = buttonState2;
      }
  }
}

Now suppose if I want to move for some 16 to 20 inputs
then for all that I ll have to make this whole set in this way only ??
:astonished: :open_mouth:

Any compression pattern ??

Hey, nice work! I assume that it functions.

You are now approaching a real giant leap forward in your progress. I encourage you to learn what I will reveal (!) using tutorials or videos or whatever learning source works best for you not through an endless process of…

trying, failing, and asking here why in order to get ahead tiny steps at a time, wearing out the patience of those who want to help. "Fake it until you make it" is a miserable strategy for improving your coding skills.

That said, and appy polly loggies if I've been rude…

Any time you find yourself numbering variables to facilitate coding by copying, pasting and editing entire blocks of code, it is a clue that you need to know about and exploit

array variables

They go great with functions and for loops.

HTH

a7

Thanks Sir :blush: !!

O o No issues sir . . . That is actually a way of motivating enough to get a better kick towards the learning and growing

Ya i was on that part only from some old Threads and designed it for 5 PINS

#include <DFPlayerMini_Fast.h>
DFPlayerMini_Fast player1;

int count;

int Buttons [] = {22,23,24,25,26};

const byte ledPin = 13;       // the pin that the LED is attached to

int buttonGotPushed; 
boolean buttonState = 0;
boolean lastButtonState = 0;

const byte LockInd = 47;   // Button Lock
const byte Stopper1 = 48;
void setup()
{
  Serial3.begin(9600);
  if(player1.begin(Serial3, false))
  {
  player1.volume(22);
  }

  for (int i = 0; i <= 5; i++) 
  {
  pinMode(Buttons[i], INPUT_PULLUP);
  }
  
  pinMode (LockInd, OUTPUT);
  digitalWrite(LockInd, LOW);

  pinMode(Stopper1, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
      StateDetectionUnitBOX(0);
      {
      if (buttonGotPushed)
      {
        player1.play(1);
        buttonGotPushed = 0;
      }
      }

//      StateDetectionUnitBOX(1);
//      {
//      if (buttonGotPushed)
//      {
//        player1.play(2);
//        buttonGotPushed = 0;
//      }
//      }
//
//      StateDetectionUnitBOX(2);
//      {
//      if (buttonGotPushed)
//      {
//        player1.play(3);
//        buttonGotPushed = 0;
//      }
//      }
//
//      StateDetectionUnitBOX(3);
//      {
//      if (buttonGotPushed)
//      {
//        player1.play(4);
//        buttonGotPushed = 0;
//      }
//      }
//
//      StateDetectionUnitBOX(4);
//      {
//      if (buttonGotPushed)
//      {
//        player1.play(5);
//        buttonGotPushed = 0;
//      }
//      }

    
    
    if(!digitalRead(Stopper1))
    {
      player1.stop();
    }
}

void StateDetectionUnitBOX(byte pin)
{
  static unsigned long timer = 0;
   unsigned long interval = 5;
   if (millis() - timer >= interval)
   {
      timer = millis();
      
      buttonState = digitalRead(Buttons[pin]);
//      while(!digitalRead(Buttons[pin]));
//        while(buttonState);
      
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            buttonGotPushed = 1;  // set the flag. we take action on it elsewhere
         }
          lastButtonState = buttonState;
      }
    }
//    while(buttonState);
//    while(!digitalRead(Buttons[pin]));
 }

Some Errors are here

Within void loop , after defining other PINS

1)Each and every PINS gets oscillated on press and holding the key

  1. After pressing a key and with audio file running , if we press another key
    then its not come into effect immediately
    at least 2 3 times needed to get activated

  2. With single defifnition of PIn it works great !!!

Please check

Thanks a ton !!!!!!!!!!!

My beach buddy is on her way, she who must not be kept waiting.

You need array variables instead of scalar variables for at at least the ones I list above.

Think about it - each button needs its own state, its own flag, its own timer and so forth.

At your level: use multiple arrays. All as big as the number of buttons.

int buttonGotPushed[NBUTTONS]; 
boolean buttonState[NBUTTONS];
boolean lastButtonState[NBUTTONS];

At your next level: use struct to make structured variables that hold all the disparate variables needed by each button. Arrays of those for multiple buttons

Somewhere in your future: C++ classes, that can wrap up code and variables in objects you can have an array of which.

But step by step. I still use parallel arrays when I am too lazy to go full-blown modern style.

a7

Hehe :sweat_smile: :grin: :joy:

Here it was raining so power out internet out . . .

.
.
.
.Well i am working with Array but due to lack of knowledge I just reached here with very latest script

#include <DFPlayerMini_Fast.h>
DFPlayerMini_Fast player1;

int count;

int buttonGotPushed[5]; 
boolean buttonState[5];
boolean lastButtonState[5];

int Buttons [] = {22,23,24,25,26};

const byte ledPin = 13;       // the pin that the LED is attached to

//int buttonGotPushed; 
//boolean buttonState = 0;
//boolean lastButtonState = 0;

const byte LockInd = 47;   // Button Lock
const byte Stopper1 = 48;
void setup()
{
  Serial3.begin(9600);
  if(player1.begin(Serial3, false))
  {
  player1.volume(22);
  }

  for (int i = 0; i <= 5; i++) 
  {
  pinMode(Buttons[i], INPUT_PULLUP);
  }
  
  pinMode (LockInd, OUTPUT);
  digitalWrite(LockInd, LOW);

  pinMode(Stopper1, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
      
      StateDetectionUnitBOX0(0);
      {
      if (buttonGotPushed[0])
      {
        player1.play(1);
        buttonGotPushed[0] = 0;
      }
      }

      StateDetectionUnitBOX1(1);
      {
      if (buttonGotPushed[1])
      {
        player1.play(2);
        buttonGotPushed[1] = 0;
      }
      }

      StateDetectionUnitBOX2(2);
      {
      if (buttonGotPushed[2])
      {
        player1.play(3);
        buttonGotPushed[2] = 0;
      }
      }

      StateDetectionUnitBOX3(3);
      {
      if (buttonGotPushed[3])
      {
        player1.play(4);
        buttonGotPushed[3] = 0;
      }
      }

      StateDetectionUnitBOX4(4);
      {
      if (buttonGotPushed[4])
      {
        player1.play(5);
        buttonGotPushed[4] = 0;
      }
      }

    
    
    if(!digitalRead(Stopper1))
    {
      player1.stop();
    }
}

void StateDetectionUnitBOX0(byte pin)
{
  static unsigned long timer = 0;
   unsigned long interval = 5;
   if (millis() - timer >= interval)
   {
      timer = millis();
      
      buttonState[0] = digitalRead(Buttons[pin]);

      if (buttonState[0] != lastButtonState[0])
      {
         if (buttonState[0] == LOW)
         {
            buttonGotPushed[0] = 1;  // set the flag. we take action on it elsewhere
         }
          lastButtonState[0] = buttonState[0];
      }
    }
 }

void StateDetectionUnitBOX1(byte pin)
{
  static unsigned long timer = 0;
   unsigned long interval = 5;
   if (millis() - timer >= interval)
   {
      timer = millis();
      
      buttonState[1] = digitalRead(Buttons[pin]);

      if (buttonState[1] != lastButtonState[1])
      {
         if (buttonState[1] == LOW)
         {
            buttonGotPushed[1] = 1;  // set the flag. we take action on it elsewhere
         }
          lastButtonState[1] = buttonState[1];
      }
    }
 }

 void StateDetectionUnitBOX2(byte pin)
{
  static unsigned long timer = 0;
   unsigned long interval = 5;
   if (millis() - timer >= interval)
   {
      timer = millis();
      
      buttonState[2] = digitalRead(Buttons[pin]);

      if (buttonState[2] != lastButtonState[2])
      {
         if (buttonState[2] == LOW)
         {
            buttonGotPushed[2] = 1;  // set the flag. we take action on it elsewhere
         }
          lastButtonState[2] = buttonState[2];
      }
    }
 }

 void StateDetectionUnitBOX3(byte pin)
{
  static unsigned long timer = 0;
   unsigned long interval = 5;
   if (millis() - timer >= interval)
   {
      timer = millis();
      
      buttonState[3] = digitalRead(Buttons[pin]);

      if (buttonState[3] != lastButtonState[3])
      {
         if (buttonState[3] == LOW)
         {
            buttonGotPushed[3] = 1;  // set the flag. we take action on it elsewhere
         }
          lastButtonState[3] = buttonState[3];
      }
    }
 }

 void StateDetectionUnitBOX4(byte pin)
{
  static unsigned long timer = 0;
   unsigned long interval = 5;
   if (millis() - timer >= interval)
   {
      timer = millis();
      
      buttonState[4] = digitalRead(Buttons[pin]);

      if (buttonState[4] != lastButtonState[4])
      {
         if (buttonState[4] == LOW)
         {
            buttonGotPushed[4] = 1;  // set the flag. we take action on it elsewhere
         }
          lastButtonState[4] = buttonState[4];
      }
    }
 }

Please tell what next ??

Ya actually I was proceeding for 6th one then suddenly planned to go with 5 only so deleted the 6th pattern

But forgot to make it 4

I didn't get this part

I am working with code , I am on it . .
Please elaborate a bit !!