Only collects data for a few seconds, to a few minutes before an interference noise comes out of the headset and from then on only plays music.
Code: Select all
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#define LOG_INTERVAL 20 // mills between entries (reduce to take more/faster data)
// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()
// the digital pins that connect to the LEDs
#define redLEDpin 2
// The analog pins that connect to the sensors
#define xInput 0 // analog 0
#define yInput 1 // analog 1
#define zInput 2 // anaolg 2
#define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter!
// These are the 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)
#define CARDCS 4 // Card chip select pin
#define MusicPlayer_CS 4
#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin
// Define music Player
Adafruit_VS1053_FilePlayer musicPlayer =
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
// Calibrated Accelerometer Ranges:
// ****************************************************************
// Determine the values for each axis at +/- 1g using AccelCalib
int xNeg = 490;
int xPos = 530;
int yNeg = 490;
int yPos = 531;
int zNeg = 489;
int zPos = 529;
// ****************************************************************
RTC_DS1307 RTC; // define the Real Time Clock object
DateTime now ;
uint32_t m;
int xReading=0;
int yReading=0;
int zReading=0;
long xScaled ;
long yScaled ;
long zScaled ;
int nowSecond, nowMinute, nowHour, nowUnix, nowDay, nowMonth, nowYear;
float xAcc;
float yAcc ;
float zAcc ;
char filename[] = "LOGGER00.CSV";
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
// the logging file
File logfile;
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
DateTime now = RTC.now();
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(now.year(), now.month(), now.day());
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(now.hour(), now.minute(), now.second());
}
void setup(void)
{
Serial.begin(9600);
Serial.println();
// initialise the music player
if (! musicPlayer.begin()) { // initialise the music player
Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
while (1); //hang here dont do anything else
}
if (!SD.begin(CARDCS)) {
Serial.println(F("Music SD or Data Logger card failed, or not present"));
while (1); // don't do anything more
}
// lower number = higher volume
musicPlayer.setVolume(15,15);
// Note that even if it's not used as the CS pin, the hardware SS pin
// 10 on most Arduino boards must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
pinMode(4, OUTPUT);
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
}
// ######################################################
RTC.adjust(DateTime(__DATE__, __TIME__)); // *** uncomment to set RTC for first time ******
// #####################################################
// set date time callback function
SdFile::dateTimeCallback(dateTime);
// create a new file
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE); // only writing to one file in case of restarts while running.
break; // leave the loop!
}
}
logfile.println("millis,stamp,datetime,x,y,z");
logfile.flush(); // write first line header
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);
// This option uses a pin interrupt. No timers required! But DREQ
// must be on an interrupt pin. For Uno use Digital pin 3
if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT)) //must stay pin 3 for music player
Serial.println(F("DREQ pin is not an interrupt pin"));
}
void loop(void)
{
if ( musicPlayer.startPlayingFile("OneFile.mp3")) { //do nothing its working
} //had to merge all files to one to run longer than one song
else if ( musicPlayer.startPlayingFile("TwoFile.mp3")) {//do nothing its working
}
else{
Serial.println(F("Could not open music file"));
while(1); // don’t do anything if music cant play
}
while (musicPlayer.playingMusic) {
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
musicPlayer.pausePlaying(true); //pause long enough to take data points and possibly write to SD
// get data from accelerometer - put in log file - wont write until time.
m = millis();
logfile.print(m); // milliseconds since start logfile is defined as: File logfile
logfile.print(", ");
// fetch the time
// log time
now = RTC.now();
logfile.print(now.unixtime()); // seconds since 1/1/1970
logfile.print(", ");
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
analogRead(xInput);
xReading = analogRead(xInput);
analogRead(yInput);
yReading = analogRead(yInput);
analogRead(zInput);
zReading = analogRead(zInput);
// Convert raw values to 'milli-Gs"
xScaled = map(xReading, xNeg, xPos, -1000, 1000);
yScaled = map(yReading, yNeg, yPos, -1000, 1000);
zScaled = map(zReading, zNeg, zPos, -1000, 1000);
// re-scale to fractional Gs
xAcc = xScaled / 1000.0;
yAcc = yScaled / 1000.0;
zAcc = zScaled / 1000.0;
logfile.print(", ");
logfile.print(xAcc);
logfile.print(", ");
logfile.print(yAcc);
logfile.print(", ");
logfile.print(zAcc);
logfile.println();
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) {
}//continue just storing in the log file; else flush to file
else {
syncTime = millis();
logfile.flush(); // actually write to file now
}
musicPlayer.pausePlaying(false); // turn music back on right now
// when noise is heard never get next line ******************* and no more data is logged.
Serial.println();
Serial.println((millis())); // so I see it come back from music
}// end of while playing music
}// end of loop()