Close

2nd step : coding the accelerometer

A project log for 2D LED Ping ball

We want to create a kind of joystick, using an accelerometer, that follows the movement of our hands and that display it on a LED matrix

sarahsarah 12/08/2023 at 15:160 Comments

The first thing we did, even before touching the LED matrix, we tried to code the accelerometer with Arduino so that it will accurately follow our movement. We tried many codes, not all of them worked and sometimes we would find a code that would work but since we didn't understand them we choose not to use them. So, we tried to write it ourselves, and with the help of this websites and YouTube videos (  here and here) , we managed to code our accelerometer.

#include <Wire.h>  

int ADXL345 = 0x52; 

float X_out, Y_out, Z_out;  // Outputs

void setup() {
  Serial.begin(9600); 
  Wire.begin(); 
  Wire.beginTransmission(ADXL345); 
  Wire.write(0x2D);
  Wire.write(8);
  Wire.endTransmission();
  delay(100);
}

void loop() {
  // === Read acceleromter data === //
  Wire.beginTransmission(ADXL345);
  Wire.write(0x32); 
  Wire.endTransmission(false);
  Wire.requestFrom(ADXL345, 6, true); 
  X_out = ( Wire.read()| Wire.read() << 8); 
  X_out = X_out/64;#+-8 sensitivity
  Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
  Y_out = Y_out/64;
  Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
  Z_out = Z_out/64;

  Serial.print("Xa= ");
  Serial.print(X_out);
  Serial.print("   Ya= ");
  Serial.print(Y_out);
  Serial.print("   Za= ");
  Serial.println(Z_out);
}

Discussions