1. #include <SPI.h>
#include <SD.h>
#include <Wire.h>
#define CTRL_REG1 0x20 // data rate, bandwidth, pwr mode, axes enable
#define CTRL_REG2 0x21 // high pass filter cutoff
#define CTRL_REG3 0x22 // interrupts
#define CTRL_REG4 0x23 // update mode, LSB/MSB, full scale, serial mode
#define CTRL_REG5 0x24 // FIFO en, high pass en
const float SC250 = 0.00875;
const float SC500 = 0.0175;
const float SC2000 = 0.070;
int L3G4200D_Address = 105; //I2C address of the L3G4200D
int x;
int y;
int z;
int sum;
int zero;
float angle;
float time;
float SC;
int time0;
float Rth;
float rate;
// 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 = 4;
int sampleDelay = 500;
//number of milliseconds between readings
void setup()
{
// Open serial communications and wait for port to open:
Wire.begin();
Serial.begin(9600);
/
/Everything Below this is for the SD card: ------Don't Change!----
2. while (!Serial)
{
; // wait for serial port to connect.
}
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;
}
Serial.println("card initialized.");
//Make sure the analog-to-digital converter takes its reference voltage from the
AREF pin FOR ACCELEROMETER
analogReference(EXTERNAL);
setupL3G4200D(500); // Configure L3G4200 - 250, 500 or 2000 deg/sec
Rth = 42;
angle = 0.0;
delay(1500);
//wait for the sensor to be ready
sum = 0.0;
for(int i = 0; i < 100; i++)
{
getGyroValues();
sum = sum + z;
}
zero = sum/100;
Serial.println(zero);
Serial.println(SC,4);
time0 = millis();
}
void loop()
3. {
// make a string for assembling the data to log: FOR SD CARD
String dataString = "";
//FOR ACCELEROMETER --Convert to Gravity
//zero_G is the reading we expect from the sensor when it detects
//no acceleration. Subtract this value from the sensor reading to
//get a shifted sensor reading.
float zero_G = 512.0;
//FOR ACCELEROMETER --Convert to Gravity
//scale is the number of units we expect the sensor reading to
//change when the acceleration along an axis changes by 1G.
//Divide the shifted sensor reading by scale to get acceleration in Gs.
float scale = 102.3;
// read three sensors and append to the string:
//Sensor 0:
int sensor0 = analogRead(0);
//simply reads the Analog pin 0 (AccelerometerX)
float Grav0 = (((float)sensor0 - zero_G)/scale);
//Converting Accelermeter reading to Gravity, not neccessary for GYRO
dataString += String(Grav0);
//adds analog pin 0 output onto the Datastring to print later
dataString += "t";
//changed to Tab for easy EXCEL
// 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);
//Sensor 1:
int sensor1 = analogRead(1);
//reads the Analog pin 1 (AccelerometerY)
float Grav1 = (((float)sensor1 - zero_G)/scale);
//Converting Accelermeter reading to Gravity, not neccessary for GYRO
dataString += String(Grav1);
//adds analog pin 1 output onto the Datastring to print later
dataString += "t";
//changed to Tab for easy EXCEL
4. //Sensor 2:
int sensor2 = analogRead(2);
//reads the Analog pin 2 (AccelerometerZ)
float Grav2 = (((float)sensor2 - zero_G)/scale);
//Converting Accelermeter reading to Gravity, not neccessary for GYRO
dataString += String(Grav2);
//adds analog pin 2 output onto the Datastring to print later
dataString += "t"; //changed to Tab for easy EXCEL
//ADD Gyro Code Here::: make it so that it adds the gyro variables to the
datastring so that the next
getGyroValues();
// This will update x, y, and z with new values
time = float(millis() - time0)/1000.0;
if (abs(z - zero) < Rth)
rate = 0.0; //added on float rate
else
rate = SC*float(z - zero); //added on float rate
angle = angle + rate*time; //added on float
dataString += String(rate);
dataString += "t"; //changed to Tab for easy EXCEL
dataString += String(z);
dataString += "t"; //changed to Tab for easy EXCEL
dataString += String(time);
dataString += "t"; //changed to Tab for easy EXCEL
dataString += String(angle);
// if the file is available, write to it: Finally prints the datastring (Don'
modify afther this point)
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
5. // delay before next reading: This helps with accuracy. currently delays for 1/2
a second (Set at top of code)
delay(sampleDelay);
}
void getGyroValues()
{
byte zMSB = readRegister(L3G4200D_Address, 0x2D);
byte zLSB = readRegister(L3G4200D_Address, 0x2C);
z = ((zMSB << 8) | zLSB);
}
int setupL3G4200D(int scale)
{
//From Jim Lindblom of Sparkfun's code
// Enable x, y, z and turn off power down: ENABLE Z-AXIS ONLY
writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001100);
// If you'd like to adjust/use the HPF, you can edit the line below to configure
CTRL_REG2:
writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);
// Configure CTRL_REG3 to generate data ready interrupt on INT2
// No interrupts used on INT1, if you'd like to configure INT1
// or INT2 otherwise, consult the datasheet:
writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);
// CTRL_REG4 controls the full-scale range, among other things:
if(scale == 250)
{
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
SC = SC250;
}
else if(scale == 500)
{
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
SC = SC500;
}
else{
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
SC = SC2000;
}
6. // CTRL_REG5 controls high-pass filtering of outputs, use it
// if you'd like:
writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}
void writeRegister
(int deviceAddress, byte address, byte val)
{
Wire.beginTransmission(deviceAddress);
// start transmission to device
Wire.write(address);
// send register address
wire.write(val);
// send value to write
Wire.endTransmission();
// end transmission
}
int readRegister(int deviceAddress, byte address)
{
int v;
Wire.beginTransmission(deviceAddress);
Wire.write(address);
// register to read
Wire.endTransmission();
Wire.requestFrom(deviceAddress, 1);
// read a byte
v = Wire.read();
return v;
}
7. // CTRL_REG5 controls high-pass filtering of outputs, use it
// if you'd like:
writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}
void writeRegister
(int deviceAddress, byte address, byte val)
{
Wire.beginTransmission(deviceAddress);
// start transmission to device
Wire.write(address);
// send register address
wire.write(val);
// send value to write
Wire.endTransmission();
// end transmission
}
int readRegister(int deviceAddress, byte address)
{
int v;
Wire.beginTransmission(deviceAddress);
Wire.write(address);
// register to read
Wire.endTransmission();
Wire.requestFrom(deviceAddress, 1);
// read a byte
v = Wire.read();
return v;
}