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!