Created
March 17, 2017 00:12
-
-
Save jesu0404/b5e8a1742f924329d4efe356d4d97394 to your computer and use it in GitHub Desktop.
This code runs the Accelerometer, speaker , and MP3 shield. The arduino is constantly reading the coordinate values of the accelerometer and triggers an alarm sound to play when the corrdinates change more than a certain amount. It also plays a coin sound when it gets the signal from the other arduino.
This file contains 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
#include "SparkFunLIS3DH.h" | |
#include "Wire.h" | |
#include "SPI.h" | |
#include <SdFat.h> // SDFat Library | |
#include <SdFatUtil.h> // SDFat Util Library | |
#include <SFEMP3Shield.h> // Mp3 Shield Library | |
LIS3DH myIMU; //Default constructor is I2C, addr 0x19. | |
int val=0; | |
int pinCheck= 5; | |
uint8_t result; | |
float xcord=0; | |
float ycord=0; | |
float zcord=0; | |
SdFat sd; | |
SFEMP3Shield MP3player; | |
const uint8_t volume = 0; // MP3 Player volume 0=max, 255=lowest (off) | |
const uint16_t monoMode = 1; // Mono setting 0=off, 3=max | |
bool playSound= false; | |
char trackName[] = "track001.mp3"; | |
char trackTwo[] = "track002.mp3"; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
delay(1000); //relax... | |
Serial.println("Processor came out of reset.\n"); | |
pinMode(pinCheck, INPUT); | |
//Call .begin() to configure the IMU | |
myIMU.begin(); | |
/* Set up all trigger pins as inputs, with pull-ups activated: */ | |
initSD(); // Initialize the SD card | |
initMP3Player(); // Initialize the MP3 Shield | |
} | |
void loop() | |
{ | |
val= digitalRead(pinCheck); | |
xcord= myIMU.readFloatAccelX(); | |
ycord= myIMU.readFloatAccelY(); | |
zcord= myIMU.readFloatAccelZ(); | |
if(val ==HIGH){ | |
Serial.println("Coin Sound is playing"); | |
result = MP3player.playMP3(trackTwo); | |
delay(1000); | |
} | |
if(val ==LOW){ | |
Serial.println("Coin Sound dead"); | |
MP3player.stopTrack(); | |
} | |
if(xcord > -.5 || xcord < -1.2){ | |
playSound= true; | |
Serial.println("Xcordinate"); | |
} | |
else if(ycord > .2 || ycord < -.4){ | |
playSound= true; | |
Serial.println("Ycordinate"); | |
} | |
else if(zcord > .6 || zcord < -.1){ | |
playSound= true; | |
Serial.println("Zcordinate"); | |
} | |
else{ | |
playSound=false; | |
MP3player.stopTrack(); | |
Serial.println("Play sound is false"); | |
} | |
if(playSound == true){ | |
Serial.println("Play sound is true"); | |
Serial.println("About to play track"); | |
result = MP3player.playMP3(trackName); | |
} | |
//Get all parameters | |
Serial.print("\nAccelerometer:\n"); | |
Serial.print(" X = "); | |
Serial.println(xcord, 4); | |
Serial.print(" Y = "); | |
Serial.println(ycord, 4); | |
Serial.print(" Z = "); | |
Serial.println(zcord, 4); | |
delay(1000); | |
} | |
void initSD() | |
{ | |
//Initialize the SdCard. | |
if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) | |
sd.initErrorHalt(); | |
if(!sd.chdir("/")) | |
sd.errorHalt("sd.chdir"); | |
} | |
void initMP3Player() | |
{ | |
uint8_t result = MP3player.begin(); // init the mp3 player shield | |
if(result != 0) // check result, see readme for error codes. | |
{ | |
// Error checking can go here! | |
} | |
MP3player.setVolume(volume, volume); | |
MP3player.setMonoMode(monoMode); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment