Go Down

Topic: SOLVED! PIR Motion to trigger VS1053 MP3 Shield (Read 6623 times) previous topic - next topic

selling1309

Aug 30, 2015, 04:25 am Last Edit: Oct 11, 2015, 09:21 pm by selling1309
I have found and tried to copy a sketch called the cemetery howler.
Simply. A motion sensor that triggers a sound clip.
I have been able to get the sketch uploaded to the Arduino UNO. My VS1053 MP3 Shield nested atop the Arduino and my PIR sensor connected to Pin5 as INPUT. Can someone more experienced please look over this sketch and help me resolve the error I get in the Serial Motion.


"If you get this error, you likely do not have a sd.begin in the main sketch, See Trouble Shooting Guide!
http://mpflaga.github.com/Sparkfun-MP3-Player-Shield-Arduino-Library/#Troubleshooting"

I got irritated and you can probably find sd.begin(); several times throughout the sketch. haha

Code: [Select]
// libraries

    #include <SPI.h>
    #include <SdFat.h>
    #include <SdFatUtil.h>
    #include <SFEMP3Shield.h>
    SFEMP3Shield MP3player;
    SdFat sd;
 
// constant variables 
   
    int pirPin = 5;                    // PIR sensor input pin
    int calibrationTime = 10;   
    unsigned long playTime = 15000;      // how long a file will play in milliseconds (15000=15 seconds)
    unsigned long pauseTime = 10000;     // how long the pause will be after the sound ends (10000=10 seconds)
    int readingInterval = 20;            // how often to read the sensor

   
// changing variables 


    int rantrack = 0;                    // track number for randomizing
    unsigned long currentMillis = 0;     // time variable to track running time: current time
    unsigned long startingMillis = 0;    // time variable to track running time: starting time
    byte result;                         // variable for mp3 player shield, can be used to debug
// setup     
     
    void setup() {
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);
  Serial.begin(115200);
  //give the sensor some time to calibrate
    for(int i = 0; i < calibrationTime; i++){
      delay(1000);
      }
    delay(50);
   pinMode(pirPin, INPUT); // make PIR sensor an input
  digitalWrite(pirPin, LOW); // activate internal pull-up resistor
  result = MP3player.begin(); // start mp3 player shield
  MP3player.setVolume(40, 40);                 // set volume of the mp3 player ( 0 = loudest )
    }
// loop
     
    void loop(){
  if (digitalRead(pirPin)== HIGH) {                // if movement sensed
          randomSeed(millis());                        // set a more random seed for the random function
          rantrack = random(55);                       // find random number
          result = MP3player.playTrack(rantrack);      // play track                         
          playtime();                                  // call function for play time       
         MP3player.stopTrack();                        // stop track   
          delay(pauseTime);                            // wait...   
    }
     
      delay(readingInterval);                          // wait with reading
   } 

// function to determine playtime

  void playtime() {
       startingMillis = millis();                                 // set both time counter
       currentMillis = millis();
      while ( currentMillis - startingMillis < playTime ) {      // while track plays, runs until playTime is reached
          currentMillis = millis();                               // set to actual time
          delay(40);                                              // debounce
       sd.begin();
       }     
   }
and again,
         Thank you for your help!

selling1309

Have I posted in the wrong section? Or am I just not the only one that cant make this dang thing work! lol
and again,
         Thank you for your help!

Grumpy_Mike

Quote
Have I posted in the wrong section?
No but you have not read the rules:-
How to use this forum

You use a very obscure library <SFEMP3Shield.h> without posting the link to it. You have also not posted the link to the original code.

Further you are requiring that some one actually has the VS1053 MP3 shield before they can try it out, so the pool of potential helpers is small.

The error message
Quote
"If you get this error, you likely do not have a sd.begin in the main sketch
Is being generated in one of the libraries not the IDE so you have a problem with how it works.

My pure guess is that either the SD card is not working or that the files on the SD card are not in a compatible format  for the library.

selling1309

#3
Sep 17, 2015, 06:09 pm Last Edit: Sep 17, 2015, 06:20 pm by selling1309
It took me some time to get back to this thread because I have been busy with construction of my project. Sorry I didn't provide enough info, allow me to try again.
As of right now (09/17/2015) I am still building and starting to wire my props.
I have a total of three Arduino Uno's,  L293D Motor Shield and a VS 1035B MP3 Shield.
I am building a Haunted Tree. It is about 12 foot tall, 3 feet wide. I am using phone wire to connect everything.

