SlideShare a Scribd company logo
1 of 7
#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!----
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()
{
// 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
//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);
// 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;
}
// 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;
}
// 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;
}

More Related Content

What's hot

555 timer lab projects
555 timer lab projects555 timer lab projects
555 timer lab projects
Bien Morfe
 
Codigo fuente
Codigo fuenteCodigo fuente
Codigo fuente
BlackD10
 
(381877808) 102054282 5-listing-program
(381877808) 102054282 5-listing-program(381877808) 102054282 5-listing-program
(381877808) 102054282 5-listing-program
Dimz I
 
Experiment write-vhdl-code-for-realize-all-logic-gates
Experiment write-vhdl-code-for-realize-all-logic-gatesExperiment write-vhdl-code-for-realize-all-logic-gates
Experiment write-vhdl-code-for-realize-all-logic-gates
Ricardo Castro
 
Data Acquisition
Data AcquisitionData Acquisition
Data Acquisition
azhar557
 

What's hot (20)

selected input/output - sensors and actuators
selected input/output - sensors and actuatorsselected input/output - sensors and actuators
selected input/output - sensors and actuators
 
Weapons grade React by Ken Wheeler
Weapons grade React by Ken Wheeler Weapons grade React by Ken Wheeler
Weapons grade React by Ken Wheeler
 
LINUX RS232程式設計
LINUX RS232程式設計LINUX RS232程式設計
LINUX RS232程式設計
 
555 timer lab projects
555 timer lab projects555 timer lab projects
555 timer lab projects
 
Lathe Spindle Sensor
Lathe Spindle SensorLathe Spindle Sensor
Lathe Spindle Sensor
 
Raspberry Pi I/O控制與感測器讀取
Raspberry Pi I/O控制與感測器讀取Raspberry Pi I/O控制與感測器讀取
Raspberry Pi I/O控制與感測器讀取
 
Embedded JavaScript
Embedded JavaScriptEmbedded JavaScript
Embedded JavaScript
 
Codigo fuente
Codigo fuenteCodigo fuente
Codigo fuente
 
REPORT
REPORTREPORT
REPORT
 
32 bit ALU Chip Design using IBM 130nm process technology
32 bit ALU Chip Design using IBM 130nm process technology32 bit ALU Chip Design using IBM 130nm process technology
32 bit ALU Chip Design using IBM 130nm process technology
 
Micro Assignment 2
Micro Assignment 2Micro Assignment 2
Micro Assignment 2
 
Direct analog
Direct analogDirect analog
Direct analog
 
Pwm wave
Pwm wave Pwm wave
Pwm wave
 
Physical prototyping lab1-input_output (2)
Physical prototyping lab1-input_output (2)Physical prototyping lab1-input_output (2)
Physical prototyping lab1-input_output (2)
 
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
 
(381877808) 102054282 5-listing-program
(381877808) 102054282 5-listing-program(381877808) 102054282 5-listing-program
(381877808) 102054282 5-listing-program
 
Mitsubachi Arduino code
Mitsubachi Arduino codeMitsubachi Arduino code
Mitsubachi Arduino code
 
Experiment write-vhdl-code-for-realize-all-logic-gates
Experiment write-vhdl-code-for-realize-all-logic-gatesExperiment write-vhdl-code-for-realize-all-logic-gates
Experiment write-vhdl-code-for-realize-all-logic-gates
 
Data Acquisition
Data AcquisitionData Acquisition
Data Acquisition
 
Standard cells library design
Standard cells library designStandard cells library design
Standard cells library design
 

Viewers also liked

Best revelation by mueller el liberal
Best revelation by mueller el liberalBest revelation by mueller el liberal
Best revelation by mueller el liberal
asael gomez
 
certificat d'accomplissement HP LIFE- Votre Public Cible
certificat d'accomplissement HP LIFE- Votre Public Ciblecertificat d'accomplissement HP LIFE- Votre Public Cible
certificat d'accomplissement HP LIFE- Votre Public Cible
Ingénieur Chimiste
 
Présentation Agence RGC
Présentation Agence RGCPrésentation Agence RGC
Présentation Agence RGC
rgcdev
 
