ESP32 Built-In TouchPin with DF player Operations

As stated in the heading, firstly here is the code :

const byte touchPin1 = 32;
const byte touchPin2 = 33;
const byte touchPin3 = 27;
const byte touchPin4 = 14;
const byte touchPin5 = 12;
const byte touchPin6 = 13;

const int lowerThreshold = 30;  //adjust these to suit your system
const int upperThreshold = 40;
boolean currentlyTouched = false;

#include <DFPlayerMini_Fast.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial1(3, 1); // RX, TX
DFPlayerMini_Fast player1;

void setup()
{
    Serial.begin(9600);
    mySerial1.begin(9600);
      if(player1.begin(mySerial1, false))
      {
        player1.volume(22);
        player1.play(200);
      }
}

void loop()
{
    int touchValue1 = touchRead(touchPin1);
    if (touchValue1 < lowerThreshold && currentlyTouched == false)
    {
        Serial.println("pin1 became touched");
        player1.loop(201);
        currentlyTouched = true;
        // while ('(touchRead(touchValue1)') {}
    }
    else if (touchValue1 > upperThreshold && currentlyTouched == true)
    {
        Serial.println("pin1 became not touched");
        currentlyTouched = false;
    }



    int touchValue2 = touchRead(touchPin2);
    if (touchValue2 < lowerThreshold && currentlyTouched == false)
    {
        Serial.println("pin2 became touched");
        player1.stop();
        currentlyTouched = true;
    }
    else if (touchValue2 > upperThreshold && currentlyTouched == true)
    {
        Serial.println("pin2 became not touched");
        currentlyTouched = false;
    }

}


Now coming to operations :-

I operating Play and Stop function of a DF Player with Built IN Touch

It is really working for Play and Stop process

Now when i use to touch the pin or plate of wire connected, it starts triggering the allotted wave file continuously or repeatedly, until and unless I release the terminal.

It should be like :-

  1. When I touch it soft or hard, it should trigger the file once only so that it can play smoothly to its length as usually

  2. It should trigger the file again only, when I lift the finger and touch it for second time

I am on ESP 32 WROOM module 38 pin.

Please Guide !!

Once a play has been initiated then you must check to see if the touch-plate has been released (is not being touched).

I think this is done by the lines here

else if (touchValue2 > upperThreshold && currentlyTouched == true)
    {
        Serial.println("pin2 became not touched");
        currentlyTouched = false;
    }

So at the time when the terminal is touched and hold, thay function should be initiàted which can read the touch once and then having nothing to do with the hold operarion or say no need to get triggered repeatedly under hold condition

Can we put a switch and case function woth break ??