What I want this thing to do is:
1) Play ambiance music while standing idle
2) When motion is detected trigger one of 6 scares at random. Some scares are accompanied with a sound file
3) delay about 45 seconds or so and reset.

The scares:
1) The Crows: I will be running three mini servo motors inside crows turning their heads back and forth... I have two servos connected together to share the same movement and the third to run its own movement. I'd be happy with just random back and forth motions. I also have three red LED's to light the crows from above. I have a couple of sound files to play with the movement. I have all of this wired and ready to connect.

2) The Falling Limb: I will be triggering a 12v solenoid for my "falling Limb". Also wired and ready to be connected. A sound file to be played with this as well.

3) Dropping Spiders: I have two DC motors that are being used to raise and drop my spiders. Two spiders per wheel. I want them to rotate forward for X milliseconds, pause, then reverse. No sound downloaded yet but ideas are welcomed.

4) Clown I have a battery powered Clown prop that sits on a swing. I am going to tap it's "try me" button to activate the clown. He laughs and kicks his legs. No external sound needed.

5) Owl Hole: I have ordered a pack of 100 5mm LEDs. Max. Forward Voltage:2.0-2.4v,3.0-3.6V Max. Reverse Voltage:5uA
I'll use some of those to back light a silhouette owl or some other creature in the trunk of my tree.
Maybe flicker and/or flash.
I have downloaded a few MP3 files of owls and growls. I haven't decided if I want an owl in the tree or a monster.  

6) Stump: I will be creating a stump with pneumatics. When triggered a monster or demon will pop out of the top of the stump. Sound to be played and LED lights flicker/flash.

The Equipment:

PIR Motion Sensor
http://www.ebay.com/itm/1pcs-HC-SR501-Adjust-IR-Pyroelectric-Infrared-PIR-Motion-Sensor-Detector-Module-/380744781714


VS1053B MP3 Shield
http://www.geeetech.com/wiki/index.php/Arduino_MP3_shield_board_with_TF_card


Motor shield specs (Still in the mail)
http://yourduino.com/sunshop2/index.php?l=product_detail&p=292


DC Motors for spiders:
http://www.amazon.com/Wheel-Motor-Arduino-Smart-Robot/dp/B00U8VGXNE


Micro Servo Motors for Crows:
http://elab.pk/en/servo-motors/65-servo-motor-9g-mini.html


Solenoid Specs for Falling Limb and Stump (there are two separate ones) (This is from my shopping cart so I'm not sure if you'll be able to open it, If not let me know)
http://www.aliexpress.com/item/free-shipping-Solenoid-Air-Valve-5port-2position-BSP-4V110-06-DC-12V/32295653345.html

So.... the questions.
1) Is this doable?
2) Will I need all three arduinos?
3) Is phone wire acceptable to use to run most of this?
4) What sketch do I use? (wow, this is almost me asking someone to write it for me. As awesome and great as that would be I am willing to do leg work on this, I'm just running out of time)
5) Does anyone have any suggestions regarding the project to make it more scary/amazing?
and again,
         Thank you for your help!

Grumpy_Mike

Quote
1) Is this doable?
Yes

Quote
2) Will I need all three arduinos?
Just one should do it.

Quote
3) Is phone wire acceptable to use to run most of this?
Yes

Quote
4) What sketch do I use?
One you write, we can help when you get stuck but you write it. Plan it out on paper first and build things up a little at at time. The trick is to do a little then test, only when the tests are 100% passed go on to the next bit. Only you have the hardware which is why no one can write your code for you.

Quote
5) Does anyone have any suggestions regarding the project to make it more scary/amazing?
I have just made an animatronic skull, with eyes that light up and move, jaw opens and closes, cranium illuminated from inside, and neck moves from side to side. Also the nasal cavity lights up, any colour but my favorite colour is green. All LEDs have controlled brightness and the eyes can move individually.

selling1309

