11/16

Dependencies:   mbed

Revision:
1:e41af8c958b8
Parent:
0:0dc99d71b2f3
Child:
3:08f020c3d2b7
--- a/main.cpp	Sun Nov 08 18:02:02 2015 +0000
+++ b/main.cpp	Fri Nov 20 19:27:43 2015 +0000
@@ -9,16 +9,17 @@
 AnalogIn XIn(p15); //X Input
 AnalogIn YIn(p16); //Y Input
 AnalogIn ZIn(p17); //Z Input
-//Serial pc(USBTX, USBRX); //Serial to print readings to terminal
+Serial pc(USBTX, USBRX); //Serial to print readings to terminal
 Ticker timer; //Timer to call ISR
 LocalFileSystem local("local"); // create local filesystem
-BusOut leds(LED4,LED3,LED2,LED1); //debug
+BusOut leds(LED4,LED3,LED2,LED1); //debugio
 
 //Global Variables
 float XOut, YOut, ZOut; //variables to store XYZ readings
-float CalX, CalY, CalZ; //variables to store 0G bias readings
+float Xo, Yo, Zo; //variables to store 0G bias readings
 int flag; //flag for ISR
-float X[1000], Y[1000], Z[1000]; //arrays to store results
+float X[2000], Y[2000], Z[2000]; //arrays to store results
+float CalX[20], CalY[20], CalZ[20]; //arrays to store calibration readings
 int n; //variable to store the number of readings
 
 //Function Prototypes
@@ -30,24 +31,47 @@
 //Main Function    
 int main() {
     
-    wait(2.0); //allow accelerometer to stabilise
+    wait(5.0); //allow accelerometer to stabilise
     Calibrate();
-    timer.attach(&ISR, 0.1); //attach ISR function to timer
+    pc.printf("Ready in:");
+    for (int i = 10; i > 0; i--) {
+        pc.printf("%d\n\r", i);
+        wait(1.0);
+    }
     
-    while (n < 1000) {
+    timer.attach(&ISR, 0.005); //attach ISR function to timer (100Hz)
+    
+    while (n < 2000) {
         if (flag) { //if flag has been set
             flag = 0; //reset flag
             Send_Data();
         }
     }
+    pc.printf("Writing...\n\r");
     writeDataToFile();
+    pc.printf("Done.\n\r");
 }
 
 //Function Definitions
 void Calibrate() {
-    CalX = XIn.read() * 3.3; //static values
-    CalY = YIn.read() * 3.3;
-    CalZ = ZIn.read() * 3.3;
+    
+    for (int i = 0; i < 20; i++) { //read and store 20 samples
+        CalX[i] = XIn.read() * 3.3;
+        CalY[i] = YIn.read() * 3.3;
+        CalZ[i] = ZIn.read() * 3.3;
+    }
+    
+    for (int i = 0; i < 20; i++) { //sum readings
+        Xo = Xo + CalX[i];
+        Yo = Yo + CalY[i];
+        Zo = Zo + CalZ[i];
+    }
+        
+        Xo = Xo / 20; //find mean and store it
+        Yo = Yo / 20;
+        Zo = Zo / 20;
+        
+        pc.printf("Calibration Values = %.3f | %.3f | %.3f\n\r", Xo, Yo, Zo);
 }
 
 void ISR() {
@@ -55,15 +79,15 @@
 }
 
 void Send_Data() {
-    XOut = (XIn.read() * 3.3) - CalX; //differences
-    YOut = (YIn.read() * 3.3) - CalY;
-    ZOut = (ZIn.read() * 3.3) - CalZ;
-    X[n] = XOut; //store result in array
-    Y[n] = YOut;
-    Z[n] = ZOut;
+    XOut = (XIn.read() * 3.3) - Xo; //differences
+    YOut = (YIn.read() * 3.3) - Yo;
+    ZOut = (ZIn.read() * 3.3) - Zo;
+    X[n] = XOut / 0.3; //convert voltage to g-force (300mv/g) and store in array
+    Y[n] = YOut / 0.3;
+    Z[n] = ZOut / 0.3;
     n++;
         
-   //pc.printf("%d) X = %.2f | Y = %.2f | Z = %.2f\n\r", n, XOut, YOut, ZOut);
+   pc.printf("%d) X = %.2f | Y = %.2f | Z = %.2f\n\r", n, XOut, YOut, ZOut);
 }
 
 void writeDataToFile() {
@@ -73,27 +97,27 @@
    
     fprintf(results, "x = ["); //print X array
     
-    for (int i = 0; i < 999; i++) {
-        fprintf(results, "%.2f,", X[i]);
+    for (int i = 0; i < 1999; i++) {
+        fprintf(results, "%.3f,", X[i]);
     }
     
-    fprintf(results, "%.2f]\n\r", X[999]);
+    fprintf(results, "%.3f]\n\r", X[1999]);
     
     fprintf(results, "y = ["); //print Y array
     
-    for (int i = 0; i < 999; i++) {
-        fprintf(results, "%.2f,", Y[i]);
+    for (int i = 0; i < 1999; i++) {
+        fprintf(results, "%.3f,", Y[i]);
     }
     
-    fprintf(results, "%.2f]\n\r", Y[999]);
+    fprintf(results, "%.3f]\n\r", Y[1999]);
     
     fprintf(results, "z = ["); //print Z array
     
-    for (int i = 0; i < 999; i++) {
-        fprintf(results, "%.2f,", Z[i]);
+    for (int i = 0; i < 1999; i++) {
+        fprintf(results, "%.3f,", Z[i]);
     }
     
-    fprintf(results, "%.2f]\n\r", Z[999]);
+    fprintf(results, "%.3f]\n\r", Z[1999]);
      
     fclose(results); // close file
     leds = 0; // turn off LEDs to signify file access has finished