Assignment 5 for OCE360, timers, tickers, and interrupts.

Dependencies:   4DGL-uLCD-SE MMA8452Q SDFileSystem bouncing_ball mbed

Fork of OCE360_4 by OCE_360

Revision:
12:1b20c6eceae8
Parent:
11:abfb94de3a41
--- a/main.cpp	Thu Nov 16 15:01:30 2017 +0000
+++ b/main.cpp	Thu Nov 16 16:01:37 2017 +0000
@@ -47,19 +47,22 @@
 //Toggle Initialization
 InterruptIn button(p13);    //Interrupt on the button on pin 13
 Timer debounce;             //defines debounce timer for proper switching
+Timer logtimer;
 
 int recorder = 0;
+int state = 0;
 
 void toggle(){
     if(debounce.read_ms()>175){
         led3= !led3;
         recorder = !recorder;
         debounce.reset(); //restart timer after toggle occurs
+        logtimer.start();
+        state = !state;
         }
     }
 
 //Position recorder
-Timer logtimer;
 Ticker logticker;
 
 float voltage_in;
@@ -112,7 +115,16 @@
     }
 
 int main(){
-
+    
+    file = fopen("/sd/ball_data.txt", "w");
+        if ( file == NULL ) {
+            error("ERROR: Could not open file for writing!\n\r");
+            return -1;
+        }
+    
+    fprintf(file, "        Time   X Position   Y Position   X Speed   Y Speed   X Acceleration   Y Acceleration   Temperature\n\r");
+    
+        
     // Initialize uLCD
     uLCD.baudrate(115200);
     uLCD.background_color(BLACK);
@@ -137,8 +149,10 @@
     debounce.start();       //starts debounce timer
     button.rise(&toggle);   //toggles on button press
     
+    logticker.attach(&datarecord,LCD_UPDATE);
+    
     while (1) {  //execute 'forever'
-    
+        /*
         if (recorder == 1){
                 //File Writing
                 //FILE *file;
@@ -158,7 +172,12 @@
                 // Close file
                 fclose(file);
                 }
-        
+        */
+        /*
+        if (recorder == 0){
+            fclose(file);
+            }
+        */
         if (DEBUG_MODE) {
             //If compiled with DEBUG_MODE flag raised, print values to screen.
             uLCD.locate(0,4);
@@ -188,6 +207,7 @@
     }
 }
 
+/*
 void datarecord(){
     float time = 1*logtimer.read();
     float pos1x = 1*ball1.posx;
@@ -205,7 +225,43 @@
     voltage_in = tempin * 3.3;
     degrees_c = (voltage_in - 0.5) * 100.0;
     
-    fprintf(file, "Ball 1: %.3g   %.3g   %.3g   %.3g   %.3g   %.3g   %.3g   %.3g\n\r",time,pos1x,pos1y,spe1x,spe1y,accx,accy,degrees_c);
+    fprintf(file, "Ball 1: %.3g         %.3g             %.3g           %.3g          %.3g             %.3g               %.3g            %.3g\n\r",time,pos1x,pos1y,spe1x,spe1y,accx,accy,degrees_c);
+    
+    fprintf(file, "Ball 2:              %.3g             %.3g           %.3g          %.3g  \n\r",pos2x,pos2y,spe2x,spe2y);
+    }
+*/
+
+void datarecord(){
+    float time = 1*logtimer.read();
+    float pos1x = 1*ball1.posx;
+    float pos1y = 1*ball1.posy;
+    float spe1x = 1*ball1.speedx;
+    float spe1y = 1*ball1.speedy;
+    float accx = 1*accel.readX();
+    float accy = 1*accel.readY();
+    
+    float pos2x = 1*ball2.posx;
+    float pos2y = 1*ball2.posy;
+    float spe2x = 1*ball2.speedx;
+    float spe2y = 1*ball2.speedy;
     
-    fprintf(file, "Ball 2:        %.3g   %.3g   %.3g   %.3g  \n\r",pos2x,pos2y,spe2x,spe2y);
-    }
\ No newline at end of file
+    voltage_in = tempin * 3.3;
+    degrees_c = (voltage_in - 0.5) * 100.0;
+    
+    if(recorder == 1){
+        // Tell the user we need to wait while we collect some data
+        pc.printf("\nCollecting data (Do not remove SD Card!) ...\n\r");
+        fprintf(file, "Ball 1: %.3g  %.3g          %.3g          %.3g   %.3g   %.3g   %.3g   %.3g\n\r",time,pos1x,pos1y,spe1x,spe1y,accx,accy,degrees_c);
+        fprintf(file, "Ball 2:       %.3g          %.3g          %.3g   %.3g  \n\r",pos2x,pos2y,spe2x,spe2y);
+        //logticker.attach(&datarecord,LCD_UPDATE);
+        pc.printf("\nData Recorded Sucessfully! \n\r");
+        }
+    /*
+    if (state == 0){
+        fclose(file);
+        state = 0;
+        }
+    */
+    }
+            
+            
\ No newline at end of file