Combining a DFPlayer module with a self-made capacitive touch sensor

Hi,

at the start I'd like to point out that I am a newbie.

I am working on a project, where a self-made capacitive touch sensor (simply a piece of metal connected to the board) turns on the DFPlayer module that play 13 short audio clips in a random order.

I have figured out how to make the sensor (it works well with a led).

DFPlayer works well with the randomAll() command.

The problem came out while trying to combine these two things.

What's supposed to happen:

the touch sensor is being activated (val >= 100)

the audio clips start playing in a random order

the touch sensor is being deactivated (I take my finger away)

the audio stops playing

What is happening:

the audio clips start playing upon connecting the board to power (is that because of the playFirst() function?)

the touch sensor seems to be triggering the randomAll() function, but upon deactivating it, the audio keeps playing.

Here is the code:

#include <CapacitiveSensor.h>
CapacitiveSensor Sensor = CapacitiveSensor(4, 6);
long val;
int pos;

#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]

# define ACTIVATED LOW


int buttonRandom = 6;
boolean isPlaying = false;



void setup () {


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

mySerial.begin (9600);
delay(1000);
playFirst();
isPlaying = true;


}



void loop () { 

 val = Sensor.capacitiveSensor(30);
Serial.println(val);

if (val >= 100 && pos == 0)
{
  digitalWrite(buttonRandom, HIGH);
  pos = 1;
  delay(500);
}

else if (val < 100 && pos == 1)
{
  digitalWrite(buttonRandom, LOW);
  pos = 0;
  delay(500);
}

delay(10);
 

}

void randomAll()
{
  execute_CMD(0x18, 0, 0);
  delay(500);
}

void playFirst()
{
  execute_CMD(0x3F, 0, 0);
  delay(500);
  setVolume(20);
  delay(500);
  execute_CMD(0x11,0,1); 
  delay(500);
}



void setVolume(int volume)
{
  execute_CMD(0x01, 0, volume); // Set the volume (0x00~0x30)
  delay(2000);
}

void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}

Any suggestions?

I am new to all of this, so please don't hesitate to point out every obvious mistake. I want to learn.

Cheers!

I would start by using a DFMini library. It will have commands to start and stop playing a track. Your code does not seem to have the "stop playing" command implemented.

There are several DFMini libraries available.

You're right, I did not write anything that would actually stop the audio.

But why does it start playing upon powering up the board?

Also, nothing seems to be showing up in the Serial Monitor.
I'd like it to show the values from sensor. It works with the sensor alone.

syrob:
You're right, I did not write anything that would actually stop the audio.

But why does it start playing upon powering up the board?

What do you think the playFirst() function does? Is it possible that is plays the first track it finds?

It does. I fixed this issue but I can't work out the other problems.

The code looks like this now:

#include <CapacitiveSensor.h>
CapacitiveSensor Sensor = CapacitiveSensor(4, 6);

#include "DFRobotDFPlayerMini.h"
#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]

# define ACTIVATED LOW


long val;
int pos;
int buttonRandom = 6;
boolean isPlaying = false;




void setup () {


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

mySerial.begin (9600);
delay(1000);


}



void loop () { 

 val = Sensor.capacitiveSensor(30);
Serial.println(val);

if (digitalRead(buttonRandom) == ACTIVATED)
{
  if (val >= 100 && pos == 0)
{
    isPlaying = true;
    playFirst();
    pos == 1;
}

else if (val < 100 && pos == 1)
{
  isPlaying = false;
  pause();
  pos == 0;
}

}
delay(10);
 

}





void play()
{
  execute_CMD(0x0D,0,1); 
  delay(500);
}

void randomAll()
{
  execute_CMD(0x18, 0, 0);
  delay(500);
}

void playFirst()
{
  execute_CMD(0x3F, 0, 0);
  delay(500);
  setVolume(20);
  delay(500);
  execute_CMD(0x11,0,1); 
  delay(500);
}

void pause()
{
  execute_CMD(0x0E,0,0);
  delay(500);
}

void setVolume(int volume)
{
  execute_CMD(0x01, 0, volume); // Set the volume (0x00~0x30)
  delay(2000);
}

void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}

I can't make it work as it's supposed to.

It should play only when I am touching the sensor.
When I take my finger away, it should stop and reset.

Any suggestions?

First off, please format your code (Ctrl-T in the IDE) so it all lines up.

Second, you don't have a Serial.begin(9600) in your setup() function, but you call Serial.println()

Third, you read the capacitive sensor and don't react to the value. You react to a button press which you have defined as INPUT. Do you have a pullup resistor connected to that button?