Hi there !
My project is to connect two ADXL 345 accelerometers to arduino through I2C interface. I have managed to code as shown below. The problem is that I am unable to get the X,Y,Z data of both sensors at the same time together as output; instead the serial print gives the below output and stops printing further.
OUTPUT of below code:-
CLEARSHEET
LABEL,Time,X0,Y0,Z0,X1,Y1,Z1
DATA,TIME,-4,12,255
As I am new to arduino and also as I do not have much knowledge of any programming languages, I am unable to find out the problem in the code.
I would be very happy if someone could suggest the errors and the solution to the problem I have.
Thanks !
#include <Wire.h>
#define accel_module (0x53)
#define accel_mod (0x1D)
byte values[6];
byte buff[6];
char output[512];
char out[512];
void setup(){
Wire.begin();
Serial.begin(9600);
Serial.println("CLEARSHEET"); // for PLX-DAQ data to excel sheet
Serial.println("LABEL,Time,X0,Y0,Z0,X1,Y1,Z1");
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(0);
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(16);
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(8);
Wire.endTransmission();
Wire.beginTransmission(accel_mod);
Wire.write(0x2D);
Wire.write(0);
Wire.endTransmission();
Wire.beginTransmission(accel_mod);
Wire.write(0x2D);
Wire.write(16);
Wire.endTransmission();
Wire.beginTransmission(accel_mod);
Wire.write(0x2D);
Wire.write(8);
Wire.endTransmission();
}
void loop(){
int xyzregister = 0x32;
int x, y, z;
Wire.beginTransmission(accel_module);
Wire.write(xyzregister);
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.requestFrom(accel_module, 6);
int i = 0;
while(Wire.available())
{
values[i] = Wire.read();
i++;
}
Wire.endTransmission();
x = (((int)values[1]) << 8) | values[0];
y = (((int)values[3]) << 8) | values[2];
z = (((int)values[5]) << 8) | values[4];
/*sprintf(output, "%d %d %d", x, y, z);*/
Serial.print("DATA,TIME,");
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(",");
Serial.print(z);
/*Serial.write(10);*/
////// 2nd ADXL345
int xyzreg = 0x32;
int x1, y1, z1;
Wire.beginTransmission(accel_mod);
Wire.write(xyzreg);
Wire.endTransmission();
Wire.beginTransmission(accel_mod);
Wire.requestFrom(accel_mod, 6);
int j=0;
while(Wire.available());
{
values[j] = Wire.read();
j++;
}
Wire.endTransmission();
x1 = (((int)buff[1]) << 8) | buff[0]; ///////change values to buff
y1 = (((int)buff[3]) << 8) | buff[2];
z1 = (((int)buff[5]) << 8) | buff[4];
Serial.print(x1);
Serial.print(";");
Serial.print(y1);
Serial.print(";");
Serial.println(z1);
delay(100);
}