Skip to content

Instantly share code, notes, and snippets.

@clayrichardson
Created March 10, 2014 02:36
Show Gist options
  • Save clayrichardson/9458572 to your computer and use it in GitHub Desktop.
Save clayrichardson/9458572 to your computer and use it in GitHub Desktop.
#include <SD.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
#include <Adafruit_BMP085_U.h>
#include <Adafruit_L3GD20_U.h>
#include <Adafruit_10DOF.h>
/* Assign a unique ID to the sensors */
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302);
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(18001);
Adafruit_L3GD20_Unified gyro = Adafruit_L3GD20_Unified(20);
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;
int led = 13;
void setup()
{
// Open serial communications and wait for port to open:
//Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
/* Initialise the sensors */
if(!accel.begin())
{
/* There was a problem detecting the ADXL345 ... check your connections */
Serial.println(F("Ooops, no LSM303 detected ... Check your wiring!"));
while(1);
}
if(!mag.begin())
{
/* There was a problem detecting the LSM303 ... check your connections */
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
while(1);
}
if(!bmp.begin())
{
/* There was a problem detecting the BMP085 ... check your connections */
Serial.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!");
while(1);
}
if(!gyro.begin())
{
/* There was a problem detecting the L3GD20 ... check your connections */
Serial.print("Ooops, no L3GD20 detected ... Check your wiring or I2C ADDR!");
while(1);
}
// 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)) {
im_fucked();
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
dataFile.println("=== NEW EVENT ===");
dataFile.close();
}
void loop()
{
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&event);
dataFile.print("{\"Milliseconds\":\""); dataFile.print(millis()); dataFile.print("\",");
dataFile.print("\"Accelerometer X\":\""); dataFile.print(event.acceleration.x); dataFile.print("\",");
dataFile.print("\"Accelerometer Y\":\""); dataFile.print(event.acceleration.y); dataFile.print("\",");
dataFile.print("\"Accelerometer Z\":\""); dataFile.print(event.acceleration.z); dataFile.print("\",");
mag.getEvent(&event);
dataFile.print("\"Magnetic X\":\""); dataFile.print(event.magnetic.x); dataFile.print("\",");
dataFile.print("\"Magnetic Y\":\""); dataFile.print(event.magnetic.y); dataFile.print("\",");
dataFile.print("\"Magnetic Z\":\""); dataFile.print(event.magnetic.z); dataFile.print("\",");
gyro.getEvent(&event);
dataFile.print("\"Gyroscope X\":\""); dataFile.print(event.gyro.x); dataFile.print("\",");
dataFile.print("\"Gyroscope Y\":\""); dataFile.print(event.gyro.y); dataFile.print("\",");
dataFile.print("\"Gyroscope Z\":\""); dataFile.print(event.gyro.z); dataFile.print("\",");
bmp.getEvent(&event);
dataFile.print("\"Pressure\":\""); dataFile.print(event.pressure); dataFile.print("\",");
float temperature;
bmp.getTemperature(&temperature);
dataFile.print("\"Temperature\":\""); dataFile.print(temperature); dataFile.println("\"}");
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
im_fucked();
}
}
void im_fucked() {
while(1) {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a second
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a second
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a second
delay(300);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment