Playing mp3 with 2 fingers!

Hey everyone,

My project is having 2 wired gloves : thumbs are connected to ground and other fingers connected to pins 2, 3, 4, 5, A2, A3, A4, A5.
When a finger touchs thumb, it plays an mp3.

I use arduino uno and DFPlayer Mini.

My code is working when the touch between fingers is short.
When 2 fingers stay in contact a few seconds the song is played another time when fingers separate.
As the state of the pin changes from HIGH to LOW randomly.

Thanks in advance for your help :slight_smile: :wink:

Here is my code :

SoftwareSerial mySerial(10, 11);
DFRobotDFPlayerMini myDFPlayer;
/*
SD CARD : Folder mp3
01 / 8 samples for 8 fingers (thumbs = ground)
*/

define ACTIVATED LOW

const int button[8] = {2, 3, 4, 5, A2, A3, A4, A5};

bool state[8] = {0, 0, 0, 0, 0, 0, 0, 0};
bool lastState[8] = {0, 0, 0, 0, 0, 0, 0, 0};

void setup ()
{
for ( int i = 0 ; i < 8 ; ++i)
{
pinMode(button[i], INPUT_PULLUP);
digitalWrite(button[i], HIGH);
}

mySerial.begin (9600);

Serial.begin(9600);
Serial.write("Initializing DFPlayer\n");

if (!myDFPlayer.begin(mySerial))
{ //Use softwareSerial to communicate with mp3.
Serial.write("Unable to begin\n");
while (true);
}
Serial.write("DFPlayer Ready\n");
delay(1000);
myDFPlayer.volume(25); //Set volume value (0~30).

//----Set different EQ----
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
}

/***********************************
FONCTIONS
***********************************/
void playSong(int folder, int track)
{
delay(100);
myDFPlayer.playFolder(folder, track);
delay(1000);
}

void loop ()
{
for (int i = 0 ; i < 8 ; ++i)
{
state[i] = digitalRead(button[i]);
}

for (int i = 0 ; i < 8 ; ++i)
{
if (state[i] != lastState[i])
{
if (state[i] == ACTIVATED)
{
playSong(1, i);
}
lastState[i] = state[i];
}
}
}

I think you simply forgot to debounce the switch inputs?

Hi MarkT,
Thank you so much !
You have the solution.
Now with the delay() function at the right place it works !
Thanks again

my code :

//INFINITE LOOP
void loop ()
{
for (int i = 0 ; i < 8 ; ++i)
{
state[i] = digitalRead(button[i]);
}

for (int i = 0 ; i < 8 ; ++i)
{
if (state[i] != lastState[i])
{
delay(100); //debounce

  if (state[i] == ACTIVATED)
  {
    myDFPlayer.playFolder(1, i);          
  }      
  lastState[i] = state[i];            
}

}
}