HI i am working on a project regarding using ADXL345 eval board (http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXLEvalBoard%20User_Guide2009-09-14.pdf)
and arduino Uno and few servo motors (as outputs)
Now i met a bottleneck. The connection is direct connection from adxl 345eval board to arduino. So i need to get the value out from the register (0x32 till 0x37) in adxl345. In the first stage, i am using a LED to indicate whether i can read the register value or not..but it fail anyway. Anyone can please check the error for me?
Btw noted that direct connection from adxl345 eval board to arduino is using USB connection. Arduino Uno -> ADXL345 eval board (USB connected)
Here is my code:
int ledPin = 2; // LED connected to digital pin 2
#include <Wire.h>
#define DEVICE (0x53) //define the address for DEVICE
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(57600); // start serial for output
writeTo(DEVICE, 0x2D, 0); //here is the part that I enable the bit in the register in 2D and 2E. for INT and Power configuration
writeTo(DEVICE, 0x2D, 16);
writeTo(DEVICE, 0x2D, 8);
writeTo(DEVICE, 0x2E, 128);
writeTo(DEVICE, 0x2E, 2);
writeTo(DEVICE, 0x2E, 1);
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
The read and write function:
void readfrom(int device, byte address)
{
int temp = 0;
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); //sends address to read from
Wire.endTransmission(); //end transmission
Wire.beginTransmission(device);
Wire.requestFrom(device,1);
while(Wire.available())
{
int c = Wire.read();
if (c != temp){
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000);} // waits for a second
else{
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); } // waits for a second
temp =c;
}//end while loop.
Wire.endTransmission(); //end transmission
}
void writeTo(int device, byte address, byte val) {
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); //end transmission
}
The loop function:
readfrom (0x53, address);
Obviously i am using I2C library....now i am wondering that will this code works if i am using USB connection?any replied is highly appreciate...