Ciepłownictwo systemowe integralnym elementem gospodarki niskoemisyjnej w agl...
Ciepłownictwo systemowe integralnym elementem gospodarki niskoemisyjnej w agl...Ciepłownictwo systemowe integralnym elementem gospodarki niskoemisyjnej w agl...
Ciepłownictwo systemowe integralnym elementem gospodarki niskoemisyjnej w agl...
kkotlarczuk
 
Presentació pp
Presentació ppPresentació pp
Presentació pp
irenecp3
 
Glyphosate resistance trait into soybean Cuban varieties: agronomical assessm...
Glyphosate resistance trait into soybean Cuban varieties: agronomical assessm...Glyphosate resistance trait into soybean Cuban varieties: agronomical assessm...
Glyphosate resistance trait into soybean Cuban varieties: agronomical assessm...
Innspub Net
 

Viewers also liked (20)

Presentación 1 berlin
Presentación 1 berlinPresentación 1 berlin
Presentación 1 berlin
 
BELLE VIE - - (21) 3936-3885 - wwwRBIMOBILIARIA.com
BELLE VIE - - (21) 3936-3885 - wwwRBIMOBILIARIA.comBELLE VIE - - (21) 3936-3885 - wwwRBIMOBILIARIA.com
BELLE VIE - - (21) 3936-3885 - wwwRBIMOBILIARIA.com
 
Best revelation by mueller el liberal
Best revelation by mueller el liberalBest revelation by mueller el liberal
Best revelation by mueller el liberal
 
Themenfelder ComTeam-Drehzahl
Themenfelder ComTeam-DrehzahlThemenfelder ComTeam-Drehzahl
Themenfelder ComTeam-Drehzahl
 
Krt
KrtKrt
Krt
 
Efruzhu anti̇cancer drug hu north cyprus 32
Efruzhu  anti̇cancer  drug  hu  north  cyprus  32Efruzhu  anti̇cancer  drug  hu  north  cyprus  32
Efruzhu anti̇cancer drug hu north cyprus 32
 
certificat d'accomplissement HP LIFE- Votre Public Cible
certificat d'accomplissement HP LIFE- Votre Public Ciblecertificat d'accomplissement HP LIFE- Votre Public Cible
certificat d'accomplissement HP LIFE- Votre Public Cible
 
Présentation Agence RGC
Présentation Agence RGCPrésentation Agence RGC
Présentation Agence RGC
 
Printing resume
Printing resumePrinting resume
Printing resume
 
Ciepłownictwo systemowe integralnym elementem gospodarki niskoemisyjnej w agl...
Ciepłownictwo systemowe integralnym elementem gospodarki niskoemisyjnej w agl...Ciepłownictwo systemowe integralnym elementem gospodarki niskoemisyjnej w agl...
Ciepłownictwo systemowe integralnym elementem gospodarki niskoemisyjnej w agl...
 
Presentació pp
Presentació ppPresentació pp
Presentació pp
 
Les pages Google +
Les pages Google +Les pages Google +
Les pages Google +
 
Glyphosate resistance trait into soybean Cuban varieties: agronomical assessm...
Glyphosate resistance trait into soybean Cuban varieties: agronomical assessm...Glyphosate resistance trait into soybean Cuban varieties: agronomical assessm...
Glyphosate resistance trait into soybean Cuban varieties: agronomical assessm...
 
Benchlearning Präsentation Internes Community Management (blp15)
Benchlearning Präsentation Internes Community Management (blp15)Benchlearning Präsentation Internes Community Management (blp15)
Benchlearning Präsentation Internes Community Management (blp15)
 
7 FACTORS FOR SUCCESS - how to achieve the digital transformation within the ...
7 FACTORS FOR SUCCESS - how to achieve the digital transformation within the ...7 FACTORS FOR SUCCESS - how to achieve the digital transformation within the ...
7 FACTORS FOR SUCCESS - how to achieve the digital transformation within the ...
 
16 profils de consommateur e-commerce
16 profils de consommateur e-commerce16 profils de consommateur e-commerce
16 profils de consommateur e-commerce
 
Clinical Visual Electrophysiology
 Clinical  Visual  Electrophysiology  Clinical  Visual  Electrophysiology
Clinical Visual Electrophysiology
 
Stuxnet
StuxnetStuxnet
Stuxnet
 