That is all amazing! I have been trying to sleep on it but I can't. Mind running wild. 
As far as the code; would the simplest rout be:
If motion is LOW (no motion) play random <ambient sound1,2,or 3>
If motion HIGH (motion presebt) then play random track <crow 1, crow 2, falling limb sound, monster sound 1, monster sound 2, etc.....>
AND
If crow 1 is played THEN servo motors left right X seconds and LEDs (depending on track time)
ELSE
If crow 2 is played THEN servos left right for X Seconds and LEDs ( track time)
ELSE
If falling limb is played THEN set pinX to HIGH to trigger pneumatic
ELSE
If monster sound 1 do whatever etc etc

This makes me feel like I'm working with an excellent spreadsheet, but that's okay, I understand that. lol
Thoughts on this method ?
I still haven't gotten this thing to even play sound with the motion. I need to get my butt in gear. I've been building and not programming.  It's like putting an amazing paint job on a crappy car that won't run!
and again,
         Thank you for your help!

Grumpy_Mike

Quote
Thoughts on this method ?
A bit spaghetti like.
I would look at the switch statement, generate a random number then use it to select exactly the action combination you want.

selling1309

Alright!
I have learned sooo much as a beginner. (well, as much as a full time employee with a family can learn)

I have had some great help along the way. (Thank you Mark)

I STILL can not get the MP3 player to play.
I get a generic error msg in my Serial Monitor.

"If you get this error, you likely do not have a sd.begin in the main sketch, See Trouble Shooting Guide!
http://mpflaga.github.com/Sparkfun-MP3-Player-Shield-Arduino-Library/#Troubleshooting
"

Bugs are good for Halloween... just not with this.
If you notice anything else that I need to address please please point it out! All help is appreciated!

Code: [Select]
// Haunted Tree
//No telling how many of those can be deleted...
#include <Servo.h>
#include <ArduinoStream.h>
#include <bufstream.h>
#include <ios.h>
#include <iostream.h>
#include <istream.h>
#include <MinimumSerial.h>
#include <ostream.h>
#include <Sd2Card.h>
#include <SdBaseFile.h>
#include <SdFat.h>
#include <SdFatConfig.h>
#include <SdFatmainpage.h>
#include <SdFatStructs.h>
#include <SdFatUtil.h>
#include <SdFile.h>
#include <SdInfo.h>
#include <SdSpi.h>
#include <SdStream.h>
#include <SdVolume.h>
#include <SPI.h>
#include <SFEMP3Shield.h>
#include <SFEMP3ShieldConfig.h>
#include <SFEMP3Shieldmainpage.h>
SFEMP3Shield MP3player;
SdFat sd;
// input pins
#define PIR1 A1
 
// output pins
  
Servo CROW1;
Servo CROW2;

#define FX1 0
#define CROWLED 4
#define CLOWNSWING 1
#define FALLINGLIMB A5
#define OWLHOLE 10
#define STUMPJUMP A4
#define STUMPLED A3
#define SCK 13
#define MISO 12
#define MOSI 11
#define CS 9
#define XRESET 8
#define XDCS 7
#define XCS 6
#define DREQ 2


// other global defines and variables
#define N_EFFECTS 6
int pos = 0;    // variable to store the servo position
byte last_effect=0; //Cause I was told to
 

/////////////////////////////
// code start
/////////////////////////////
// effect #0
void boo() {
  Serial.print("BOO!\n");
  // sample flash code
  for (byte i=0;i<6;i++)
  {
    digitalWrite(FX1,HIGH);
    delay(100);
    digitalWrite(FX1,LOW);
    delay(100);
  }
}

//Crows
//Crow1
void Crows(){
MP3player.playTrack(001); //play crow caws
Serial.print("Crow1\n");
digitalWrite(CROWLED, HIGH);
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    CROW1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    CROW1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
   
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    CROW1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    CROW1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
   
  CROW1.write(90);


//Crow2
Serial.print("Crow2\n");
  for (pos = 0; pos <= 180; pos += 1){  // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    CROW2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1)  // goes from 180 degrees to 0 degrees
    CROW2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
     
CROW2.write(25);
digitalWrite(CROWLED, LOW);
delay(4000);    
  }}}
 
      
//ClownSwing
void ClownSwing(){
  Serial.print("ClownSwing\n");
digitalWrite (CLOWNSWING, LOW);
digitalWrite (CLOWNSWING, HIGH); //activate tryme button on prop
delay(100);
digitalWrite (CLOWNSWING, LOW);
delay(4000);

}

//FallingLimb
void FallingLimb(){
  Serial.print("FallingLimb\n");
MP3player.playTrack(003); //play falling limb sound and screams
  digitalWrite(FALLINGLIMB, HIGH);   // turn the solenoid on Limb Falls
  delay(20000);              // wait for a second or twenty
  digitalWrite(FALLINGLIMB, LOW);    // turn the solenoid off
  delay(1000);              // wait for a second
}
//Spiders
void Spiders(){
  Serial.print("EEK! SPIDERS!\n");
MP3player.playTrack(004);
//spiders1
//drive motor forward 5 seconds
//stop
//drive revers 5 second
//stop
//spiders2
//drive motor forward 2 seconds
//stop
//drive motor forward 3 seconds
//stop
//revers 2 seconds
//forward 1 second
//revers 4 seconds
delay(1000);
  }

//OwlHole
void OwlHole(){
  Serial.print("Owl\n");
MP3player.playTrack(005); //play Owl hoots
digitalWrite(OWLHOLE, LOW);
digitalWrite(OWLHOLE, HIGH); //activate LED's to backlight owl in hole
delay(1000);
digitalWrite(OWLHOLE, LOW);
delay(600);
digitalWrite(OWLHOLE, HIGH);
delay(1000);
digitalWrite(OWLHOLE, LOW);
delay (4100);
digitalWrite(OWLHOLE, HIGH);
delay (2000);
digitalWrite(OWLHOLE, LOW);  //thats what i think an Owl hoot will look like
delay(3000);
digitalWrite(OWLHOLE, HIGH); //activate LED's to backlight owl in hole
delay(1000);
digitalWrite(OWLHOLE, LOW);
delay(200);
digitalWrite(OWLHOLE, HIGH);
delay(1000);
digitalWrite(OWLHOLE, LOW);
delay (300);
digitalWrite(OWLHOLE, HIGH);
delay (2000);
digitalWrite(OWLHOLE, LOW); 
delay (4000);
}

//Stump
void StumpJump() {
  Serial.print("Stump\n");
  MP3player.playTrack(006); //play scream scare
  digitalWrite(STUMPJUMP, HIGH);   // turn the solenoid on Limb Falls
  digitalWrite(STUMPLED, HIGH);
  delay(20000);              // wait for a second or twenty
  digitalWrite(STUMPJUMP, LOW);    // turn the solenoid off
  delay(1000);              // wait for a second
  digitalWrite(STUMPLED, LOW); //I will have flow control on line .Keep it lit while retracts
delay(4000);
}

void nothing(){
  if (digitalRead(PIR1) == LOW){
Serial.print("DID YOU HEAR THAT?\n");
      MP3player.playTrack(007); //play ambiant music when not scaring ppl (might as well do something while its doing nothing)
    
}}
// add more effect routines
// no, no more...I just cant right now
 
// end effect routines
//////////////////////////////
byte generateEvent() {
  byte ev=last_effect;
  while (ev==last_effect)
    ev=random(N_EFFECTS);
  return ev;
}
 
/////////////////////////////
/////////////////////////////
 
void setup()
{
  Serial.begin(9600);      // open the serial port at 9600 bps:
  Serial.print("You turned me on.\n");   
  Serial.print("sd begin\n");
  sd.begin();
  delay(1000);
  MP3player.begin();
  Serial.print("MP3 Begin\n");
  delay(1000);
  MP3player.setVolume(40, 40);
  delay(2000);
  pinMode(PIR1,INPUT);
  pinMode(FX1,OUTPUT);
  pinMode (CLOWNSWING, OUTPUT);
  pinMode (FALLINGLIMB, OUTPUT);
  pinMode (OWLHOLE, OUTPUT);
  pinMode (STUMPJUMP, OUTPUT);
  pinMode (STUMPLED, OUTPUT);
  CROW1.attach(5);
  CROW2.attach(3);
  pinMode(MP3_DREQ, INPUT);
  pinMode(MP3_XCS, OUTPUT);
  pinMode(MP3_XDCS, OUTPUT);
  pinMode(MP3_RESET, OUTPUT);

 randomSeed(analogRead(8)); //Is this the number of scares?
 }
 
