Use accelerometer to interrupt.

Dependencies:   mbed SDFileSystem

Fork of shomberg_hw_7 by Russell Shomberg

Files at this revision

API Documentation at this revision

Comitter:
rshomberg
Date:
Sun Nov 18 19:24:26 2018 +0000
Parent:
28:a59485b1626b
Commit message:
working code. Tested. Works well.; Doesn't have ability to change interrupt.

Changed in this revision

MMA8452Q.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
sensors.h Show annotated file Show diff for this revision Revisions of this file
diff -r a59485b1626b -r d33071ffaa5f MMA8452Q.cpp
--- a/MMA8452Q.cpp	Fri Nov 16 19:53:25 2018 +0000
+++ b/MMA8452Q.cpp	Sun Nov 18 19:24:26 2018 +0000
@@ -26,15 +26,18 @@
 {
     // Check to make sure the chip's ID matches the factory ID
     uint8_t c = readRegister(REG_WHO_AM_I);
+    printf("ID %c",c);
     if( c != FACTORY_ID ) {
         return false;
     }
-
     // Set default scale and data rate
     standby();
     setScale(DEFAULT_FSR);
+    printf("1");
     setODR(DEFAULT_ODR);
+    printf("2");
     setInterrupt();
+    printf("interrupt");
     active();
 
     return true;
@@ -186,7 +189,7 @@
     writeRegister( REG_CTRL_REG4, 0x20 ); // turn on transient flag
     writeRegister( REG_CTRL_REG5, 0x20 ); // route transient interrupt to pin1
     writeRegister( REG_TRANSIENT_CFG, 0x1E ); // detection turned on for x,y,z and reading source clears flag
-    //writeRegister( REG_TRANSIENT_THS,  ); //
+    writeRegister( REG_TRANSIENT_THS, 0x10 ); //
     //writeRegister( REG_TRANSIENT_COUNT,  ); //
 
 }
diff -r a59485b1626b -r d33071ffaa5f main.cpp
--- 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
diff -r a59485b1626b -r d33071ffaa5f sensors.h
--- a/sensors.h	Fri Nov 16 19:53:25 2018 +0000
+++ b/sensors.h	Sun Nov 18 19:24:26 2018 +0000
@@ -14,9 +14,9 @@
 #define PIN_TEMP_SENSOR p20
 
 // Pin Configuration Accel
-#define PIN_ACCEL_SDA           p27
-#define PIN_ACCEL_SCL           p28
-#define PIN_ACCEL_INTERRUPT     p26
+#define PIN_ACCEL_SDA           p28
+#define PIN_ACCEL_SCL           p27
+#define PIN_ACCEL_INTERRUPT     p14
 
 #define TRAANSIENT_RECORD_TIME  3 //seconds