If it keeps re-starting, as you have described the situation, it's doing so because it's getting a cue to do so.
("Oh, look, the plate is touched? - here's a play command, dfplayer! Have a good time with that.)"

Look at the switch, when it becomes active start the track,
ignore the switch briefly,
then look to see if it has gone back to inactive.

A bit I am getting the point here but how it ll go to inactive state when the plate is still under touch condition.

I am trying to apply the switch case in this way

const byte touchPin1 = 32;
const byte touchPin2 = 33;
const byte touchPin3 = 27;
const byte touchPin4 = 14;
const byte touchPin5 = 12;
const byte touchPin6 = 13;

const int ledPin = 2;

const int lowerThreshold = 30;  //adjust these to suit your system
const int upperThreshold = 40;
const int threshold = 20;
boolean currentlyTouched = false;

#include <DFPlayerMini_Fast.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial1(3, 1); // RX, TX
DFPlayerMini_Fast player1;

void setup()
{
    Serial.begin(9600);
    mySerial1.begin(9600);
      if(player1.begin(mySerial1, false))
      {
        player1.volume(22);
        // player1.play(200);
      }

      pinMode (ledPin, OUTPUT);
}

void loop()
{
    int touchValue1 = touchRead(touchPin1);
    Serial.print(touchValue1);

    switch (touchValue1) {
    case 0:
    if (touchValue1 < lowerThreshold && currentlyTouched == false)
    {
        Serial.println("pin1 became touched");
        player1.loop(201);
        currentlyTouched = true;
        // while ('(touchRead(touchValue1)') {}
    }
    else if (touchValue1 > upperThreshold && currentlyTouched == true)
    {
        Serial.println("pin1 became not touched");
        currentlyTouched = false;
    }
    break;
    }


}


Is it possible this type of way ??

This sketch triggers the players once and no res-starting exists with the touch

But this event is happening once only then after getting triggered

After that, the system gets completely inactive or get freeze.

Any solution to this design or it can't be implanted any of the way ???

What values does touchRead() return? I also don’t think you need the switch statement.
You may also want to keep track of the last read value too and compare it to the new value.

screenshot_20240917_181906

Don't know why it always shows like that . .. baud rate is all ok

Please help me in solving the issue as I am not getting anything to make things clear

Sorry was in a meeting, where does touchRead come from? Is it a function you wrote or does it come from a library?

I ask because you are using the value it returns in a switch statement, and the only case you have is “case 0”. And in that case you are comparing it to a threshold value but because the case is 0 there is no need to compare it to the threshold.

Remove the switch statement and just have the if/else statements that compare the threshold.

Ya basically it came from analog input restarting solution
I applied the same in that way so blah blah things included
Well, soon going to filter out those . .

Few minutes back I tried with this code below

const byte touchPin1 = 32;
const byte touchPin2 = 33;
const byte touchPin3 = 27;
const byte touchPin4 = 14;
const byte touchPin5 = 12;
const byte touchPin6 = 13;

const int ledPin = 2;

const int lowerThreshold = 30;  //adjust these to suit your system
const int upperThreshold = 40;
const int threshold = 20;
boolean currentlyTouched = false;

#include <DFPlayerMini_Fast.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial1(3, 1); // RX, TX
DFPlayerMini_Fast player1;

enum PLAYS
{
  Play1,
  Play2,
  DelayPlay1,
};
PLAYS currentState1 = Play1;

enum STOPS
{
  Stop1,
  Stop2,
  DelayPlay2,
};
STOPS currentState2 = Stop1;

void setup()
{
    Serial.begin(9600);
    mySerial1.begin(9600);
      if(player1.begin(mySerial1, false))
      {
        player1.volume(22);
        // player1.play(200);
      }

      pinMode (ledPin, OUTPUT);
}

void loop()
{
  switch (currentState1)//execute the code for the current state

      {

        case Play1:

        int touchValue1 = touchRead(touchPin1);
        Serial.print(touchValue1);
        if (touchValue1 < lowerThreshold && currentlyTouched == false)
        {
        Serial.println("pin1 became touched");
        player1.loop(201);
        currentlyTouched = true;
        // while ('(touchRead(touchValue1)') {}
        }
        else if (touchValue1 > upperThreshold && currentlyTouched == true)
        {
        Serial.println("pin1 became not touched");
        currentlyTouched = false;
        }
        break;



        int touchValue2 = touchRead(touchPin2);
        Serial.print(touchValue2);
        if (touchValue2 < lowerThreshold && currentlyTouched == false)
        {
        Serial.println("pin2 became touched");
        player1.stop();
        currentlyTouched = true;
        // while ('(touchRead(touchValue1)') {}
        }
        else if (touchValue2 > upperThreshold && currentlyTouched == true)
        {
        Serial.println("pin2 became not touched");
        currentlyTouched = false;
        }

        break;

      }

}


For single track or single play function it works like I want to have

But the second part which includes stop function is not working
That command is not functioning

Please check

As you said, I am going to try it with If/ELSE . . Let's see !!!

You have a “break;” command so it will never get down to the stop code. Remove the first break;

I tried

Removing that break starts the issue again of that restarting or re triggering

There the glitch exists !!!

You need 2 separate “currentlyTouched” variables. One resets the other. There should also be a flag in the DFPlayer library that checks if a track is currently being played. I don’t remember offhand the actual name, but it’s set to true when a track is being played, so all you need to do is check that flag and it should only trigger the next loop when a track is NOT currently being played.

I see

So no idea about flag
Would you please get me a bit idea related to this one you explained in a way broader

The flag is called “isPlaying()” and is set to true when a track is playing.

O O I got it

Then we need the BUSY pin to include in the project, I think !!!

Once I operated external indicator LED to indicate if the player is playing or not currently , 2 years back !!

Well what's the reason that the serial monitor is printing this way

screenshot_20240917_211257

It does that some times. Just press the EN button on the Esp32 board and it should start printing. Also double check your baud rate is correct.

//  For ESP32
//
//


const byte tap = 4;
int threshold = 50;
int touchVal;

unsigned long markTime;
unsigned long touchTime;
bool tapped = false;

unsigned long counter = 0;

void setup() 
{
  Serial.begin(19200);
  while (!Serial);
  Serial.print("\r\n\r\n");
  Serial.println("Dégagez le pont.");
  delay(2000);
  Serial.println("Allons-y...");
  touchVal = touchRead(tap);
  Serial.print(touchVal);
  Serial.print("\r\n\r\n");
}

void loop() 
{
  touchVal = touchRead(tap);
  if(touchVal < threshold && tapped == false)
  {
    markTime = millis();
    tapped = true;
    Serial.print("play_");
    Serial.println(counter);
    counter ++;
  }

  if (touchVal >= threshold && tapped == true)
  {
    touchTime = millis() - markTime;
    Serial.println(touchTime);
    tapped = false;
  }
}

No Change . .

On pressing EN

it prints

screenshot_20240917_211943

And further same !!!