I'm having a bit of trouble with my rocket sensor I have a code and it works somewhat
but I cannot get the sd card "open log" to read when power is connected which is tough because I only have a day to complete and test the code any other time I'll just give up but a grade is involved and I really don't want to let the team down in short this is an Arduino Forum Emergency! Please help
here's the code
#include <Wire.h>
#include <SD.h>
// 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;
//i2c write
void i2c_write(int address, byte reg, byte data) {
// Send output register address
Wire.beginTransmission(address);
Wire.send(reg);
// Connect to device and send byte
Wire.send(data); // low byte
Wire.endTransmission();
}
//i2c read
void i2c_read(int address, byte reg, int count, byte* data) {
int i = 0;
// Send input register address
Wire.beginTransmission(address);
Wire.send(reg);
Wire.endTransmission();
// Connect to device and request bytes
Wire.beginTransmission(address);
Wire.requestFrom(address,count);
while(Wire.available()) // slave may send less than requested
{
char c = Wire.receive(); // receive a byte as character
data[i] = c;
i++;
}
Wire.endTransmission();
}
//Accelerometer
#define ADXL345_ADDRESS (0xA6 >> 1)
//There are 6 data registers, they are sequential starting
//with the LSB of X. We'll read all 6 in a burst and won't
//address them individually
#define ADXL345_REGISTER_XLSB (0x32)
//Need to set power control bit to wake up the adxl345
#define ADXL_REGISTER_PWRCTL (0x2D)
#define ADXL_PWRCTL_MEASURE (1 << 3)
void init_adxl345() {
byte data = 0;
i2c_write(ADXL345_ADDRESS, ADXL_REGISTER_PWRCTL, ADXL_PWRCTL_MEASURE);
//Check to see if it worked!
i2c_read(ADXL345_ADDRESS, ADXL_REGISTER_PWRCTL, 1, &data);
Serial.println((unsigned int)data);
}
int accelerometer_data[3];
void read_adxl345() {
byte bytes[6];
memset(bytes,0,6);
//read 6 bytes from the ADXL345
i2c_read(ADXL345_ADDRESS, ADXL345_REGISTER_XLSB, 6, bytes);
//now unpack the bytes
for (int i=0;i<3;++i) {
accelerometer_data[i] = (int)bytes[2*i] + (((int)bytes[2*i + 1]) << 8);
}
}
//Gyroscope
#define ITG3200_ADDRESS (0xD0 >> 1)
//request burst of 6 bytes from this address
#define ITG3200_REGISTER_XMSB (0x1D)
#define ITG3200_REGISTER_DLPF_FS (0x16)
#define ITG3200_FULLSCALE (0x03 << 3)
#define ITG3200_42HZ (0x03)
void init_itg3200() {
byte data = 0;
//Set DLPF to 42 Hz (change it if you want) and
//set the scale to "Full Scale"
i2c_write(ITG3200_ADDRESS, ITG3200_REGISTER_DLPF_FS, ITG3200_FULLSCALE | ITG3200_42HZ);
//Sanity check! Make sure the register value is correct.
i2c_read(ITG3200_ADDRESS, ITG3200_REGISTER_DLPF_FS, 1, &data);
Serial.println((unsigned int)data);
}
int gyro_data[3];
void read_itg3200() {
byte bytes[6];
memset(bytes,0,6);
//read 6 bytes from the ITG3200
i2c_read(ITG3200_ADDRESS, ITG3200_REGISTER_XMSB, 6, bytes); //now unpack the bytes
for (int i=0;i<3;++i) {
gyro_data[i] = (int)bytes[2*i + 1] + (((int)bytes[2*i]) << 8);
}
}
//Magenetometer
#define HMC5843_ADDRESS (0x3C >> 1)
//First data address of 6 is XMSB. Also need to set a configuration register for
//continuous measurement
#define HMC5843_REGISTER_XMSB (0x03)
#define HMC5843_REGISTER_MEASMODE (0x02)
#define HMC5843_MEASMODE_CONT (0x00)
void init_hmc5843() {
byte data = 0;
//set up continuous measurement
i2c_write(HMC5843_ADDRESS, HMC5843_REGISTER_MEASMODE, HMC5843_MEASMODE_CONT);
//Sanity check, make sure the register value is correct.
i2c_read(HMC5843_ADDRESS, HMC5843_REGISTER_MEASMODE, 1, &data);
Serial.println((unsigned int)data);
}
int magnetometer_data[3];
void read_hmc5843() {
byte bytes[6];
memset(bytes,0,6);
//read 6 bytes from the HMC5843
i2c_read(HMC5843_ADDRESS, HMC5843_REGISTER_XMSB, 6, bytes);
//now unpack the bytes
for (int i=0;i<3;++i) {
magnetometer_data[i] = (int)bytes[2*i + 1] + (((int)bytes[2*i]) << 8);
}
}
// Code Begins
void setup()
//SDcard
{
Serial.begin(9600);
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.");
{
Wire.begin();
Serial.begin(9600);
for(int i = 0; i < 3; ++i) {
accelerometer_data[i] = gyro_data[i] = magnetometer_data[i] = 0;
}
init_adxl345();
init_hmc5843();
init_itg3200();
}
}
//Sensor stick
void loop()
//SD Card
{
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 4; analogPin < 5; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// 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) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
{
read_adxl345();
Serial.print("ACCEL: ");
Serial.print(accelerometer_data[0]);
Serial.print("\t");
Serial.print(accelerometer_data[1]);
Serial.print("\t");
Serial.print(accelerometer_data[2]);
Serial.print("\n");
read_hmc5843();
Serial.print("MAG: ");
Serial.print(magnetometer_data[0]);
Serial.print(",");
Serial.print(magnetometer_data[1]);
Serial.print(",");
Serial.print(magnetometer_data[2]);
Serial.print("\n");
read_itg3200();
Serial.print("GYRO: ");
Serial.print(gyro_data[0]);
Serial.print("\t");
Serial.print(gyro_data[1]);
Serial.print("\t");
Serial.print(gyro_data[2]);
Serial.print("\n");
//Sample at 10Hz
delay(100);
}
}
}
any and all help is needed and