Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MPU6050.h>
- #include <I2Cdev.h>
- #include <SPI.h>
- #include <SD.h>
- #define PIN_LED_RUNNING 4
- #define PIN_LED_EMPTY 5
- #define PIN_LED_ERROR 6
- #define PIN_SWITCH_STARTSTOP 21
- #define PIN_SWITCH_RESET 20
- #define PIN_SDCARD_CS 8
- #define SDCARD_FILEPATH "data.csv"
- #define SERIAL_BAUDRATE 9600
- #define RECORD_INTERVAL 50
- #define RECORD_NUM_TO_STORE 10
- int16_t ax, ay, az;
- MPU6050 accel;
- File dataFile;
- bool dataStarted;
- unsigned long timeLastMeasurement;
- int countUnflushed;
- void setup() {
- //Set up LED pins
- pinMode(PIN_LED_RUNNING, OUTPUT);
- pinMode(PIN_LED_EMPTY, OUTPUT);
- pinMode(PIN_LED_ERROR, OUTPUT);
- //Set up switch pins
- pinMode(PIN_SWITCH_STARTSTOP, INPUT);
- pinMode(PIN_SWITCH_RESET, INPUT);
- //Set up serial
- Serial.begin(SERIAL_BAUDRATE);
- //Set up MPU6050
- Serial.print("Setting up MPU6050 ... ");
- Wire.begin();
- accel.initialize();
- if (accel.testConnection())
- {
- Serial.println("MPU6050 Initilialised");
- }
- else
- {
- Serial.println("Error: MPU6050 could not be initalised");
- digitalWrite(PIN_LED_ERROR, HIGH);
- stopApp();
- }
- accel.setFullScaleAccelRange(1);
- //Set up SD card
- Serial.print("Setting up SD Card ... ");
- if (SD.begin(PIN_SDCARD_CS))
- {
- Serial.println("SD Card Initalised");
- }
- else
- {
- Serial.println("Error: SD Card failed or not present");
- digitalWrite(PIN_LED_ERROR, HIGH);
- stopApp();
- }
- Serial.print("Setting up file ... ");
- if(SD.exists(SDCARD_FILEPATH))
- {
- Serial.print("Data already on card ... ");
- }
- updateFreePin();
- dataFile = SD.open(SDCARD_FILEPATH, FILE_WRITE);
- if(dataFile)
- {
- Serial.println("SD Card File is open");
- }
- else
- {
- Serial.println("Error: Cannot open the SD Card File");
- digitalWrite(PIN_LED_ERROR, HIGH);
- stopApp();
- }
- dataStarted = false;
- timeLastMeasurement = millis();
- countUnflushed = 0;
- }
- void updateFreePin()
- {
- digitalWrite(PIN_LED_EMPTY, SD.exists(SDCARD_FILEPATH));
- digitalWrite(PIN_LED_RUNNING, dataStarted);
- }
- void stopApp()
- {
- while(1);
- }
- void loop() {
- if(digitalRead(PIN_SWITCH_STARTSTOP))
- {
- while(digitalRead(PIN_SWITCH_STARTSTOP));
- dataStarted = !dataStarted;
- updateFreePin();
- }
- if(digitalRead(PIN_SWITCH_RESET) && SD.exists(SDCARD_FILEPATH))
- {
- while(digitalRead(PIN_SWITCH_RESET));
- dataStarted = false;
- dataFile.close();
- SD.remove(SDCARD_FILEPATH);
- updateFreePin();
- dataFile = SD.open(SDCARD_FILEPATH, FILE_WRITE);
- }
- if((millis() - timeLastMeasurement > RECORD_INTERVAL) && dataStarted)
- {
- dataFile.print(String(millis(), DEC));
- dataFile.print(",");
- dataFile.print(String(accel.getAccelerationX(), DEC));
- dataFile.print(",");
- dataFile.print(String(accel.getAccelerationY(), DEC));
- dataFile.print(",");
- dataFile.println(String(accel.getAccelerationZ(), DEC));
- countUnflushed++;
- if(countUnflushed == RECORD_NUM_TO_STORE)
- {
- dataFile.flush();
- countUnflushed = 0;
- }
- }
- }
Add Comment
Please, Sign In to add comment