I understand that I am probably doing things inefficient because I am still relatively new to this. I am using an Arduino Mega 2560, an MPL3114A2 and the Adafruit SD card breakout board. I want to log the data as fast as I possibly can, and this is recording data about every 2 seconds, extremely slow considering I have no delay, at least in my opinion. I have no idea what it could be! Is it just how long it takes to receive data from the sensor or write to the SD card? Please help!
#include <SD.h> // So the SD card can be accessed
#include <Wire.h> // So we can communicate over I2C
#include <Adafruit_MPL3115A2.h> // Library for accessing the MPL3115A2 altimeter/barometer/temperature sensor
Adafruit_MPL3115A2 altimeterBoard = Adafruit_MPL3115A2(); // Initializes new altimeter object
const int chipSelect = 53; // Chip select pin (default)
const int chipDetect = 22; // Chip detect pin
String fileName = "log.txt"; // Log file name
int cardInLast = 0; // Variable that stores if the card was in last time the program checked
int altLast = 0; // Variable that stores if the altimeter board was registered last or if it lost connection
int indicatorState = 0;
float altitude = 0.0f; // Stores currect altitude
float pressure = 0.0f; // Stores current pressure
float temperature = 0.0f; // Stores current temperature
void setup() {
Serial.begin(9600); // 9600 baud rate!!!
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(13, OUTPUT); // Init for indicator LED
pinMode(chipDetect, INPUT_PULLUP); // Init for chip detect pin, internal pullup resistor enabled
//Initializes SD card and returns an error if inititalization fails
Serial.println("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card initialization failed, or card not present");
return; // Exits setup if the card fails
}
Serial.println("card initialized.");
writeToSD(fileName, "Program Started"); // Writes a message in the log file on the SD card that at this point it starts a new output session
cardInLast = 1; // Stores that the card was in during first initialization
}
void loop() {
if (!altimeterBoard.begin()) {
Serial.println("Couldnt find MPL3115A2 sensor");
altLast = 0;
}
else
{
altLast = 1;
altitude = altimeterBoard.getAltitude(); // Reads altitude in meters
pressure = altimeterBoard.getPressure(); // Reads pressure in pascals
temperature = altimeterBoard.getTemperature(); // Reads temperature in degrees celsius
}
if (digitalRead(chipDetect) == 1 && altLast == 1){ // Checks if there is a SD card insterted into the datalogger and all the sensors are online
if (cardInLast == 0){ // Checks if the card was just put in
Serial.println("Initializing SD card...");
if (!SD.begin(chipSelect)) { // Re-inititalizes the SD card
Serial.println("Card initialization failed, or card not present");
}
else
{
Serial.println("card initialized.");
}
writeToSD(fileName, "Program Restarted"); // Writes a message in the log file on the SD card that at this point it starts a new output session
}
cardInLast = 1; // Stores that the card is in for next loop
digitalWrite(13, LOW); // Sets the LED to low to indicate no attention is needed
String dataString = String(millis() / 1000) + ", " + String(altitude) + ", " + String(pressure) + ", " + String(temperature);
writeToSD(fileName, dataString); // Writes the data to the SD card
}
else
{
cardInLast = 0; // If the card was not in this time around, store that
digitalWrite(13, HIGH); // Set the indicator LED to high to indicate the SD card is damaged/removed/corrupt
}
}
int writeToSD(String fileName, String data) { // Function that writes to the SD card
File dataFile = SD.open(fileName, FILE_WRITE); // Creates a temporary file variable
if (dataFile) { // If the file has been opened successfully
dataFile.println(data); // Writes the data
dataFile.close(); // Flushes the buffer and closes file
}
else {
return 1; // If the file could not be opened, return an error code
}
}