Hello,
I am trying to create a datalogger of sorts using the BMP180 and ADXL345 pressure sensor and accelerometer breakout boards from Adafruit. Of course, to store large amounts of data, one must use an SD card. I am using an Ethernet SD card shield at the moment.
Running the ReadWrite or DataLogger examples from the SD library work perfectly, however, I cannot get the file to open properly when I try to implement it in my own code.
Right now I am just trying to read the accelerometer data (all th parts pertaining to capturing data from the BMP180 are commented out.
Here is my entire code:
#include <SD.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <Adafruit_ADXL345_U.h>
#include <SPI.h>
/*Sensors*/
//Accelerometer and altimeter on I2C
Adafruit_ADXL345_Unified Accelerometer = Adafruit_ADXL345_Unified(12345);
Adafruit_BMP085_Unified Altimeter = Adafruit_BMP085_Unified(10085);
//Temperature sensor - LM35
#define TempPin A0
//The file that we will write to - SD on SPI
//File Data;
//chip select pin for SPI for the SD card
const int CS = 4;
//Current build of the code - Major.Minor.Revision
const String CodeVersion = "0.0.3";
//Current hardware version - Major.Minor
const String HardVersion = "0.1";
//Variables to store the elapsed time for stopping the recording
unsigned long previousMillis = 0;
long interval = 10000; //ten seconds
void setup()
{
Serial.begin(9600);
Serial.println(F("-------------------------"));
Serial.println(F("Datalogger"));
Serial.print(F("Hardware version: "));Serial.println(HardVersion);
Serial.print(F("Software version: "));Serial.println(CodeVersion);
Serial.println(F("-------------------------"));
Serial.println(F(""));
delay(1000);
//Set up the peripheral devices
SetupPeripherals();
//Output sensor information to verify that they are working properly
DisplaySensorDetails();
Serial.println("");
delay(1000);
}
void loop()
{
Serial.println(F("Beginning logging data..."));
int timesThrough =0;
while (timesThrough<10)
{
Serial.print(F("Logging data: "));Serial.println(timesThrough);
//The string to write to the file
//Read the BMP's information
/*sensors_event_t altEvent;
Altimeter.getEvent(&altEvent);
if (altEvent.pressure)
{
Data.print("Pressure: ");
Data.print(altEvent.pressure);
Data.println(" hPa");
float temperature;
Altimeter.getTemperature(&temperature);
Data.print("Temperature: ");
Data.print(temperature);
Data.println(" C");
float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
Data.print("Altitude: ");
Data.print(Altimeter.pressureToAltitude(seaLevelPressure,
altEvent.pressure,
temperature));
Serial.println(" m");
Serial.println("");
}
else
{
Data.println("Sensor error");
}*/
//Read and log the accelerometer's information
sensors_event_t accelEvent;
Accelerometer.getEvent(&accelEvent);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
String dataString = "";
if (dataFile)
{
char * s;
dataString += ("X: "); dataString += dtostrf(accelEvent.acceleration.x,2,2,s); dataString +=(" ");
dataString +=("Y: "); dataString +=dtostrf(accelEvent.acceleration.y,2,2,s); dataString +=(" ");
dataString +=("Z: "); dataString +=dtostrf(accelEvent.acceleration.z,2,2,s); dataString +=(" ");dataString +=("m/s^2 ");
dataFile.println(dataString);
/*dataFile.print(("X: ")); dataFile.print(accelEvent.acceleration.x); dataFile.print((" "));
dataFile.print(("Y: ")); dataFile.print(accelEvent.acceleration.y); dataFile.print((" "));
dataFile.print(("Z: ")); dataFile.print(accelEvent.acceleration.z); dataFile.print((" "));dataFile.println(("m/s^2 "));
dataFile.close();*/
}else
{
Serial.println("Error opening file");
}
Serial.print(F("X: ")); Serial.print(accelEvent.acceleration.x); Serial.print(F(" "));
Serial.print(F("Y: ")); Serial.print(accelEvent.acceleration.y); Serial.print(F(" "));
Serial.print(F("Z: ")); Serial.print(accelEvent.acceleration.z); Serial.print(F(" "));Serial.println(F("m/s^2 "));
delay(1000);
timesThrough++;
}
Serial.println(F("Finished logging data."));
delay(1000);
exit(0);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
////// OTHER METHODS ////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//Display details about teh sensors
void DisplaySensorDetails()
{
sensor_t accelSensor;
Accelerometer.getSensor(&accelSensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(accelSensor.name);
Serial.print ("Driver Ver: "); Serial.println(accelSensor.version);
Serial.print ("Unique ID: "); Serial.println(accelSensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(accelSensor.max_value); Serial.println(" m/s^2");
Serial.print ("Min Value: "); Serial.print(accelSensor.min_value); Serial.println(" m/s^2");
Serial.print ("Resolution: "); Serial.print(accelSensor.resolution); Serial.println(" m/s^2");
Serial.print ("Data Rate: "); Serial.print(GetAccelDataRate()); Serial.println(" Hz");
Serial.print ("Data Range: +/-"); Serial.print(GetAccelDataRange()); Serial.println("g");
Serial.println("------------------------------------");
Serial.println("");
sensor_t bmpSensor;
Altimeter.getSensor(&bmpSensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(bmpSensor.name);
Serial.print ("Driver Ver: "); Serial.println(bmpSensor.version);
Serial.print ("Unique ID: "); Serial.println(bmpSensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(bmpSensor.max_value); Serial.println(" hPa");
Serial.print ("Min Value: "); Serial.print(bmpSensor.min_value); Serial.println(" hPa");
Serial.print ("Resolution: "); Serial.print(bmpSensor.resolution); Serial.println(" hPa");
Serial.println("------------------------------------");
Serial.println("");
}
//Set up the sensors and teh SD card
void SetupPeripherals()
{
//Initialize SD card
Serial.println(F("Initializing data storage..."));
pinMode(CS, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(10,HIGH);
if(!SD.begin(CS))
{
Serial.println(F("Problem initializing SD card - check that a card is inserted and that the wiring is correct!"));
while(1);
}
Serial.println(F("MicroSD storage initialized!"));
//Set up the sensors
Serial.println("Initializing sensors...");
//Altimeter
if(!Altimeter.begin())
{
Serial.println("Problem initializing BMP180 Pressure Sensor - Check wiring or I2C Address!");
while(1);
}
Serial.println("BMP180 initialized!");
//Accelerometer
if(!Accelerometer.begin())
{
Serial.println("Problem initializing ADXL345 Accelerometer - Check wiring or I2C address!");
while(1);
}
//Set accelerometer range to +/- 16g - should be sufficient for these purposes
Accelerometer.setRange(ADXL345_RANGE_16_G);
Accelerometer.setDataRate(ADXL345_DATARATE_3200_HZ);
Serial.println("ADXL345 initialized!");
}
//returns the data rate of the accelerometer
int GetAccelDataRate()
{
switch(Accelerometer.getDataRate())
{
case ADXL345_DATARATE_3200_HZ:
return 3200;
case ADXL345_DATARATE_1600_HZ:
return 1600;
case ADXL345_DATARATE_800_HZ:
return 800;
case ADXL345_DATARATE_400_HZ:
return 400;
case ADXL345_DATARATE_200_HZ:
return 200;
case ADXL345_DATARATE_100_HZ:
return 100;
case ADXL345_DATARATE_50_HZ:
return 50;
case ADXL345_DATARATE_25_HZ:
return 25;
case ADXL345_DATARATE_12_5_HZ:
return 12.5;
case ADXL345_DATARATE_6_25HZ:
return 6.25;
case ADXL345_DATARATE_3_13_HZ:
return 3.13;
case ADXL345_DATARATE_1_56_HZ:
return 1.56;
case ADXL345_DATARATE_0_78_HZ:
return 0.78;
case ADXL345_DATARATE_0_39_HZ:
return 0.39;
case ADXL345_DATARATE_0_20_HZ:
return 0.20;
case ADXL345_DATARATE_0_10_HZ:
return 0.10;
default:
return 0;
}
}
//Returns the range of the accelerometer
int GetAccelDataRange()
{
switch(Accelerometer.getRange())
{
case ADXL345_RANGE_16_G:
return 16 ;
case ADXL345_RANGE_8_G:
return 8;
case ADXL345_RANGE_4_G:
return 4;
case ADXL345_RANGE_2_G:
return 2;
default:
return 0;
}
}
The file is created properly, but nothing is written to it. I appreciate any insight this wonderful forum can give!
Thank you!