Last active
February 10, 2016 16:12
-
-
Save mpflaga/5522030 to your computer and use it in GitHub Desktop.
Froggy14's code with corrections as to allow it to get compiled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// libraries | |
#include <SPI.h> | |
#include <SdFat.h> | |
#include <SdFatUtil.h> | |
#include <SFEMP3Shield.h> | |
// initialize MP3 card | |
SdFat sd; | |
SFEMP3Shield MP3player; | |
// 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); | |
//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 | |
if(!sd.begin(9, SPI_HALF_SPEED)) sd.initErrorHalt(); | |
if (!sd.chdir("/")) sd.errorHalt("sd.chdir"); | |
result = MP3player.begin(); // start mp3 player shield | |
MP3player.setVolume(10, 10); | |
} | |
// loop | |
void loop() { | |
if (digitalRead(pirPin) == HIGH) { // if movement sensed | |
randomSeed (millis()); // set a more random seed for the random function | |
rantrack = random(5);// 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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment