Java + Pi4J + I2C + AXL345
Posted: Fri Oct 04, 2013 2:02 pm
Hey guys,
I have been struggeling with Pi4J for some time now, and I wonder if there is a tutorial somewhere to get the AXL345 working. I tried to translate the Adafruit library (http://www.adafruit.com/products/1231), to java. However i do get a response, but the values just don't make any sense. If i move the sensor it changes the values, but not to a good value. I think i did something wrong with the combination of the bytes and ints. I used the following code:
I hope somebody can see my mistake!
I have been struggeling with Pi4J for some time now, and I wonder if there is a tutorial somewhere to get the AXL345 working. I tried to translate the Adafruit library (http://www.adafruit.com/products/1231), to java. However i do get a response, but the values just don't make any sense. If i move the sensor it changes the values, but not to a good value. I think i did something wrong with the combination of the bytes and ints. I used the following code:
Code: Select all
private static final byte ADRESS = 0x53;
private static final byte ADXL345_REG_POWER_CTL = 0x2D;
private static final byte ADXL345_REG_DATAX0 = 0x32; //X-axis data 0
private static final byte ADXL345_REG_DATAY0 = 0x34; //Y-axis data 0
private static final byte ADXL345_REG_DATAZ0 = 0x36; //Z-axis data 0
private static final double ADXL345_MG2G_MULTIPLIER = 0.04; // 4mg per lsb
private static final float SENSOR_GRAVITY_STANDARD = 9.80665F;
private static final int ADXL345_REG_OFSY = 0x1F;
public Accel(I2CBus bus) throws IOException{
System.out.println("Starting Accel");
device = bus.getDevice(ADRESS);
device.write(ADXL345_REG_POWER_CTL, (byte)0x08);
}
public void readAxis() throws IOException{
//device.write(ADXL345_REG_DATAY0); //send data to register to read
byte[] buff = new byte[2];
device.read(ADXL345_REG_DATAY0,buff,0,2);
int response = (buff[0] | buff[1]<< 8);
double y = (double)response * ADXL345_MG2G_MULTIPLIER;
System.out.println(y);
}
public int asInt(byte b){
int i = b;
if(i<0){
i = i +256;
}
return i;
}