Go Down

Topic: Capacitive Touch to MP3 Shield geeetech VS1053 (Read 1 time) previous topic - next topic

sutsche

Hi all,

i'm struggling a bit with a very simple code, i guess.
I know there is something missing but don't really know how to better declare or identify /activate the MP3

I tried a lot of different things, thats why the code is a bit messy now but can't seem to get the Mp3 via capacitive touch to work. I did this a while ago with different MP3 shields and WaveShield which worked fine but somehow I lost my code and the geeetech doesn't seem to used a lot (certainly for reasons..)

Anyhow, if someone could enlighten me about my silly mistakes in the code I'd really appreciate it.






#include <CapacitiveSensor.h>       // libraries
#include <SPI.h>      // libraries
#include <SdFat.h>
#include <SFEMP3Shield.h>


SdFat sd;       //SD Karte benennen
SFEMP3Shield MP3player;   //MP3 Shield als „MP3Player benennen

CapacitiveSensor   CS = CapacitiveSensor(2, 3); // 10M resistor

int t = 6000;     //Der Wert für „t" gibt im Code den zeitlichen Abstand zwischen dem Abspielen der Datei an.
int inputPin = 3;
int outputPin = 2;
//int capState = LOW;
//int val = 0;

void setup()
{

  //    CS.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  // pinMode (

  sd.begin(SD_SEL, SPI_HALF_SPEED);
  //) sd.initErrorHalt(); //SD Karte mit MP3 Dateien auslesen
  // if (!sd.chdir("/")) sd.errorHalt("sd.chdir");

  MP3player.begin();    //MP3 Shield starten
  MP3player.setVolume(10, 10); //Die Lautstärke einstellen

  pinMode(inputPin, INPUT);
  pinMode(outputPin, OUTPUT);

  Serial.begin(9600);


}


void loop()
{
  long start = millis();
  long sensor =  CS.capacitiveSensor(5);

  Serial.print(millis() - start);        // check on performance in milliseconds
  Serial.print("\t");                    // tab character for debug windown spacing

  Serial.print(sensor);                  // print sensor output 1
  Serial.println("\t");

  // arbitrary delay to limit data to serial port

  // if(!sd.begin(9, SPI_HALF_SPEED)) sd.initErrorHalt();  //SD Karte mit MP3 Dateien auslesen
  //  if (!sd.chdir("/")) sd.errorHalt("sd.chdir");

  if (sensor > 100)
  { MP3player.available();
    MP3player.playTrack(1);
  }
  else
  { MP3player.end();
  }
  delay(100);

  /*val =digitalRead (inputPin);
    if (val == HIGH) {
    digitalWrite(outputPin, HIGH);

    MP3player.playTrack(1);

    if(capState == LOW)
    {
      Serial.println ("Motion detected!");
      capState = HIGH;

      }
    }
    else{
    digitalWrite(outputPin, LOW);
    if (capState == HIGH) {
      Serial.println ("Motion ended!");
      capState = LOW;
      }
    }
  */

}
 


Grumpy_Mike

#2
Aug 29, 2017, 04:23 pm Last Edit: Aug 29, 2017, 04:25 pm by Grumpy_Mike
Well no.
You have not bothered to read the how to use this forum sticky post so you posted the code incorrectly.
Also you provide almost no real information.
A link to your MP3 module would be good.
What PIR sensor?
How is it wired?

What code have you tried and can you post it. The code posted has no attempt to read the PIR sensor.

Have you got just the MP3 player to work by itself?

sutsche

#3
Aug 29, 2017, 11:59 pm Last Edit: Aug 30, 2017, 12:05 am by sutsche
Hi,  yes, you're right. I was a bit in a hurry and didn't really check the guidelines properly. Thought browsing already taught me the important parts. Sorry for that. So here again.


