#include <Wire.h>
const int MPU_addr = 0x68; // I2C address of the MPU6050
int16_t accelerometer_x, accelerometer_y, accelerometer_z; // Accelerometer readings
int16_t gyroscope_x, gyroscope_y, gyroscope_z; // Gyroscope readings
int led_pin = 4; // Pin for the non-addressable LED
void setup() {
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // Set to 0 to wake up the MPU6050
Wire.endTransmission(true);
Serial.begin(9600);
pinMode(led_pin, OUTPUT);
}
void loop() {
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // Starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // Request 14 bytes from the MPU6050
// Read the accelerometer and gyroscope values
accelerometer_x = Wire.read()<<8 | Wire.read();
accelerometer_y = Wire.read()<<8 | Wire.read();
accelerometer_z = Wire.read()<<8 | Wire.read();
Wire.read();
Wire.read();
gyroscope_x = Wire.read()<<8 | Wire.read();
gyroscope_y = Wire.read()<<8 | Wire.read();
gyroscope_z = Wire.read()<<8 | Wire.read();
// Print the readings to the serial monitor
Serial.print("Ax: "); Serial.print(accelerometer_x);
Serial.print(" Ay: "); Serial.print(accelerometer_y);
Serial.print(" Az: "); Serial.println(accelerometer_z);
Serial.print("Gx: "); Serial.print(gyroscope_x);
Serial.print(" Gy: "); Serial.print(gyroscope_y);
Serial.print(" Gz: "); Serial.println(gyroscope_z);
// Change the intensity of the LED based on the accelerometer readings
int intensity = map(abs(gyroscope_z), 0, 16384, 0, 255);
analogWrite(led_pin, intensity);
delay(2); // Wait for a short time before taking the next reading
}