Use accelerometer to interrupt.
Dependencies: mbed SDFileSystem
Fork of shomberg_hw_7 by
Diff: main.cpp
- Revision:
- 29:d33071ffaa5f
- Parent:
- 28:a59485b1626b
--- a/main.cpp Fri Nov 16 19:53:25 2018 +0000 +++ b/main.cpp Sun Nov 18 19:24:26 2018 +0000 @@ -8,6 +8,8 @@ #include "sensors.h" #include "dataLog.h" +DigitalOut ledData(LED1); +DigitalOut ledAccel(LED2); void initiateLogs(void); void runDataLog(void); @@ -40,13 +42,12 @@ // Variables for sensors Ticker dataLogger; -MMA8452Q accel( PIN_ACCEL_SDA, PIN_ACCEL_SDA, 0x1D ); +MMA8452Q accel( PIN_ACCEL_SDA, PIN_ACCEL_SCL, 0x1D ); InterruptIn accelInterrupt(PIN_ACCEL_INTERRUPT); Ticker accelLogger; int main() { - initiateLogs(); // Set up accelerometer @@ -56,21 +57,21 @@ //start timer and begin main loop t.start(); while(1) { + ledData = 0; + while( !readSwitch() ) { - while( !readSwitch() ) { // nothing happens when the switch is off } - // Once switch is on intiate all logs // and start loop until switch is off fpData = fopen( dataLog, "a" ); - dataLogger.attach(&runDataLog,1); + dataLogger.attach( &runDataLog, .5 ); + ledData = 1; while(readSwitch()) { // Main loop for logging wait( .1 ); } // exit loop when switch is turned off - dataLogger.detach(); fclose( fpData ); @@ -86,7 +87,7 @@ currentTemp = mvToTemp( currentVoltage ); // Record to file - fprintf( fpData, "%f, %f, %f, %f \n\r", currentTime, currentVoltage, currentTemp, currentTemp-startingTemp ); + fprintf( fpData, "%f, %f, %f, %f \n", currentTime, currentVoltage, currentTemp, currentTemp-startingTemp ); } @@ -101,18 +102,27 @@ // Open new files and create headers fpData = fopen( dataLog, "r" ); - fprintf(fpData,"Time (s), Voltage (mv), Temperature (C), Delta T (C)\n\r"); + if ( fpData==NULL ) { + fpData = fopen( dataLog, "w" ); + fprintf(fpData,"Time (s), Voltage (mv), Temperature (C), Delta T (C)\n\r"); + } fclose( fpData ); fpAccel = fopen( accelLog, "r" ); - fprintf(fpAccel, "Time (s), X, Y, Z \n\r"); + if ( fpAccel==NULL ) { + fpAccel = fopen( accelLog, "w" ); + fprintf(fpAccel, "Time (s), X, Y, Z \n"); + } fclose( fpAccel ); } void startAccelLog() { fpAccel = fopen( accelLog, "a" ); - + accelLogTimer.reset(); + accelLogTimer.start(); + accelLogger.attach( &runAccelLog, 0.02 ); + ledAccel = 1; } @@ -124,7 +134,7 @@ float y = accel.readY(); float z = accel.readZ(); - fprintf( fpAccel, "%f, %f, %f, %f", t.read(), x, y, z ); + fprintf( fpAccel, "%f, %f, %f, %f \n\r", t.read(), x, y, z ); } else { endAccelLog(); return; @@ -133,8 +143,9 @@ void endAccelLog() { + ledAccel = 0; accelLogTimer.stop(); - accelLogTimer.reset(); accelLogger.detach(); fclose( fpAccel ); + accel.readRegister( REG_TRANSIENT_SRC ); } \ No newline at end of file