130 lines
3.8 KiB
Plaintext
130 lines
3.8 KiB
Plaintext
#include <Wire.h>
|
|
|
|
#define lim 512
|
|
#define DEVICE (0x53) //ADXL345 device address
|
|
#define TO_READ (6) //num of bytes we are going to read each time (two bytes for each axis)
|
|
|
|
byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device
|
|
char str[512]; //string buffer to transform data before sending it to the serial port
|
|
|
|
void setup()
|
|
{
|
|
Wire.begin(); // join i2c bus (address optional for master)
|
|
Serial.begin(19200); // start serial for output
|
|
|
|
pinMode(9,INPUT);
|
|
|
|
//Turning on the ADXL345
|
|
writeTo(DEVICE, 0x2D, 0);
|
|
writeTo(DEVICE, 0x2D, 16);
|
|
writeTo(DEVICE, 0x2D, 8);
|
|
|
|
//writeTo(DEVICE, 0x31, 8);
|
|
|
|
writeTo(DEVICE, 0x31, 11);
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
int regAddress = 0x32; //first axis-acceleration-data register on the ADXL345
|
|
int x, y, z;
|
|
|
|
writeTo(DEVICE, 0x31, 8);
|
|
|
|
readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
|
|
|
|
//each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
|
|
//thus we are converting both bytes in to one int
|
|
x = (((int)buff[1]) << 8) | buff[0];
|
|
x = map(x,-lim,lim,0,200);
|
|
y = (((int)buff[3])<< 8) | buff[2];
|
|
y = map(y,-lim,lim,0,200);
|
|
z = (((int)buff[5]) << 8) | buff[4];
|
|
z = map(z,-lim,lim,0,200);
|
|
|
|
Serial.print(234,BYTE);
|
|
Serial.print(x,BYTE);
|
|
Serial.print(235,BYTE);
|
|
Serial.print(y,BYTE);
|
|
Serial.print(236,BYTE);
|
|
Serial.print(z,BYTE);
|
|
|
|
writeTo(DEVICE, 0x31, 11);
|
|
|
|
readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
|
|
|
|
//each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
|
|
//thus we are converting both bytes in to one int
|
|
x = (((int)buff[1]) << 8) | buff[0];
|
|
x = map(x,-lim,lim,0,200);
|
|
y = (((int)buff[3])<< 8) | buff[2];
|
|
y = map(y,-lim,lim,0,200);
|
|
z = (((int)buff[5]) << 8) | buff[4];
|
|
z = map(z,-lim,lim,0,200);
|
|
|
|
Serial.print(232,BYTE);
|
|
Serial.print(x,BYTE);
|
|
Serial.print(233,BYTE);
|
|
Serial.print(y,BYTE);
|
|
|
|
Serial.print(239,BYTE);
|
|
if (digitalRead(9)) Serial.print(0,BYTE);
|
|
else Serial.print(200,BYTE);
|
|
|
|
int bat=map(analogRead(3),372,482,0,100);
|
|
Serial.print(237,BYTE);
|
|
Serial.print(bat,BYTE);
|
|
|
|
Serial.print(230,BYTE);
|
|
Serial.print(map(analogRead(6),0,1023,0,200),BYTE);
|
|
|
|
Serial.print(231,BYTE);
|
|
Serial.print(map(analogRead(7),0,1023,0,200),BYTE);
|
|
|
|
Serial.print(238,BYTE);
|
|
Serial.print(map(analogRead(0),0,1023,0,200),BYTE);
|
|
|
|
Serial.print(240,BYTE);
|
|
Serial.print(map(analogRead(1),0,1023,0,200),BYTE);
|
|
|
|
Serial.print(241,BYTE);
|
|
Serial.print(map(analogRead(2),0,1023,0,200),BYTE);
|
|
|
|
//we send the x y z values as a string to the serial port
|
|
/*sprintf(str, "%d %d %d", x, y, z);
|
|
Serial.print(str);
|
|
Serial.print(10, BYTE);*/
|
|
|
|
//It appears that delay is needed in order not to clog the port
|
|
delay(15);
|
|
}
|
|
|
|
//---------------- Functions
|
|
//Writes val to address register on device
|
|
void writeTo(int device, byte address, byte val) {
|
|
Wire.beginTransmission(device); //start transmission to device
|
|
Wire.send(address); // send register address
|
|
Wire.send(val); // send value to write
|
|
Wire.endTransmission(); //end transmission
|
|
}
|
|
|
|
//reads num bytes starting from address register on device in to buff array
|
|
void readFrom(int device, byte address, int num, byte buff[]) {
|
|
Wire.beginTransmission(device); //start transmission to device
|
|
Wire.send(address); //sends address to read from
|
|
Wire.endTransmission(); //end transmission
|
|
|
|
Wire.beginTransmission(device); //start transmission to device
|
|
Wire.requestFrom(device, num); // request 6 bytes from device
|
|
|
|
int i = 0;
|
|
while(Wire.available()) //device may send less than requested (abnormal)
|
|
{
|
|
buff[i] = Wire.receive(); // receive a byte
|
|
i++;
|
|
}
|
|
Wire.endTransmission(); //end transmission
|
|
}
|
|
|
|
|