Hi all,
I'm trying to connect two ADXL345 accelerometers to an Arduino UNO (via I2C), but as I am an absolute beginner at Arduino, I'm getting stuck.
And this is the code I use (I tried to keep it as simple as possible):
#include <Wire.h> // Get I2C library
// ADXL345 1
#define ACC1 (0x53) //define ADXL345 address
#define A1_TO_READ (6) //read bytes (2) every time
// ADXL345 2
#define ACC2 (0x1D) //define ADXL345 address
#define A2_TO_READ (6) //read bytes (2) every time
// for the offset
int a1_offx = 0;
int a1_offy = 0;
int a1_offz = 0;
// for the offest
int a2_offx = 0;
int a2_offy = 0;
int a2_offz = 0;
char str[512];
void getAccelerometerData1(int * result) {
int regAddress = 0x32; //The setting of data of the first axis of ADXL345
byte buff[A1_TO_READ];
readFrom(ACC1, regAddress, A1_TO_READ, buff); //read the data from adxl345
//the value of every axis has 10 resolution, which means 2 bytes
//we have to convert 2 bytes into 1 int value.
result[0] = (((int)buff[1]) << 8) | buff[0] + a1_offx;
result[1] = (((int)buff[3]) << 8) | buff[2] + a1_offy;
result[2] = (((int)buff[5]) << 8) | buff[4] + a1_offz;
}
void getAccelerometerData2(int * result) {
int regAddress = 0x32; //The setting of data ofthe first axis of ADXL345
byte buff[A2_TO_READ];
readFrom(ACC2, regAddress, A2_TO_READ, buff); //read the data from adxl345
//the value of every axis has 10 resolution, which means 2 bytes
//we have to convert 2 bytes into 1 int value.
result[0] = (((int)buff[1]) << 8) | buff[0] + a2_offx;
result[1] = (((int)buff[3]) << 8) | buff[2] + a2_offy;
result[2] = (((int)buff[5]) << 8) | buff[4] + a2_offz;
}
void setup()
{
Serial.begin(9600);
Wire.begin();
writeTo(ACC1, 0x2D, 24);
writeTo(ACC2, 0x2D, 24);
}
// loop reading and printing data
void loop()
{
int acc1[3];
int acc2[3];
getAccelerometerData1(acc1);
getAccelerometerData2(acc1);
sprintf(str, "%d,%d,%d,%d,%d,%d", acc1[0], acc1[1], acc1[2], acc2[0], acc2[1], acc2[2]);
// Serial.print(str);
Serial.print(str);
Serial.write(10);
delay(10); // delay is needed in order not to clog the port
}
// functions
// write val into the address register of accelerometer
void writeTo(int DEVICE, byte address, byte val) {
Wire.beginTransmission(DEVICE); //send to sensor
Wire.write(address); // send register address
Wire.write(val); // send the value which needed to write
Wire.endTransmission(); //end transmission
}
// read data from the buffer array of address registers in the accelerometer sensor
void readFrom(int DEVICE, byte address, int num, byte buff[]) {
Wire.beginTransmission(DEVICE); //start to send to accelerometer sensor
Wire.write(address); //send address which are read
Wire.endTransmission(); //end transmission
Wire.beginTransmission(DEVICE); //start to send to ACC
Wire.requestFrom(DEVICE, num); // require sending 6 bytes data from accelerometer sensor
int i = 0;
while (Wire.available()) //Error when the return value is smaller than required value
{
buff[i] = Wire.read(); // receive data
i++;
}
Wire.endTransmission(); //end transmission
}
The problem is that my output is messed up: there is data printed into the serial monitor, but the same line is repeated over and over (19969,19713,19457,214,2,513) and it doesn't change when I move one of the accelerometers.
What am I doing wrong?
Greetings!