El código mostrado permite el uso de dos sensores ADXL345 de forma simultanea usando el protocolo de comunicación I2C. Al colocar el pin SDO a tierra en el sensor A y el pin SDO a voltaje (3.3V) en el sensor B lo que hace es alternar las direcciones de ambos sensores, haciendo así que ambos tengan direcciones distintas (y por lo tanto puedan ser usados de forma casi simultanea).
El código de ejemplo pretende indicar la posición del impacto entre ambos sensores, en el código se pueden ver los datos X, Y & Z los cuales pueden ser usados para poder obtener aceleraciones, velocidades o distancias (según sea el caso)
#include <Wire.h> // Wire library - used for I2C communication
int ADXL345 = 0x53; // The ADXL345 sensor I2C address
int ADXL345B = 0x1D;
float X_out, Y_out, Z_out; // Outputs
float X_outb, Y_outb, Z_outb; // Outputs B
float sumavar, mayor, division;
void setup()
{
Serial.begin(9600); // Initiate serial communication for printing the results on the Serial monitor
Wire.begin(); // Initiate the Wire library
// Set ADXL345 in measuring mode
Wire.beginTransmission(ADXL345); // Start communicating with the device
Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
// Enable measurement
Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable
Wire.endTransmission();
Wire.begin(); // Initiate the Wire library
// Set ADXL345 in measuring mode
Wire.beginTransmission(ADXL345B); // Start communicating with the device
Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
// Enable measurement
Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable
Wire.endTransmission();
delay(10);
}
void loop()
{
// === Read acceleromter data === //
Wire.beginTransmission(ADXL345);
Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
X_out = ( Wire.read()| Wire.read() << 8); // X-axis value
X_out = X_out/256; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
Y_out = Y_out/256;
Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
Z_out = Z_out/256;
Serial.print("\nX ");
Serial.print("a");
Serial.print("= ");
Serial.print(X_out);
Serial.println("\nY ");
Serial.print("a");
Serial.print("= ");
Serial.print(Y_out);
Serial.println("\nZ ");
Serial.print("a");
Serial.print("= ");
Serial.print(Z_out);
Serial.println("\n------------------");
// === Read acceleromter data === //
Wire.beginTransmission(ADXL345B);
Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(ADXL345B, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
X_outb = ( Wire.read()| Wire.read() << 8); // X-axis value
X_outb = X_outb/256; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
Y_outb = ( Wire.read()| Wire.read() << 8); // Y-axis value
Y_outb = Y_outb/256;
Z_outb = ( Wire.read()| Wire.read() << 8); // Z-axis value
Z_outb = Z_outb/256;
Serial.print("\nX ");
Serial.print("b");
Serial.print("= ");
Serial.print(X_outb);
Serial.println("\nY ");
Serial.print("b");
Serial.print("= ");
Serial.print(Y_outb);
Serial.println("\nZ ");
Serial.print("b");
Serial.print("= ");
Serial.print(Z_outb);
Serial.println("\n------------------");
sumavar= Z_outb + Z_out;
if (Z_outb > Z_out)
{
mayor = Z_outb;
}
else
{
mayor = Z_out;
}
division = mayor / sumavar;
division = division * 100;
if (Z_outb > Z_out)
{
Serial.println("\n El golpe está orientado hacia la derecha un ") ;
Serial.println(division);
Serial.println( "\n ");
// Serial.println( " respecto la mitad (50%) \n");
}
else
{
Serial.println("\n El golpe está orientado hacia la izquierda un ") ;
Serial.println(division);
Serial.println( "\n ");
//Serial.println( " respecto la mitad (50%) \n");
}
delay(1000);
}