hey all,
I'm currently working on this project that uses the MMA7455 accelerometer using I2C to the arduino. My goal for this project is to obtain the accelerations and use them to calculate g-force and save on a sd card. Currently, I am having trouble getting the accelerations positive and negative. I just get values from 0 to 255. I have everything else working that i need except this. So I'm asking if you all can help me out.
Thanks in advance.
Here's my code
#include <Wire.h>
#include <SD.h>
#define ACCELEROMETER 0x1D //Address for Accelerometer
//REGISTERS
#define MODE_CONTROL 0x16 //Mode control register
#define PULSE_DET 0x1B //Pulse detection threshold limit value
#define X_OUT 0x06 //8 bit register containing value for X
#define Y_OUT 0x07 //8 bit register containing value for Y
#define Z_OUT 0x08 //8 bit register containing value for Z
#define DETECTION 0x19 //Detection source register
#define THRESHOLD 0x18 // setting the threshold to be based on the threshold value as positive or negative
//VALUES
//#define Z_PULSE 0x40 //Pulse detected on Z-axis
#define SENSEVALUE 0x05 // Measurement mode to write to Mode Control
#define level 0x1A
File myFile;
int but2 = 7;
int but = 8;
int rdyPin = 9;
int reading, reading2;
int previous = LOW;
double force;
int mass = 20;
int val;
//required setup function
void setup() {
Wire.begin();
Serial.begin(9600);
accWrite(MODE_CONTROL, SENSEVALUE); //writes measurement mode to mode control
accWrite(DETECTION, 0x00); // setting up for motion detection
accWrite(THRESHOLD, 0x00);
accWrite(level, 0x2F);
Serial.print("Initializing SD card...."); //getting SD card ready
pinMode(10, OUTPUT);
pinMode(but, INPUT);
pinMode(rdyPin, OUTPUT);
pinMode(but2, INPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("accellog.csv", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to accellog.csv ");
///String header = "xVal, yVal, zVal";
myFile.println("X, Y, Z"); //myFile.println(header);
// close the file:
myFile.close();
Serial.println("header");
} else {
// if the file didn't open, print an error:
Serial.println("error opening file");
}
/* re-open the file for reading:
myFile = SD.open("accellog_1.csv");
if (myFile) {
Serial.println("accellog_1.csv:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening accellog_1.csv");
}*/
}
void loop()
{
while(digitalRead(but)== HIGH)
{
digitalWrite(rdyPin, LOW);
}
while (digitalRead(but2)== HIGH)
{
//force = mass*X_OUT;
//Serial.println(force);
//Data string for storing to SD card
// Use CSV format
/*Serial.print("X: ");
Serial.print(accRead(X_OUT));
Serial.print(" Y: ");
Serial.print(accRead(Y_OUT));
Serial.print(" Z: ");
Serial.println(accRead(Z_OUT));
delay(100);
*/
//String dataString = String(accRead(X_OUT)) + "," + String(accRead(Y_OUT)) + "," + String(accRead(Z_OUT));
digitalWrite(rdyPin, HIGH);
File myFile = SD.open("accellog.csv", FILE_WRITE);
if (myFile)
{
myFile.print (accRead(X_OUT)); // myFile.println(dataString);
myFile.print (",");
myFile.print (accRead(Y_OUT));
myFile.print (",");
myFile.println (accRead(Z_OUT));
myFile.close();
//Serial.println(dataString);
Serial.print (accRead(X_OUT)); // myFile.println(dataString);
Serial.print (",");
Serial.print (accRead(Y_OUT));
Serial.print (",");
Serial.println (accRead(Z_OUT));
}
//{
//Serial.println("Could not open file");
//}
digitalWrite(rdyPin, LOW);
delay(50);
}
}
void accWrite(byte address, byte data) {
Wire.beginTransmission(ACCELEROMETER);
Wire.write(address);
Wire.write(data);
Wire.endTransmission();
}
//function to read byte data from a register
unsigned int accRead(byte address){
byte val = 0x00;
Wire.beginTransmission(ACCELEROMETER);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(ACCELEROMETER, 1);
if(Wire.available() > 0) {
val = Wire.read();
}
Wire.endTransmission();
return val;
}
// MMA 7455 pin
// 1 - VIN to Arduino 3.3V
// 5 - GND, ground to Arduino GND
// 6 - Chip Select - connect this to 5V with a 1K resistor, this selects I2C mode
// 7 - Data - connect this to the ATmega's SDA (data line) on analog input pin 4, with 4.7K pull up resistor
// 8 - Clock - connect this to ATmega's SCL (clock line) on analog input pin 5, with 4.7K pull up resistor