I am working on a very simple setup of Arduino Uno and an MP3 Shield ( geeetech VS1053-https://www.elecrow.com/vs1053-mp3-shield-p-777.html )-
Via capacitive touch I want to activate the MP3 shield and play a file as long as the values are high and otherwise stop. Probably two to three MP3 files played random but haven't gotten there yet since I struggle with bringing the code together. No PIR sensors.

I have a functional code for the MP3 player audio and another one for the capacitive sensing. I can't bring them together and am not sure whether I lack the understanding of how to combine them or whether some of the pins I'm using are busy.

Here is the MP3 code

Code: [Select]

#include <SPI.h>      // libraries
#include <SdFat.h>
#include <SFEMP3Shield.h>


SdFat sd;       //SD Karte benennen
SFEMP3Shield MP3player;   //MP3 Shield als „MP3Player benennen

int t=6000;       //Der Wert für „t" gibt im Code den zeitlichen Abstand zwischen dem Abspielen der Datei an.

void setup()                   
{
     
 Serial.begin(9600);

 if(!sd.begin(9, SPI_HALF_SPEED)) sd.initErrorHalt();  //SD Karte mit MP3 Dateien auslesen
 if (!sd.chdir("/")) sd.errorHalt("sd.chdir");

  MP3player.begin();    //MP3 Shield starten
  MP3player.setVolume(10,10); //Die Lautstärke einstellen
 
  }

void loop()                   
{
                               

MP3player.available();
 MP3player.playTrack(1);     //Das MP3 Shield spielt nun die MP3 Datei mit dem Namen „track001". Bei einer Datei mit dem Namen „track002" müsste eine „2" anstelle der „1" in diese Klammer. 
  delay(t);               //Vorher festgelegte Pause
 
}


Here is the Capacitive Sense Code

Code: [Select]

#include <CapacitiveSensor.h>       // libraries

CapacitiveSensor   CS = CapacitiveSensor(2,3);  // 10M resistor between pins 2 & 3, pin 3 is sensor pin


void setup()                   
{
   
//   cs_7_6.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   Serial.begin(9600);


}

void loop()                   
{
    long start = millis();
    long total1 =  CS.capacitiveSensor(8);

    Serial.print(millis() - start);        // check on performance in milliseconds
    Serial.print("\t");                    // tab character for debug windown spacing

    Serial.print(total1);                  // print sensor output 1
    Serial.println("\t");

    delay(100);                             // arbitrary delay to limit data to serial port

}




NOW: i tried to bring those together in different ways, most times the capacitive touch was working but not in communication with the MP3 shield. One of the examples is posted above. I believe there should be a very simple if command to bring them together but ...

one thing i tried:

Code: [Select]


#include <CapacitiveSensor.h>       // libraries
#include <SPI.h>      // libraries
#include <SdFat.h>
#include <SFEMP3Shield.h>


SdFat sd;       //SD Karte benennen
SFEMP3Shield MP3player;   //MP3 Shield als „MP3Player benennen

CapacitiveSensor   CS = CapacitiveSensor(2, 3); // 10M resistor

int t = 6000;     //Der Wert für „t" gibt im Code den zeitlichen Abstand zwischen dem Abspielen der Datei an.


void setup()
{

 
  sd.begin(SD_SEL, SPI_HALF_SPEED);


  MP3player.begin();    //MP3 Shield starten
  MP3player.setVolume(10, 10); //Die Lautstärke einstellen

  Serial.begin(9600);


}


void loop()
{
  long start = millis();
  long sensor =  CS.capacitiveSensor(5);

  Serial.print(millis() - start);        // check on performance in milliseconds
  Serial.print("\t");                    // tab character for debug windown spacing

  Serial.print(sensor);                  // print sensor output 1
  Serial.println("\t");

  // arbitrary delay to limit data to serial port


  if (sensor > 100)
  { MP3player.available();
    MP3player.playTrack(1);
  }
  else
  { MP3player.end();
  }
  delay(100);

 

}



gives me no error of the code itself but readings in the serial monitor like that

Code: [Select]

1247 -2
1249 -2
1247 -2
1247 -2
1248 -2
1247 -2
1248 -2
1247 -2


yesterday I had a code running that perfectly read out the values n the serial monitor and was responding to the command of playing a file (once sensor value is higher then...) and activated the shield, sadly without playing a file but creating some noise of activity. but it also stopped reading then. anyways i messed up the code later and now i am very confused while believing the solution must be laughable simple.

any suggestions appreciated and if I am ignoring existing rules please let me know, cause I'm not aware of that now. Cheers

Grumpy_Mike

#4
Aug 30, 2017, 12:40 pm Last Edit: Aug 30, 2017, 12:40 pm by Grumpy_Mike
Thanks that is a lot more clear.
I think what is happening with that last code is that once the touch sensor is triggered and the file is playing the next time round the loop you start the file playing again, hence it never "gets going".

What you need to do is that once the file is playing keep looking at the capsense output and to see if the file is still playing. Only when that test fails you stop the file playing.

sutsche

hi, thank you first of all.

do you think I can maybe just use a delay to get pauses between the readings?
or a play.once function maybe?

what I would love to know is whether I can get a serial read of the attempt of the Shield starting to play, because since i can't hear anything I am not sure whether it even tries. was wondering if it maybe doesn't really understand the commands i put in.

the cap sense reading is quite stable. i am using a long wire and depending where i touch i get very different ranges of values.. very few noise

Grumpy_Mike

#6
Aug 30, 2017, 03:45 pm Last Edit: Aug 30, 2017, 03:46 pm by Grumpy_Mike
No each time round the loop you start the file again.
Therefore once you have kicked the file off playing you have to ensure you don't do it again.
The simplest way would be to have a while loop inside your if statement that checks the cap sense to see if the finger is still being held down, when it is noliponger held down then stop the file and exit the loop.
Your else section of you if statement should then be removed.

Quote
what I would love to know is whether I can get a serial read of the attempt of the Shield starting to play,
Yes just put a serial print in after the start to play command.

sutsche

#7
Aug 30, 2017, 05:26 pm Last Edit: Aug 31, 2017, 12:33 am by sutsche
So, i got it playing now. Not perfect, but a start.
Some questions still arose. But now i've been looking at interrupt commands and looping options but couldn't find a good example that reads when the file is finished playing and restarts the loop. Did you have any specific commands in mind, Mike? Any examples i could look up?

Quote
loop inside your if statement that checks the cap sense to see if the finger is still being held down, when it is noliponger held down then stop the file and exit the loop.
As for the other questions: I tried and it worked to insert a pinMode for Pin2,3 but i shouldn't need them since i have them identified in the CapacitiveSersor in the beginning, right? Anyways. Its working somehow.


Code: [Select]
#include <CapacitiveSensor.h>       // libraries
#include <SPI.h>      // libraries
#include <SdFat.h>
#include <SFEMP3Shield.h>

CapacitiveSensor   cs_2_3 = CapacitiveSensor(2, 3); // 10M resistor between pins 7 & 6, pin 6 is sensor pin

SdFat sd;       //SD Karte benennen
SFEMP3Shield MP3player;   //MP3 Shield als „MP3Player benennen

int t = 6000;     //Der Wert für „t" gibt im Code den zeitlichen Abstand zwischen dem Abspielen der Datei an.
// int state;
int time;
void setup()
{


  Serial.begin(9600);

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);


  sd.begin(9, SPI_HALF_SPEED); //SD Karte mit MP3 Dateien auslesen

  if (!sd.begin(9, SPI_HALF_SPEED)) sd.initErrorHalt(); //SD Karte mit MP3 Dateien auslesen

}

