#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <Servo.h>

// Create an ADXL345 accelerometer object
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

// Create a servo object
Servo myservo;

void setup() {
  // Start the serial communication
  Serial.begin(9600);

  // Initialize the accelerometer
  accel.begin();

  // Attach the servo to pin 9
  myservo.attach(9);
}

void loop() {
  // Get a new sensor event
  sensors_event_t event;
  accel.getEvent(&event);

  // Print the tilt data
  Serial.print("X:");
  Serial.print(event.acceleration.x);
  Serial.print("Y:");
  Serial.print(event.acceleration.y);
  Serial.print("Z:");
  Serial.println(event.acceleration.z);

  // Map the tilt data to a servo angle
  int servoAngle = map(event.acceleration.x, -1, 1, 0, 180);

  // Set the servo position
  myservo.write(servoAngle);

  // Wait for a while
  delay(50);
}