What I am trying to accomplish is the following:
- In setup, set all my globals to false -- easy
- In the loop, check my entry sensor, if it trips run all my other routines --- not so easy
What does "run all my other routines mean?"
Sensor tripped-->play sound file --> while sound file is playing do all these things
- Lower the crossing signal arms
- Turn the crossing signal flashers on and they stay on until I say they turn off
- Turn the road power off and it stays off until I turn it back on
Once the exitSensor is tripped, turn road power back on, raise signal arms, turn flashers off, then stop sound card from playing.
For right now I am simply trying to figure out how to just keep the sound playing after I trip the sensor. It only plays one time for a split second now.
I am a C# ASP.Net guy so continuously running loops loops are not my forte.
//Libraries Required
//Music Maker MP3 -- include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// Pins used for the music maker shield
#define SHIELD_RESET -1 // VS1053 reset pin (unused!)
#define SHIELD_CS 7 // VS1053 chip select pin (output)
#define SHIELD_DCS 6 // VS1053 Data/command select pin (output)
// Common pins
#define CARDCS 4 // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
//global variables required
boolean crossingSignalInOperation = false;
boolean signalEntrySensor = false;
boolean debugMode = false;
//Functions
void onSignalSound(Adafruit_VS1053_FilePlayer mp, const char* fileName ) {
// Play the train crossing sound file
if (debugMode)
{
Serial.println(F("Playing track - "));
Serial.print(fileName);
}
//all sound files are on the micro sd card on the music player shield
if (!mp.startPlayingFile(fileName)) {
Serial.println("Could not open file ");
Serial.print(fileName);
while (1);
}
while (mp.playingMusic) {
// file is now playing so run other routines
//offRoadPower();
//lowerSignalArms();
//startSignalFlashers();
// crossingSignalInOperation = true;
delay(4000); //to see if it will play for more than a split second
}
doOnce = true;
}
void offSignalSound(Adafruit_VS1053_FilePlayer mp){
mp.stopPlaying();
}
boolean checkEntrySensor() {
//check entry sensor, if tripped return true otherwise false
return true; //no code yet, just testing now
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
debugMode = true; //set this to false once code is fully operational
//initialise music maker shield
if (!musicPlayer.begin()) {
if (debugMode)
{
Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
}
while (1);
}
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(20, 20);
if (debugMode)
{
Serial.println(F("VS1053 found"));
}
// initialise the SD card
SD.begin(CARDCS);
//ensure our global booleans all start false
signalEntrySensor = false ;
}
void loop() {
// put your main code here, to run repeatedly
signalEntrySensor = checkEntrySensor();
if (signalEntrySensor == true) {
if (doOnce == false) {
onSignalSound(musicPlayer, "trainxng.mp3"); //all other functions are called in the sound routine?
if (debugMode)
{
Serial.println("Sound byte called");
}
doOnce = true;
}
}
}
Any help appreciated.