Home › Forums › Sparkfun MP3 Shield Library Support Forum › Help with mp3shield sensor trigger
Tagged: Mp3 shield, sensor, trigger
- This topic has 2 replies, 2 voices, and was last updated 11 years, 4 months ago by Michael P. Flaga.
-
AuthorPosts
-
May 12, 2013 at 2:35 am #2615AnonymousInactive
Hi guys,
Firstly thanks heaps for the library 🙂
I am attempting a very simple project, one of which there is many examples of over the net. Just to trigger a single sample via a sensor input to the shield. I have attempted to use the cemetary howler project, along with a bunch of other codes, to get this to work. But for whatever reason, it is just not happening!
I have a number of different sensors(maxsonar, sharp distance, IR’s) so happy to change the code to suit one of those. This piece of code here I am using an IR sensor (E18-D80NK) that just returns 1’s and 0’s (0 for motion detected)
This is the little code I have put together, any hints? I am starting again from scratch now but not very good at this and having a huge amount of trouble routing the sensor input to trigger the MP3player.playTrack(1) command.
Cheers 🙂 I am all for teaching myself but its been a very stressful week and now I need some help from the experts!
Jack Colley
below is the code, sorry i couldnt figure out how to wrap it!
// libraries
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>// initialize MP3 card
SdFat sd;
SFEMP3Shield MP3player;// constant variables
int inputPin = 5;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // IR sensor input pin
unsigned long pauseTime = 10000;Â Â Â Â // how long the pause will be after music turned off
int readingInterval = 10;Â Â Â Â Â Â Â Â Â Â Â // interval to read the sensor// changing variables
int trigger = 1;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // variable for pin status
byte result;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // variable for mp3 player shield, can be used to debug// setup
void setup() {
pinMode(inputPin, INPUT);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // make sensor an input
digitalWrite(inputPin, HIGH);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // activate internal pull-up resistor
result = MP3player.begin();Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // start mp3 player shield
MP3player.setVolume(10, 10);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // set volume of the mp3 player}
// loop
void loop(){
trigger = digitalRead(inputPin);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // read sensor
if (trigger == 0) {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // if sensed
result = MP3player.playTrack(1);Â Â // play track
delay(pauseTime);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // wait…}
delay(readingInterval);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // wait with reading
}May 12, 2013 at 4:43 am #2616AnonymousInactiveUPDATE!!!
Have got it to work using both analog and digital sensors (two E18’s and a sharp distance)
here is my updated code. Any advice on it would be great, but it works!
// libraries
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>// initialize MP3 card
SdFat sd;
SFEMP3Shield MP3player;// constant variables
int inputPin = 0;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // IR sensor input pin
unsigned long pauseTime = 5000;Â Â Â Â // how long the pause will be after music turned off
int readingInterval = 10;Â Â Â Â // interval to read the sensor
int SignalPin=5;
int SignalPin2=4;
#define SignalPin 5
#define SignalPin2 4// setup
void setup() {
Serial.begin(9600);
sd.begin(SD_SEL, SPI_HALF_SPEED);
MP3player.begin();
MP3player.setVolume(10, 10);
pinMode(inputPin, INPUT);
pinMode(SignalPin, INPUT);}
// loop
void loop(){
if (analogRead(inputPin) > 450)Â Â Â Â Â Â {
MP3player.playTrack(2);
delay(pauseTime);}
{
{
int Signal = 1;Signal = digitalRead(SignalPin);
if (Signal == 1){
Serial.print (Signal);
Serial.println(” no obstacle”);
}else {
MP3player.playTrack(1);
delay(pauseTime);
}}
{
int Signal = 1;Signal = digitalRead(SignalPin2);
if (Signal == 1){
Serial.print (Signal);
Serial.println(” no obstacle”);
}else {
MP3player.playTrack(2);
delay(pauseTime);
}}
}delay(readingInterval);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // wait with reading
}
May 13, 2013 at 10:05 am #2620Michael P. FlagaMemberSee https://gist.github.com/mpflaga/5569322
Signal
only needs to be typed once and each digital read will over right its prior value. along with no need to compartmentalize as much.note the use of
while(MP3player.isPlaying()) {
//wait until done playing
}with out it you can attempt to play others, but will have an error as it is already playing. Note the commented out line of
MP3player.stopTrack();
which will help that if you desire. which won’t work with the abovewhile(MP3player.isPlaying())
as you wont’ get to it. So you have some choices. -
AuthorPosts
- You must be logged in to reply to this topic.