void loop()
{
  long start = millis();
  long sensor =  cs_2_3.capacitiveSensor(5);

  Serial.print(millis() - start);        // check on performance in milliseconds
  Serial.print("\t");                    // tab character for debug windown spacing

  Serial.print(sensor);                  // print sensor output 1
  Serial.println("\t");

  if (sensor > 100) {
    MP3player.begin();    //MP3 Shield starten
    MP3player.setVolume(10, 10); //Die Lautstärke einstellen

    Serial.println("PLAY");
    MP3player.available();
    MP3player.playTrack(1);
    while (1);
    delay(100);

    time = MP3player.currentPosition();
    Serial.println(time);
  }


  if (sensor < 100) {
    Serial.println("SILENT");
  }

}

//   restart loop
/*
  void stateChange(){
  state = !state;
  x = x++;
  }*/



Looking at the following part...

Code: [Select]
    Serial.println("PLAY");
    MP3player.available();
    MP3player.playTrack(1);
    while (1);
    delay(100);

    time = MP3player.currentPosition();
    Serial.println(time);



 it did work without the "while" command as well, but gave me weird numerical values the moment it started playing the file.

Code: [Select]
1 92
SILENT
0 119
PLAY
0
1548 -2
SILENT
1550 -2
SILENT
1552 -2
SILENT
1550 -2
SILENT


I believe this is evidence for the Shield not being able to read values while its playing a file, right?
Which is a bit disappointing because initially i wanted to stop the MP3 file playing when the touch is released. If this is a matter of the code i've choosen and I could probably still figure out a way how to realise that it would be amazing.




Grumpy_Mike

#8
Aug 31, 2017, 12:35 am Last Edit: Aug 31, 2017, 12:36 am by Grumpy_Mike
The
Code: [Select]
while (1);
Will just freeze the computer and will stop it from doing anything else. You should not have it.

Quote
tried and it worked to insert a pinMode for Pin2,3 but i shouldn't need them since i have them identified in the CapacitiveSersor in the beginning, right? Anyways. Its working somehow.
That would depend if the library actually set the pin mode, it is up to the libraries authors  on if they want to do this.

Quote
I believe this is evidence for the Shield not being able to read values while its playing a file, right?
As the MP3 module is playing the files autonomously I don't see why this should be.

You have not changed the code like I told you to.

Forget the capsense  bit for the moment and just use a push button on a digital pin first until you get the sound module working as you want.

sutsche

#9
Aug 31, 2017, 06:07 pm Last Edit: Aug 31, 2017, 06:14 pm by sutsche
removed the while. found it looking for interrupts. thanks for the info

Quote
You have not changed the code like I told you to.

Forget the capsense  bit for the moment and just use a push button on a digital pin first until you get the sound module working as you want.
been working on that. seemed easier then it is right now.
Getting a lot of errors as well as infos on inaccessibility of the sd.card in the serial monitor sometimes if i try the following


