Originally there was an error... but some black magic happened and it's gone. Feel free to move this to wherever approriate.
Read the temperature, pressure, and altitude from a BMP085 barometer - Check
Read the time from the RealTime Clock on a adafruit datalogger shield - Check
Output the data to an SD card and log the data - Check
// Hardware:
// Arduino Duelmilanove (based on the the ATmega328p
// Arduino Datalogging Shield (http://www.adafruit.com/products/1141)/ coin battery included
// BMP085 Barometric Pressure/Temperature/Altitude Sensor (http://www.adafruit.com/products/391)
// SD Card
#include "SD.h"
#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <Adafruit_Sensor.h>
#include "RTClib.h"
Adafruit_BMP085 bmp; // library for the sensor
RTC_DS1307 RTC; // library for the Realtime Clock
const int chipSelect = 10; // for the data logging shield, we use digital pin 10 for the SD cs line
void setup()
{
Serial.begin(9600);
Wire.begin();
RTC.begin();
// -----------------------------SD CARD Initialization -------------------- //
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
else {
Serial.println("card initialized.");
}
//------------------ Initialize sensor and check for errors -------------------- //
bmp.begin();
if(!bmp.begin())
{
while(1);
}
if (! RTC.isrunning()) {
RTC.adjust(DateTime(__DATE__, __TIME__)); // Sets the date and time to your computer clock
}
}
// --------------------------------Main Code ---------------------------------- //
void loop(void)
{
// ------------------ Create or open a text file "ATLAS" --------------- //
// Warning: if the text file already exists it may cause an error. To know if
// the data is writing to the SD card correctly, you should see a flashing red
// light on the arduino every 1 second (or whatever you set the delay too.
File logfile = SD.open("ATLAS.txt", FILE_WRITE); //Keep the name short, long ones may cause issues
// ------------------------- Print the current time --------------------------- //
DateTime now = RTC.now(); // Determine the current time from the RealTime Clock
String timeStamp = readTimestamp(now); // Call the readTimestamp function
logfile.print(timeStamp); //Print time in format DD/MM/YYYY 00:00:00
//Serial.print(timeStamp); // Print on serial monitor (optional)
// ----------------------- Call BMP085 for a new reading ---------------------- //
sensors_event_t event;
bmp.getEvent(&event);
if (event.pressure) // Check if arduino is reading the BMP085 sensor
{
// ----------------------- Atmospheric pressure in hPa ---------------------- //
float pressure = event.pressure;
// ------------------------ Temperature in degrees C ------------------------ //
float temperature;
bmp.getTemperature(&temperature);
// ---- Determine the corresponding altitude given temperature & pressure --- //
float seaLevelPressure = 1015; // Define your standard sea level pressure (in hPa) for better results
float altitude = bmp.pressureToAltitude(seaLevelPressure,event.pressure,temperature);
// Print to SD card
logfile.print(" ");
logfile.print(pressure);
logfile.print(" ");
logfile.print(temperature);
logfile.print(" ");
logfile.print(altitude);
logfile.println();
// Print to serial monitor (optional)
// Serial.print(" ");
// Serial.print(pressure);
// Serial.print(" ");
// Serial.print(temperature);
// Serial.print(" ");
// Serial.print(altitude);
// Serial.println();
}
else // If it is not reading a value properly
{
// Serial.println("Sensor error");
}
logfile.close(); // Close the datafile
// ---- Change this value to how often you want to write the data to the SD card --- //
delay(1000); // Wait 1000 milliseconds (or 1 second)
}
// ------- This function will write the current time into one single string -------- //
String readTimestamp(DateTime thisTime) {
// concatenate a string with all the parts of the date and time:
String result = String(thisTime.day());
result +="/";
result +=String(thisTime.month(),DEC);
result +="/";
result +=String(thisTime.year(),DEC);
result +=" ";
result +=String(thisTime.hour(),DEC);
result +=":";
result +=String(thisTime.minute(),DEC);
result +=":";
result +=String(thisTime.second(),DEC);
return result;
}
Hardware Setup: (Arduino Duelmilanove, BMP085 Pressure Sensor Board, Adafruit Datalogger Shield)
Enjoy!