Skip to content

Instantly share code, notes, and snippets.

Created March 17, 2017 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b12b55d78dcc79cda786c1433e39472c to your computer and use it in GitHub Desktop.
Save anonymous/b12b55d78dcc79cda786c1433e39472c to your computer and use it in GitHub Desktop.
//MP3 shield libraries
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
//Initialize sd card and MP3 shield
SFEMP3Shield MP3player;
SdFat sd;
//Create variable to store pressure sensor value
int analogValue=0;
//Stores if the USB is inside the box
boolean isInside=false;
//Variables control which sound will be played
boolean isCheering=false;
boolean isFirst=true;
boolean isSigh=false;
boolean isSniffle=false;
boolean isCry=false;
//Variable used for timing and creating intervals between audio
unsigned long previousMillis=0;
void setup() {
Serial.begin(9600);
sd.begin(SD_SEL, SPI_HALF_SPEED);
MP3player.begin();
//Increase the volume of audio output
MP3player.setVolume(0x00, 0x00);
}
void loop() {
//Calls the function to check if the USB is in box
isInside=boxFull();
unsigned long currentMillis=millis();
//If the USB is removed and no other sound has been played, "oh, no" plays
if(!isInside && isFirst){
previousMillis=currentMillis;
//Stops any track already playing
MP3player.stopTrack();
//Ensures that cheer plays when USB returned
isCheering=true;
isFirst=false;
//Calls function to play "oh no"
playSigh();
}
//If checks that the USB is outside box and its been 10 seconds since last track play
//also makes sure that sniffle isn't played twice in a row
else if(currentMillis-previousMillis>10000 && !isInside && !isSniffle){
previousMillis=currentMillis;
MP3player.stopTrack();
isCheering=true;
isFirst=false;
//Ensures that cry plays next time
isSniffle=true;
isCry=false;
//Cals function to play sniffle
playSniffle();
}
//If checks that the USB is outside box and its been 10 seconds since last track play
//also makes sure that cry isn't played twice in a row
else if(currentMillis-previousMillis>10000 && !isInside && !isCry){
previousMillis=currentMillis;
isCheering=true;
//Ensures that sniffle plays next time
isCry=true;
isSniffle=false;
//Calls function to play sniffle
playCry();
}
else if(isCheering && isInside){
MP3player.stopTrack();
previousMillis=0;
//Calls function to play cheer
playCheer();
//Resets variables for when USB is removed again
isCheering=false;
isFirst=true;
isSigh=false;
isSniffle=false;
isCry=false;
}
}
boolean boxFull(){
//Gets analog force sensor value
analogValue=analogRead(A0);
Serial.println(analogValue);
//Checks if the USB is inside or outside
if(analogValue<50){
//USB is outside
return false;
}
else{
//USB is inside
return true;
}
}
//Functions to play certain track numbers from sd card
void playSigh(){
MP3player.playTrack(1);
}
void playSniffle(){
MP3player.playTrack(2);
}
void playCry(){
MP3player.playTrack(3);
}
void playCheer(){
MP3player.playTrack(4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment