Lab_interaccio/2014/sck_adxl345/sck_adxl345.ino
2025-02-25 21:29:42 +01:00

141 lines
3.2 KiB
C++

#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)
int lecturas = 1;
byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device
char str[512]; //string buffer to transform data before writeing it to the serial port
int regAddress = 0x32; //first axis-acceleration-data register on the ADXL345
long int x=0;
long int y=0;
long int z=0;
int x_old=0;
int y_old=0;
int z_old=0;
uint32_t lastSend = 0;
void setup()
{
Serial.begin(9600);
Wire.begin(); // join i2c bus (address optional for master)
writeTo(DEVICE, 0x2D, 0x08);
// writeTo(DEVICE, 0x31, 0x00); //2g
// writeTo(DEVICE, 0x31, 0x01); //4g
writeTo(DEVICE, 0x31, 0x02); //8g
// writeTo(DEVICE, 0x31, 0x03); //16g
delay(15);
}
void loop()
{
checkAdxl();
Serial.print("x= ");
Serial.print(x);
Serial.print(", ");
Serial.print("y= ");
Serial.print(y);
Serial.print(", ");
Serial.print("z= ");
Serial.println(z);
delay(100);
}
void checkAdxl()
{
byte res = 0;
averageADXL();
if ( (x > (x_old + res) ) || (x < (x_old - res) ) )
{
x_old = x;
}
if ( (y > (y_old + res) ) || (y < (y_old - res) ) )
{
y_old = y;
}
if ( (z > (z_old + res) ) || (z < (z_old - res) ) )
{
z_old = z;
}
}
int average(int anaPin)
{
long total = 0;
long average = 0;
int count = 0;
for(int i=0; i<lecturas; i++)
{
total = total + analogRead(anaPin);
}
average = total / lecturas;
return(average);
}
int averageADXL()
{
int temp_x=0;
int temp_y=0;
int temp_z=0;
x=0;
y=0;
z=0;
for(int i=0; i<lecturas; i++)
{
readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
temp_x = (((int)buff[1]) << 8) | buff[0];
temp_x = map(temp_x,-lim,lim,0,1023);
temp_y = (((int)buff[3])<< 8) | buff[2];
temp_y = map(temp_y,-lim,lim,0,1023);
temp_z = (((int)buff[5]) << 8) | buff[4];
temp_z = map(temp_z,-lim,lim,0,1023);
x = (int)(temp_x + x);
y = (int)(temp_y + y);
z = (int)(temp_z + z);
}
x = (int)(x / lecturas);
y = (int)(y / lecturas);
z = (int)(z / lecturas);
}
//---------------- Functions
//Writes val to address register on device
void writeTo(int device, byte address, byte val) {
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); // write register address
Wire.write(val); // write 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.write(address); //writes 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 write less than requested (abnormal)
{
buff[i] = Wire.read(); // read a byte
i++;
}
Wire.endTransmission(); //end transmission
}