#include <Wire.h>
#include <MPU6050.h>
// Define constants
const int NUM_LEDS = 14;
const int LED_PINS[NUM_LEDS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A3};
const int SMOOTHNESS = 2.5; // Higher value = smoother transition
// Define MPU6050 object
MPU6050 mpu;
int currentLED = 0;
int targetLED = 0;
void setup() {
Serial.begin(9600);
// Initialize MPU6050
Wire.begin();
mpu.initialize();
mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_250);
mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
// Initialize LED pins
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(LED_PINS[i], OUTPUT);
}
}
void loop() {
// Read accelerometer data
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
// Calculate tilt angle
float angle = atan2(ay, ax) * 180 / PI;
// Map angle to LED index
targetLED = map(angle, 90, -90, 0, NUM_LEDS - 1);
targetLED = constrain(targetLED, 0, NUM_LEDS - 1);
// Smooth LED transition
if (currentLED < targetLED) {
currentLED = min(currentLED + SMOOTHNESS, targetLED);
} else if (currentLED > targetLED) {
currentLED = max(currentLED - SMOOTHNESS, targetLED);
}
// Update LEDs
for (int i = 0; i < NUM_LEDS; i++) {
if (i == currentLED) {
digitalWrite(LED_PINS[i], HIGH);
} else {
digitalWrite(LED_PINS[i], LOW);
}
}
delay(50);
}