Mathias Lyngklip / Mbed 2 deprecated Accelerometer_SD_logger_test

Dependencies:   MMA8451Q SDFileSystemv2 TSI mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //  #########################################################
00002 //  Includes                                                # 
00003 //  #########################################################
00004 #include "mbed.h"               // Mbed library
00005 #include "SDFileSystem.h"       // SD Libary
00006 #include "MMA8451Q.h"           // Built-in Accelerometer Libary
00007 #include "TSISensor.h"          // Built-in Touch-sensor Libary
00008 
00009 //  #########################################################
00010 //  Define settings                                         # 
00011 //  #########################################################
00012 #define SD_CARD 1
00013 
00014 //  #########################################################
00015 //  Adresses for talking to the I2C sensors                 # 
00016 //  #########################################################
00017 #define MMA8451_I2C_ADDRESS   (0x1d<<1)     // Accelerometer
00018 
00019 
00020 //  #########################################################
00021 //  Constructors                                            #
00022 //  #########################################################
00023 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);            // Built-in Accelerometer
00024 SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd");              // the pinout on the mbed Cool Components workshop board
00025 Serial pc(USBTX, USBRX);                                    // Serial w/ PC through wire
00026 TSISensor tsi; //-------------------------------------------// TouchSensor på Mbed'en 
00027 
00028 
00029 //  #########################################################
00030 //  Global Variables                                        #
00031 //  #########################################################
00032 int fileOpen = 0;
00033 char fileName[23] = "/sd/mydir/LOGGER00.CSV";
00034 int T1 = 0;
00035 int T2 = 0;
00036 
00037 
00038 
00039 
00040 
00041 
00042 int main(){
00043     
00044     
00045 pc.baud(9600);
00046 Timer t;
00047 
00048 
00049 T1 = tsi.readDistance(); 
00050 
00051 // ----------------------------------------------------------
00052 // Setup part - This only runs ONCE
00053 // ----------------------------------------------------------
00054     t.start();
00055     t.reset();
00056     #if SD_CARD
00057     mkdir("/sd/mydir", 0777); // Making the folder in where to put the file
00058     int i = 1;
00059     nameFinder:
00060     fileName[18] = '.';                 // Making the dot in .CSV
00061     fileName[17] = i%10 + '0';          // Numbering the filename
00062     fileName[16] = i/10 + '0';          // to something not already there
00063     i++;
00064     FILE *fp = fopen(fileName, "r" );   // This checks if there is a file called fileName
00065     if(fp != NULL){
00066         printf("Going to namefinder %i \n", i);
00067         goto nameFinder;
00068     }
00069     
00070     if(fp == NULL){    
00071         printf("Wuhu");    
00072         fp = fopen(fileName, "w" ); // This creates a file called fileName
00073         if(fp == NULL){
00074             printf("\nFAILED\n");
00075             error("failed");
00076             }
00077     }
00078     #endif
00079     
00080     
00081     
00082         #if SD_CARD
00083     fprintf(fp, "X,Y,Z,Altitude,Temperature \n");        // Prints a header telling Excel what sort of information is in which place
00084 
00085 
00086     fprintf(fp, "Hej, Hej, BOV \n");
00087     
00088 
00089     #endif
00090     
00091     
00092 // ----------------------------------------------------------
00093 // Loop part - This runs AFTER setup
00094 // ----------------------------------------------------------
00095     while(1){
00096         /*
00097         float XYZ[3];
00098         acc.getAccAllAxis(XYZ);
00099     
00100         for(int i = 0; i < 3; i++){
00101             printf("XYZ[%i]: %f", i, XYZ[i]);
00102         };
00103         */
00104         float x = 0;
00105         float y = 0;
00106         float z = 0;
00107         x = acc.getAccX();   //
00108         y = acc.getAccY();   //-> Getting accelerometer-information
00109         z = acc.getAccZ();   //
00110         fprintf(fp, "%f, %f, %f, %f \n", x, y, z,t.read());
00111         printf("X: %f\t  Y: %f\t  Z: %f \n", x, y, z);
00112         
00113         T2 = tsi.readDistance();
00114         if(T2-T1 > 0.5){
00115             break;         // Makes the loop stop.
00116         }    
00117         
00118         wait(0.01);
00119     }// while
00120 
00121     fclose(fp); // Closes the SD card. Do this after every write
00122 
00123 }// main