Hello all,
Recently, I have been attempting to increase the frequency of cataloguing from an MPU6050, and BMP180, to an SD Card. I have been successful in reducing latency from several readings. However, one problem remains.
My code is inefficient in the fact that every loop in the "void loop()" function, I open, log the data, and close the SD Card. This entire cycle takes 10 ms. I have read that if I open the SD Card once, and close it once, I can drastically decrease the latency of the cycle.
I am unsure as to how the code would work to only open the SD Card once, and close it once as well.
Below is my current code (for testing, not actual usage):
#include <Adafruit_BMP085.h> //Include Adafuit BMP180 Library
#include <Adafruit_MPU6050.h> //Include Adafruit MPU6050 Library
#include <Adafruit_Sensor.h> //Include General Adafruit Sensor Library
#include <SD.h> //Include the SD Card Library
#include <SPI.h> //Include the SPI Communication Library
#include <Wire.h> //Include I2C Communication Lirary
Adafruit_MPU6050 mpu; //Define MPU Word as "mpu"
Adafruit_BMP085 bmp; //Define BMP Word as "bmp"
int CS_Pin = 10; //Define Chip Select Pin for SD Reader as Digital Pin 10 (as per PCB)
int BMP_IND_PIN = 4; //Define LED Indicator Pin for BMP Indicator LED as Digital Pin 4 (as per PCB)
int MPU_IND_PIN = 5; //Define LED Indicator Pin for MPU Indicator LED as Digital Pin 5 (as per PCB)
int SD_IND_PIN = 6; //Define LED Indicator Pin for SD Card Indicator LED as Digital Pin 6 (as per PCB)
int Increment = 0; //Define Increment variable as means for a timer
int j = 0; //Define j as increment for loops for indicating error of SD Card
unsigned long Time;
#define BMP085_ULTRAHIGHRES 3
void setup() {
//DEFINING OUTPUTS
pinMode(BMP_IND_PIN,OUTPUT); //Define BMP_IND_PIN as an OUTPUT - for LED
pinMode(MPU_IND_PIN,OUTPUT); //Define MPU_IND_PIN as an OUTPUT - for LED
pinMode(SD_IND_PIN,OUTPUT); //Define SD_IND_PIN as an OUTPUT - for LED
pinMode(CS_Pin,OUTPUT); //Define CS_Pin as an OUTPUT - for Chip Select Pin, SPI Communication
SPI.begin(); //Begin SPI Communication
Wire.begin(); //Begin I2C Commauniction
Wire.setClock(1000000L);
Serial.begin(9600);
//COMPUTER STARTUP SEQUENCE - CHECKING CONNECTIONS
//CHECKING BMP180 CONNECTIONS
if(bmp.begin(BMP085_ULTRALOWPOWER)){
digitalWrite(BMP_IND_PIN,HIGH); //Will turn on LED if BMP180 is found
delay(200);
Serial.println("Found BMP180");
}
//CHECKING MPU6050 CONNECTIONS
if(mpu.begin()){
digitalWrite(MPU_IND_PIN,HIGH); //Will turn on LED if MPU6050 is found
delay(200);
Serial.println("Found MPU6050");
}
//CHECKING SD CARD CONNECTIONS
if(SD.begin(CS_Pin)){
digitalWrite(SD_IND_PIN,HIGH); //Will turn on LED if SD Card is found
delay(200);
Serial.println("Found SD CARD MODULE");
}
else{
Serial.println("NO SD CARD");
for(j=1;j<=200;j=j+1){
digitalWrite(SD_IND_PIN,HIGH);
delay(100);
digitalWrite(SD_IND_PIN,LOW);
delay(100);
//WILL BLINK LED 3 IF SD CARD IS NOT FOUND
}
}
//FINAL STARTUP SEQUENCE CHECK
if(SD.begin(CS_Pin) and mpu.begin() and bmp.begin(BMP085_ULTRALOWPOWER)){
//STARTUP BLINK LED SEQUENCE
digitalWrite(BMP_IND_PIN,LOW);
digitalWrite(MPU_IND_PIN,LOW);
digitalWrite(SD_IND_PIN,LOW);
delay(200);
digitalWrite(BMP_IND_PIN,HIGH);
digitalWrite(MPU_IND_PIN,HIGH);
digitalWrite(SD_IND_PIN,HIGH);
delay(1000);
digitalWrite(BMP_IND_PIN,LOW);
digitalWrite(MPU_IND_PIN,LOW);
digitalWrite(SD_IND_PIN,LOW);
}//END OF IF STATEMENT
//CONFIGURE SENSOR SETTINGS
mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_260_HZ);
}//END OF VOID LOOP
void loop(){
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
float accX = (((a.acceleration.x)/9.81)-0.05); //Stores Calibrated X Acceleration Value in g
Time = millis();
Serial.print(Time);
Serial.println("Accel X");
float accY = (((a.acceleration.y)/9.81)+0.03); //Stores Calibrated Y Acceleration Value in g
Time = millis();
Serial.print(Time);
Serial.println("Accel Y");
float accZ = (((a.acceleration.z)/9.81)-0.16); //Stores Calibrated Z Acceleration Value in g
Time = millis();
Serial.print(Time);
Serial.println("Accel Z");
float gyroX = (g.gyro.x+0.03); //Stores Calibrated X Angular Rate Value in Rad/s
Time = millis();
Serial.print(Time);
Serial.println("Gyro x");
float gyroY = (g.gyro.y-0.02); //Stores Calibrated Y Angular Rate Value in Rad/s
Time = millis();
Serial.print(Time);
Serial.println("Gyro Y");
float gyroZ = (g.gyro.z-0.02); //Stores Calirbated Z Angular Rate Value in Rad/s
Time = millis();
Serial.print(Time);
Serial.println("Gyro Z");
float Temperature = bmp.readTemperature();
Time = millis();
Serial.print(Time);
Serial.println("Temp");
float Pressure = (bmp.readPressure());
Time = millis();
Serial.print(Time);
Serial.println("Pressure");
File dataFile = SD.open("logs.csv", FILE_WRITE); //Create "File" object, referred to as dataFile. Will also create a file on Micro SD Card named "log.txt" with Write argument
if (dataFile){ //If dataFile could be opened....
dataFile.print(accX); //Print the first data string onto the MicroSD Card
dataFile.print(",");
dataFile.print(accY);
dataFile.print(",");
dataFile.print(accZ);
dataFile.print(",");
dataFile.print(gyroX);
dataFile.print(",");
dataFile.print(gyroY);
dataFile.print(",");
dataFile.print(gyroZ);
dataFile.print(",");
dataFile.print(Temperature);
dataFile.print(",");
dataFile.println(Pressure);
dataFile.close(); //Close the DataFile
digitalWrite(SD_IND_PIN,HIGH);
Time = millis();
Serial.println(Time);
Serial.println("DATA LOGGED");
Serial.println("");
}
else{
digitalWrite(BMP_IND_PIN,HIGH); //INDICATES FAILURE OF OPENING SD CARD
digitalWrite(MPU_IND_PIN,HIGH);
Serial.println("OPENING DATAFILE IS UNSUCCESSFUL");
delay(20000);
}
if(Increment == 500){ //If Micro SD Card uploads have occurred 18 times, turn on the LED
Serial.println("Safe to remove Micro-USB"); //Will indicate on Serial when it is safe to remove the Micro-USB cable from the Teensy LC
dataFile.close(); //Close Micro SD Card, just to be safe
digitalWrite(BMP_IND_PIN,HIGH);
digitalWrite(MPU_IND_PIN,HIGH);
digitalWrite(SD_IND_PIN,HIGH);
delay(20000); //Delay for 20 seconds, give ample time to disconnect Teensy
Serial.println("Unsafe to remove Micro-USB"); //Will indicate that it is unsafe to remove the Micro-USB cable
dataFile.close(); //Close the DataFile
}
else{
Increment = Increment + 1; //Increment
}
digitalWrite(SD_IND_PIN,LOW);
}
Thank you in advance,
Dawn