Code: [Select]
// #include <CapacitiveSensor.h>       // libraries
#include <SPI.h>
#include <SdFat.h>
#include <SFEMP3Shield.h>

// CapacitiveSensor   cs_2_3 = CapacitiveSensor(2, 3); // 10M resistor between pins, pin 3 is sensor pin

SdFat sd;       //SD Karte benennen
SFEMP3Shield MP3player;   //MP3 Shield als „MP3Player benennen


const int buttonPin = 2;
// const int ledPin = 13;


int t = 6000;     //Der Wert für „t" gibt im Code den zeitlichen Abstand zwischen dem Abspielen der Datei an.
int buttonState = 0;

void setup()
{


  Serial.begin(9600);

  // initialize the LED pin as an output:
//  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  //pinMode(2, OUTPUT);
  // pinMode(3, OUTPUT);

sd.begin(9, SPI_HALF_SPEED);
 // if (!sd.begin(9, SPI_HALF_SPEED)) sd.initErrorHalt(); //SD Karte mit MP3 Dateien auslesen
 // if (!sd.chdir("/")) sd.errorHalt("sd.chdir");

}

void loop()
{

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {

    digitalWrite(buttonPin, HIGH);
    MP3player.begin();    //MP3 Shield starten
    MP3player.setVolume(10, 10); //Die Lautstärke einstellen

    Serial.println("PLAY");
    MP3player.available();
    MP3player.playTrack(1);
    delay(1000);
  }


its a very frustrating process.
i dont really get what the problem with this shield is.

Running button code on the arduino seems unproblematic. Running test on the MP3 Shield already doesn't let me use pin13 // had this impression before that the PIN is blocked thought the sd.card read. Which is fine, have an Led on PIN3 now, for checking

So trying to implement the command into my code or the other way around but the moment i close the circuit / push the button the readings stop and not just do i have to upload a different code, then that code again but also have to restart the board itself (unwire even) if i want to try again. Very bizarre, never experienced that. It is as if the Arduino needs some cleaning before being stable again. Somehow the entire ability of reading the button does only work after full restart. Sometimes when pushing the button the sound starts, sometimes not, sometimes just starts playing for a sec.

Here where I am:

Code: [Select]

       // libraries
#include <SPI.h>     
#include <SdFat.h>
#include <SFEMP3Shield.h>


SdFat sd;       //SD Karte benennen
SFEMP3Shield MP3player;   //MP3 Shield als „MP3Player benennen

int t = 6000;     //Der Wert für „t" gibt im Code den zeitlichen Abstand zwischen dem Abspielen der Datei an.


void setup()
{

  Serial.begin(9600);

  pinMode(2, INPUT_PULLUP);
  pinMode(3, OUTPUT);


 if(!sd.begin(9, SPI_HALF_SPEED)) sd.initErrorHalt();  //SD Karte mit MP3 Dateien auslesen
   if (!sd.chdir("/")) sd.errorHalt("sd.chdir");
   
}

void loop()
{

 
 int sensorVal = digitalRead(2);
 Serial.println(sensorVal);


 if (sensorVal == HIGH) {
    digitalWrite(3, LOW);
   
  } else {
    digitalWrite(3, HIGH);
 
  }

if (sensorVal == LOW){
 
      MP3player.begin();    //MP3 Shield starten
    MP3player.setVolume(10, 10); //Die Lautstärke einstellen

    Serial.println("PLAY");
    MP3player.available();
    MP3player.playTrack(1);
    delay(1000);
  }

   }}

Grumpy_Mike

Quote
ain but also have to restart the board itself (unwire even) if i want to try again. Very bizarre, never experienced that.
That might be what you are doing but that is not necessary, that is simply not how processors of this type behave. At worst all you need is a power cycle when it gets stuck.


sutsche

That might be what you are doing but that is not necessary, that is simply not how processors of this type behave. At worst all you need is a power cycle when it gets stuck.


can you explain what you mean with the power cycle?
as mentioned before, I haven't experienced that either. In fact, restarting / stopping power isn't even enough.
If i want the code to run again and read the "right" values, I have to install a different code, then again the bespoke and then it starts again. Its seriously bizarre.

Grumpy_Mike

Quote
can you explain what you mean with the power cycle?
Removing all power and then restoring the power.

The behaviours you describe are simply not possible due to the way the processor is designed. You must be missing something.

Do you have parasite powering going on somewhere. That is the device you are removing the power from has some other powered device's output connected to an input of the unpowered thus causing that device to be not actually unpowered.

sutsche

SOLVED!


sorry for my lack of responding. i mainly didn't read the data sheet properly and used pins that were blocked.
happy to elaborate if there is interest.

cheers for the help

Go Up