#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
int ledLeft = 7; // Define the pin for the left LED
int ledRight = 8; // Define the pin for the right LED
int16_t gyroX, gyroY, gyroZ;
int16_t threshold = 5000; // Adjust this threshold value to suit your needs
int turnDirection = 0; // 0 for no turn, 1 for left, -1 for right
unsigned long lastTurnTime = 0;
unsigned long turnDelay = 2000; // Adjust this delay as needed (in milliseconds)
void setup() {
Wire.begin();
mpu.initialize();
pinMode(ledLeft, OUTPUT);
pinMode(ledRight, OUTPUT);
Serial.begin(9600);
}
void loop() {
mpu.getRotation(&gyroX, &gyroY, &gyroZ);
unsigned long currentTime = millis();
if (abs(gyroX) > threshold) {
if (gyroX > 0 && turnDirection != 1) {
// Gyro rotation to the right
if (currentTime - lastTurnTime > turnDelay) {
digitalWrite(ledRight, HIGH);
digitalWrite(ledLeft, LOW);
turnDirection = -1;
lastTurnTime = currentTime;
}
} else if (gyroX < 0 && turnDirection != -1) {
// Gyro rotation to the left
if (currentTime - lastTurnTime > turnDelay) {
digitalWrite(ledLeft, HIGH);
digitalWrite(ledRight, LOW);
turnDirection = 1;
lastTurnTime = currentTime;
}
}
} else {
turnDirection = 0; // No turn
digitalWrite(ledLeft, LOW);
digitalWrite(ledRight, LOW);
}
// Print gyro values for debugging
Serial.print("X-axis: ");
Serial.println(gyroX);
delay(10); // Adjust this delay as needed
}