mattysmith22

Arduino Acceleration Datalogger

Jan 9th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <MPU6050.h>
  2. #include <I2Cdev.h>
  3. #include <SPI.h>
  4. #include <SD.h>
  5.  
  6. #define PIN_LED_RUNNING 4
  7. #define PIN_LED_EMPTY 5
  8. #define PIN_LED_ERROR 6
  9.  
  10. #define PIN_SWITCH_STARTSTOP 21
  11. #define PIN_SWITCH_RESET 20
  12.  
  13. #define PIN_SDCARD_CS 8
  14.  
  15. #define SDCARD_FILEPATH "data.csv"
  16.  
  17. #define SERIAL_BAUDRATE 9600
  18.  
  19. #define RECORD_INTERVAL 50
  20. #define RECORD_NUM_TO_STORE 10
  21.  
  22. int16_t ax, ay, az;
  23.  
  24. MPU6050 accel;
  25.  
  26. File dataFile;
  27.  
  28. bool dataStarted;
  29.  
  30. unsigned long timeLastMeasurement;
  31. int countUnflushed;
  32.  
  33. void setup() {
  34.   //Set up LED pins
  35.   pinMode(PIN_LED_RUNNING, OUTPUT);
  36.   pinMode(PIN_LED_EMPTY, OUTPUT);
  37.   pinMode(PIN_LED_ERROR, OUTPUT);
  38.  
  39.   //Set up switch pins
  40.   pinMode(PIN_SWITCH_STARTSTOP, INPUT);
  41.   pinMode(PIN_SWITCH_RESET, INPUT);
  42.  
  43.   //Set up serial
  44.   Serial.begin(SERIAL_BAUDRATE);
  45.  
  46.   //Set up MPU6050
  47.   Serial.print("Setting up MPU6050 ... ");
  48.   Wire.begin();
  49.   accel.initialize();
  50.   if (accel.testConnection())
  51.   {
  52.     Serial.println("MPU6050 Initilialised");
  53.   }
  54.   else
  55.   {
  56.     Serial.println("Error: MPU6050 could not be initalised");
  57.     digitalWrite(PIN_LED_ERROR, HIGH);
  58.     stopApp();
  59.   }
  60.   accel.setFullScaleAccelRange(1);
  61.  
  62.   //Set up SD card
  63.   Serial.print("Setting up SD Card ... ");
  64.  
  65.   if (SD.begin(PIN_SDCARD_CS))
  66.   {
  67.     Serial.println("SD Card Initalised");
  68.   }
  69.   else
  70.   {
  71.     Serial.println("Error: SD Card failed or not present");
  72.     digitalWrite(PIN_LED_ERROR, HIGH);
  73.     stopApp();
  74.   }
  75.  
  76.   Serial.print("Setting up file ... ");
  77.  
  78.   if(SD.exists(SDCARD_FILEPATH))
  79.   {
  80.     Serial.print("Data already on card ... ");
  81.   }
  82.  
  83.   updateFreePin();
  84.  
  85.   dataFile = SD.open(SDCARD_FILEPATH, FILE_WRITE);
  86.  
  87.   if(dataFile)
  88.   {
  89.     Serial.println("SD Card File is open");
  90.   }
  91.   else
  92.   {
  93.     Serial.println("Error: Cannot open the SD Card File");
  94.     digitalWrite(PIN_LED_ERROR, HIGH);
  95.     stopApp();
  96.   }
  97.  
  98.   dataStarted = false;
  99.  
  100.   timeLastMeasurement = millis();
  101.   countUnflushed = 0;
  102. }
  103.  
  104. void updateFreePin()
  105. {
  106.   digitalWrite(PIN_LED_EMPTY, SD.exists(SDCARD_FILEPATH));
  107.   digitalWrite(PIN_LED_RUNNING, dataStarted);
  108. }
  109.  
  110. void stopApp()
  111. {
  112.   while(1);
  113. }
  114.  
  115. void loop() {
  116.   if(digitalRead(PIN_SWITCH_STARTSTOP))
  117.   {
  118.     while(digitalRead(PIN_SWITCH_STARTSTOP));
  119.    
  120.     dataStarted = !dataStarted;
  121.     updateFreePin();
  122.    
  123.   }
  124.  
  125.   if(digitalRead(PIN_SWITCH_RESET) && SD.exists(SDCARD_FILEPATH))
  126.   {
  127.     while(digitalRead(PIN_SWITCH_RESET));
  128.    
  129.     dataStarted = false;
  130.  
  131.     dataFile.close();
  132.     SD.remove(SDCARD_FILEPATH);
  133.     updateFreePin();
  134.     dataFile = SD.open(SDCARD_FILEPATH, FILE_WRITE);
  135.   }
  136.  
  137.   if((millis() - timeLastMeasurement > RECORD_INTERVAL) && dataStarted)
  138.   {
  139.     dataFile.print(String(millis(), DEC));
  140.     dataFile.print(",");
  141.     dataFile.print(String(accel.getAccelerationX(), DEC));
  142.     dataFile.print(",");
  143.     dataFile.print(String(accel.getAccelerationY(), DEC));
  144.     dataFile.print(",");
  145.     dataFile.println(String(accel.getAccelerationZ(), DEC));
  146.     countUnflushed++;
  147.  
  148.     if(countUnflushed == RECORD_NUM_TO_STORE)
  149.     {
  150.       dataFile.flush();
  151.       countUnflushed = 0;
  152.     }
  153.   }
  154. }
Add Comment
Please, Sign In to add comment