Enterprise 2.0 bei der Deutschen Telekom
Enterprise 2.0 bei der Deutschen TelekomEnterprise 2.0 bei der Deutschen Telekom
Enterprise 2.0 bei der Deutschen Telekom
 
Etude E-commerce 2020 Malinea
Etude E-commerce 2020 MalineaEtude E-commerce 2020 Malinea
Etude E-commerce 2020 Malinea
 

Similar to FINISHED_CODE

What will be quantization step size in numbers and in voltage for th.pdf
What will be quantization step size in numbers and in voltage for th.pdfWhat will be quantization step size in numbers and in voltage for th.pdf
What will be quantization step size in numbers and in voltage for th.pdf
SIGMATAX1
 
GENESIS BOARD MINI SUMO ROBOT PROGRAMFOR 3 OPPONENT SENSOR, .pdf
 GENESIS BOARD MINI SUMO ROBOT PROGRAMFOR 3 OPPONENT SENSOR, .pdf GENESIS BOARD MINI SUMO ROBOT PROGRAMFOR 3 OPPONENT SENSOR, .pdf
GENESIS BOARD MINI SUMO ROBOT PROGRAMFOR 3 OPPONENT SENSOR, .pdf
alltiusind
 
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdfHow do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
footstatus
 
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
josnihmurni2907
 

Similar to FINISHED_CODE (20)

What will be quantization step size in numbers and in voltage for th.pdf
What will be quantization step size in numbers and in voltage for th.pdfWhat will be quantization step size in numbers and in voltage for th.pdf
What will be quantization step size in numbers and in voltage for th.pdf
 
GENESIS BOARD MINI SUMO ROBOT PROGRAMFOR 3 OPPONENT SENSOR, .pdf
 GENESIS BOARD MINI SUMO ROBOT PROGRAMFOR 3 OPPONENT SENSOR, .pdf GENESIS BOARD MINI SUMO ROBOT PROGRAMFOR 3 OPPONENT SENSOR, .pdf
GENESIS BOARD MINI SUMO ROBOT PROGRAMFOR 3 OPPONENT SENSOR, .pdf
 
Gaztea Tech Robotica 2016
Gaztea Tech Robotica 2016Gaztea Tech Robotica 2016
Gaztea Tech Robotica 2016
 
EEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdfEEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdf
 
The IoT Academy IoT Training Arduino Part 3 programming
The IoT Academy IoT Training Arduino Part 3 programmingThe IoT Academy IoT Training Arduino Part 3 programming
The IoT Academy IoT Training Arduino Part 3 programming
 
Mikroc gps
Mikroc gpsMikroc gps
Mikroc gps
 
Lampiran 1.programdocx
Lampiran 1.programdocxLampiran 1.programdocx
Lampiran 1.programdocx
 
Temperature Control Fan Using 8051 Microcontroller
Temperature Control Fan Using 8051 MicrocontrollerTemperature Control Fan Using 8051 Microcontroller
Temperature Control Fan Using 8051 Microcontroller
 
2.1 ### uVision Project, (C) Keil Software .docx
2.1   ### uVision Project, (C) Keil Software    .docx2.1   ### uVision Project, (C) Keil Software    .docx
2.1 ### uVision Project, (C) Keil Software .docx
 
Product catlog
Product catlogProduct catlog
Product catlog
 
CC2500 Wireless Trans-receiver Module
CC2500 Wireless Trans-receiver ModuleCC2500 Wireless Trans-receiver Module
CC2500 Wireless Trans-receiver Module
 
LCD_Example.pptx
LCD_Example.pptxLCD_Example.pptx
LCD_Example.pptx
 
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdfHow do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
 
PIC and LCD
PIC and LCDPIC and LCD
PIC and LCD
 
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, InternalsLAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
 
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
 
codings related to avr micro controller
codings related to avr micro controllercodings related to avr micro controller
codings related to avr micro controller
 
MaxBotix Code Examples
MaxBotix Code ExamplesMaxBotix Code Examples
MaxBotix Code Examples
 
Hardware interfacing basics using AVR
Hardware interfacing basics using AVRHardware interfacing basics using AVR
Hardware interfacing basics using AVR
 
An Example MIPS
An Example  MIPSAn Example  MIPS
An Example MIPS
 

FINISHED_CODE

  • 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; }