Datalogging

I have a code utilizing a magnetometer, GPS, altimeter, and MQ-4 sensor, and I need to be able to log this data onto an SD card when away from the computer. I have very basic knowledge of arduino, and cant understand out to format such a code. Can anyone help?

Here is the code:

#include <Wire.h>
#define address 0x1E
#define Addr 0x50
#include <NMEAGPS.h>
#include <NeoSWSerial.h>

NeoSWSerial gpsSerial(3, 2);
NMEAGPS GPS;
gps_fix fix;

#include <Wire.h>
#include “SparkFunMPL3115A2.h”

MPL3115A2 myPressure;

void setup() {
// put your setup code here, to run once:

//MQ4 setup

Wire.begin();

Serial.begin(9600);
delay(300);

//Altimeter setup

Wire.begin();
Serial.begin(9600);

myPressure.begin();

//Configure the sensor
myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
//myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa

myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
myPressure.enableEventFlags(); // Enable all three pressure and temp event flags

//Magnetometer setup

Serial.begin(9600);
Wire.begin();

//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();

//GPS Setup

Serial.begin(9600);
Serial.println( F(“Run once!”) ); // F macro saves RAM!

gpsSerial.begin(9600);
gpsSerial.println( F("$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,028") ); // RMC & GGA only
gpsSerial.println( F("$PMTK220,1000
1F") ); // 1Hz update rate

}

void loop() {
// put your main code here, to run repeatedly:

waitForFix();
printFix();

//GPS Loop
}
void waitForFix()
{
uint8_t fixes = 0;

do {
if (GPS.available( gpsSerial )) {
fix = GPS.read();
fixes++;
}
} while ((fixes < 2) && !fix.valid.location);

} // waitForFix

void printFix()
{
Serial.print( F("Time: ") );
if (fix.valid.time) {
if (fix.dateTime.hours < 10)
Serial.print( ‘0’ );
Serial.print(fix.dateTime.hours);
Serial.print(’:’);
if (fix.dateTime.hours < 10)
Serial.print( ‘0’ );
Serial.print(fix.dateTime.minutes);
Serial.print(’:’);
if (fix.dateTime.hours < 10)
Serial.print( ‘0’ );
Serial.print(fix.dateTime.seconds);
Serial.print(’.’);
if (fix.dateTime_cs < 10)
Serial.print( ‘0’ );
Serial.print(fix.dateTime_cs);
}
Serial.println();

Serial.print( F("Date: ") );
if (fix.valid.date) {
Serial.print(fix.dateTime.day);
Serial.print(’/’);
Serial.print(fix.dateTime.month);
Serial.print(’/’);
Serial.print( fix.dateTime.full_year() );
}
Serial.println();

Serial.print( F("Fix status: ") );
if (fix.valid.status)
Serial.print( (int) fix.status );
Serial.println();

if (fix.valid.location) {
Serial.print( F("Location: “) );
Serial.print( fix.latitude(), 4 );
Serial.print( F(”, ") );
Serial.println(fix.longitude(), 4);
}

if (fix.valid.speed) {
Serial.print("Speed (knots): ");
Serial.println( fix.speed() );
}

if (fix.valid.heading) {
Serial.print( F("Angle: ") );
Serial.println( fix.heading() );
}

if (fix.valid.altitude) {
Serial.print( F("Altitude: ") );
Serial.println( fix.altitude() );
}

if (fix.valid.satellites) {
Serial.print( F("Satellites: ") );
Serial.println( fix.satellites );
}

//Magnetometer Loop
int x,y,z; //triple axis data

//Tell the HMC5883L where to begin reading data
Wire.beginTransmission(address);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();

//Read data from each axis, 2 registers per axis
Wire.requestFrom(address, 6);
if(6<=Wire.available()){

x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}

//Print out values of each axis
Serial.print("x: “);
Serial.print(x);
Serial.print(” y: “);
Serial.print(y);
Serial.print(” z: ");
Serial.println(z);

delay(250);

//Altimeter Loop

float altitude = myPressure.readAltitudeFt();
Serial.print(" Altitude(ft):");
Serial.print(altitude, 2);

//float pressure = myPressure.readPressure();
//Serial.print(“Pressure¶:”);
//Serial.print(pressure, 2);

//float temperature = myPressure.readTemp();
//Serial.print(" Temp(c):");
//Serial.print(temperature, 2);

float temperature = myPressure.readTempF();
Serial.print(" Temp(f):");
Serial.print(temperature, 2);

Serial.println();

//MQ4 loop

unsigned int data[2];

Wire.beginTransmission(Addr);

Wire.write(0x00);

Wire.endTransmission();

Wire.requestFrom(Addr, 2);

if(Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}

int raw_adc = ((data[0] & 0x0F) * 256) + data[1];
float ppm = (10000.0 / 4096.0) * raw_adc + 200.0;

Serial.print("Methane concentration : ");
Serial.println(ppm);
delay(300);

}

You have Serial.begin and Wire.begin more than once, perhaps because you have yet to work on merging codes. The examples in the SD section of the IDE are all you need, and printing to SD file is not very different from Serial.print.

Check the stickies on how to post code using </> and those excessive blank lines are quite discourteous.

But how would I insert my preexisting code into a datalogging example code? It doesnt make sense to me.

You only ned to merge what is relevant. Computers are stupid, but only need to be told to do something once. http://forum.arduino.cc/index.php/topic,148850.0.html

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :)

jmacris02,

I was in your shoes about a year ago and found it easiest to bit something like this one sensor at a time--I created something similar for my first project and it took a lot of hacking to get things working.

1) get GPS working and at least print the results to the serial monitor, TinyGPS is a good library that has an example test_with_gps_device that may work without any changes--at least it did for me

2) the get your second sensor working

3) the get your SD card working. SdFat library has a datalogger that may help

Once you get a few of these working then start merging the code. I don't have these sensors so i can't test your code.

Hang in there, you'll get it.