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 :-
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
It should trigger the file again only, when I lift the finger and touch it for second time
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.
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.
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 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.