void loop() {
  byte effect;
  if (digitalRead(PIR1))// throw a ! in front of the digital Read if the signal goes low when active
  {
    effect = generateEvent();
    switch (effect) {
    case 1:
      Crows();
     
        break;
      case 2:
      ClownSwing();
        break;
      case 3:
      FallingLimb();
        break;
      case 4:
      Spiders();
        break;
      case 5:
      OwlHole();
        break;
      case 6:
      StumpJump(); 
      break;
     case 7:
        boo();
        break;
   
      default:
      nothing();
 }// end switch case
  }// end if PIR sensor
 
}// end main loop
/////////////////////////////
//This sketch started 9/22/2015
//Thank you Mark MacDonald! You Rock!!!


and again,
         Thank you for your help!

Grumpy_Mike

It sounds to me like you have a hardware problem. Something like the SD card not having the correct format. Does it work with the examples that came with the library?

selling1309

#9
Sep 26, 2015, 06:39 am Last Edit: Sep 26, 2015, 06:48 am by selling1309 Reason: adding attachement
Yes, I was able to get the same files to play using the SFEMP3Shield example. I have kinda re-worked my sketch a bit... but I still get the same error.

You turned me on.
sd begin: 0
MP3 Begin: If you get this error, you likely do not have a sd.begin in the main sketch, See Trouble Shooting Guide!
http://mpflaga.github.com/Sparkfun-MP3-Player-Shield-Arduino-Library/#Troubleshooting
6
Owl

Not sure what the "6" is.

and again,
         Thank you for your help!

Grumpy_Mike

Have you tried it with all the servos disconnected?

Grumpy_Mike

The thing is when you are faced with a problem like this you have to hack at your code until it works.
So take a safe copy of your final code. Then hack as much of it down. If it were me I would do this at the loop function level. Just have a simple loop that consists of calling the owl function then a few seconds delay. Remove all motor movement commands from the owl function and see what happens.
If you still get the same results then disconnect the PIR and the motors.

Make the bit of your code close to the examples that work. Then add in the extra hardware one at a time testing all the way.

In future don't get the code so big and complex before you test. The trick is to make small changes and then test, then you have an idea where things start to go wrong.

cowjam

Are you using the geeetech mp3 shield?

I'm playing with one now and needed a bit of help so headed here (but I got it playing sound).

I used adafruit's code instead of the geeetech stuff. You can download the libraries here: https://learn.adafruit.com/adafruit-vs1053-mp3-aac-ogg-midi-wav-play-and-record-codec-tutorial/software

Copy the libraries into the libraries file of your arduino installation folder.

In the arduino program go to Examples and look for 'Adafruit VS1053 Library' and open 'player simple'.

In the sketch you'll need to change the pins to match your shield.  If it's the geeetech one then set the 'breakout_reset' to 8, breakout_CS to 6, breakout_DCS to 7, CARDCS to 9 and DREQ to 2.

It requires some MP3s on the shield called track001.mp3 and track002.mp3.

You should be able to use this as a base, and work up from here to get it to trigger.

selling1309

I am becoming so discouraged right now with this thing! grrrr
Yes, I do have the geeetech shield. I followed your advice and changed the pins and tried to get it to work and still no luck. I went back and used the SFMP3Shield library and uploaded the "MP3Shield_Library_Demo" and I couldn't get it to play my files like I could before.
in the Serial monitor I got "Error code: 2 when trying to play track" and from my research I see that its due to SdFat library. I uninstalled all my libraries and re-installed them and still getting the same result.
I switched my SD cards and formatted the new one, uploaded my files and titled them each as track00X.mp3 I have six tracks total.
I had hopped to get this thing working by October 1st but it's not going to happen. Way too much to do still. I'm happy I started when I did! lol (Not your problem, I'm just venting I think.)
I think I may end up having a MP3Shield for sale soon!
Most of everything else is working acceptable but i'm just now starting to wire it all together.
I'll probably be posting seeking for help on other things very soon... Why do we do this to ourselves?!
and again,
         Thank you for your help!

Grumpy_Mike

Quote
Why do we do this to ourselves?!
Because in the end it will be fun!

un installing the libraries doesn't actually do anything apart from removing the file from a folder. But adding a file to the folder requires you restart the Arduino IDE before it takes note of it.

You need to get that demo working before anything else.

Go Up