DF PLAYER with OneWireKeypad trigger issue

Connecting UNO with a DF Player applying OneWireKeypad Library

&

With this finely working code below :

#include "OnewireKeypad.h" // OneWireKeypad Library

char KEYS[] =   // Define keys' values of Keypad
{
	'1', '2', '3', 'A',
	'4', '5', '6', 'B',
	'7', '8', '9', 'C',
	'*', '0', '#', 'D'
};

/* Define Library :
OnewireKeypad <Print, #of buttons>
Keypad(Serial, Char values, #Rows, #Cols, Arduino Pin, Row_resistor, Columns_resistor) */
OnewireKeypad <Print, 16 > Keypad(Serial, KEYS, 4, 4, A0, 4700, 1000);
int ledpin = 2;

#include <DFPlayerMini_Fast.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial1(3, 1); // RX, TX
DFPlayerMini_Fast player1;

void setup ()
{
	Serial.begin(9600);
  pinMode(ledpin, OUTPUT);
  mySerial1.begin(9600);
   if(player1.begin(mySerial1, false))
  {
  player1.volume(12);
  player1.play(11);
  }
}

void loop()
{
	Keypad.SetHoldTime(50);  // Key held time in ms
	Keypad.SetDebounceTime(25); // Key Debounce time in ms
	if ((Keypad.Key_State() == 3))    // not pressed = 0, pressed = 1, released = 2,  held = 3
	{
		char keypress = Keypad.Getkey();  // put value of key pressed in variable 'keypress'
		Serial.print("Keypad Key: ");
		Serial.println(keypress);  // Display value on Serial Monitor
		// while ((Keypad.Key_State())) {} // Stay here while Key is held down

    if(keypress=='1')
    {
      player1.loop(14);
    }

    if(keypress=='2')
    {
      player1.loop(15);
    }

    if(keypress=='A')
    {
      player1.stop();
    }
  }
}

I am facing 2 small problems

  1. There is a very very small delay between the trigger and sound play when a key on keypad is pressed.
    It is very small but can effect many of the project programs so want to remove it completely

  2. While keep pressing the key or say doing press and hold function, the system continue triggers the assigned file from beginning depending on

Keypad.SetHoldTime(50);

Both the above points are dependent on SetHoldTime

If I am decreasing the value then the delay is getting shorten and shorten thus triggering the exact time of key press

But at the same moment it triggers file 2 3 times in the meantime
or
If there is any delay in releasing the key then it starts triggering the file continuously from beginning

I just want to remove this repeatedly triggering from the system
It is like :
If I press a key and hold it
It ll trigger the file once only
&
To trigger it again I ll have to release the key and press it back again. . .

Just exactly like

if(!digitalRead(Pin1))
  {
    player1.loop(10);
    while(!digitalRead(Pin1));
  }

Please Guide !!!!!

When Keypad.Key_State() is 3 (during press-and-hold) your program says to keep playing the file. You need a flag (boolean) inside state "3" to say "We are playing a file, do not play another file" and when the file is done playing, reset the flag to allow state "3" to play another file.

Oh its like that

A bit complex it is

Sir this kind of script is very new to me

Would you please help me with a script to make things clear in practical way ??

Its a complete new format for me in advanced level actually !!!
I am still learning basics as it is vast !!!!!!

Start with configuring a flag in the beginning of the sketch...

boolean stone = 0; // flag to keep from replaying during "button held"
boolean soup = 0;

Inside "if keypad...state 3" (press-and-hold), check keypress...

    if(keypress=='1') 
    {

Then check for the flag...

      if (stone == 0) { // false = not yet played
        player1.loop(14); // play the file 
        stone = 1; // set the flag to true = played / do not play again
      } // close stone
    } // close keypress "1"

    if(keypress=='2')
    {
      if (soup == 0) {
        player1.loop(15);
        soup = 1;
      }
    }
  stone = 0; // reset flag when outside the key press scope
  soup = 0; // reset flag
}

Ya Sir its working but in a different manner
Its one more type of design I came to know . . Thanks

But my target is like
pressing and playing immediately and if I used to press and hold the key, it ll not trigger again

For that purpose I need to release the key and again need to press it to trigger or to play

Its like, for a short amount of sound say, a click or a tick sound
If I press the key it ll produce a click or tick

We can take example of a keypad lock system or a mobile keypad , in which when we press a key it produces a 'BEEP' sound on any key press
where if we use to keep it press it do not create sound or

You can't use pin 1 for software serial on Uno. You are already using it for Serial.

O Actually i shifted the board from UNO to ESP8266 later !!!!

Please help me !!

Please carefully re-read your posts so far in this topic. Are there any other changes that you have not told the forum about?

If using ESP32, you should not be using SoftwareSerial. ESP32 has other available hardware serial ports which should always be used in preference to SoftwareSerial.

No for now it is ESP8266

And actually Sir the matter here is the triggering part which we need to make it work as per our requirement

so what should I apply for the purpose ??

Ya Sir please tell !!!

D7 & D8 should be the other hardware serial pin on your board.

Of course this is just a guess as I don’t know exactly which ESP8266 you have.

I See
Is there anything to do with pin selection :thinking:

Its your library so you can make it very well I think :blush:

I put this

SoftwareSerial mySerial1(13, 15);

But here nothing is going on
No play no read . . .

Don’t use software serial, just use Serial1 (maybe Serial2 for that board).

You also need to move them down to D7&D8 as Rx and Tx are for the serial monitor.

OK Le me check with that

I see . . . OK I am on it . . .

Compilation error: Serial1.h: No such file or directory

It is displaying in error msg

Turns out that board is weird. After Serial.begin() call Serial.swap();