#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MPU6050.h>
MPU6050 mpu;
const int buzzerPin = 3; // Digital pin for active buzzer
const int ledPins[] = {4, 5, 6, 7}; // Digital pins for LEDs
const int threshold = 500; // Threshold for detecting movement
const long morseDelay = 500; // Delay for Morse code playback
const long buzzerDelay = 30000; // Delay between buzzer plays
unsigned long lastReadTime = 0; // Last time accelerometer was read
unsigned long lastButtonPressTime = 0; // Last time button was pressed
unsigned long lastBuzzerTime = 0; // Last time buzzer was played
bool buzzerPlaying = false; // Flag to track buzzer playing status
void setup() {
Serial.begin(9600); // Initialize serial communication
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT); // Set LED pins as output
}
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
// Initialize the Wire library (necessary for I2C communication)
Wire.begin();
mpu.initialize(); // Initialize the MPU6050
// Initialize LCD if needed
// LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD I2C address to 0x27, and the LCD size to 16x2
// lcd.init();
// lcd.backlight(); // Turn on the backlight
// lcd.print("Setup Complete");
}
void loop() {
unsigned long currentTime = millis();
// Start time count
if (currentTime - lastReadTime >= 1000) {
// Read accelerometer input
readAccelerometer();
lastReadTime = currentTime;
}
// Check for buzzer delay
if (!buzzerPlaying && currentTime - lastBuzzerTime >= buzzerDelay) {
playBuzzer();
}
// Check for direction functions
if (abs(mpu.getAccelerationX()) > threshold) {
Left(currentTime - lastButtonPressTime >= 30000);
} else {
digitalWrite(ledPins[0], LOW); // Turn off LED
}
if (abs(mpu.getAccelerationX()) > threshold) {
Right(currentTime - lastButtonPressTime >= 30000);
} else {
digitalWrite(ledPins[1], LOW); // Turn off LED
}
if (abs(mpu.getAccelerationY()) > threshold) {
Forward(currentTime - lastButtonPressTime >= 30000);
} else {
digitalWrite(ledPins[2], LOW); // Turn off LED
}
if (abs(mpu.getAccelerationY()) > threshold) {
Backward(currentTime - lastButtonPressTime >= 30000);
} else {
digitalWrite(ledPins[3], LOW); // Turn off LED
}
if (currentTime - lastButtonPressTime >= 30000) {
lastButtonPressTime = currentTime; // Reset timer
}
}
void readAccelerometer() {
// Update accelerometer readings
mpu.update();
}
void indicateDirection(int index, const char* message, bool morseActive) {
if (morseActive) {
// Play Morse code if needed
// playMorse(morseCode);
}
digitalWrite(ledPins[index], HIGH); // Light up LED
// Display direction on LCD if needed
// displayMessage(message);
}
void Left(bool morseActive) {
indicateDirection(0, "Left", morseActive);
}
void Right(bool morseActive) {
indicateDirection(1, "Right", morseActive);
}
void Forward(bool morseActive) {
indicateDirection(2, "Forward", morseActive);
}
void Backward(bool morseActive) {
indicateDirection(3, "Backward", morseActive);
}
void playMorse(const char* morseCode) {
// Play Morse code implementation
}
void playBuzzer() {
buzzerPlaying = true;
// Play your desired buzzer tone here
digitalWrite(buzzerPin, HIGH);
delay(500); // Adjust as needed
digitalWrite(buzzerPin, LOW);
lastBuzzerTime = millis();
buzzerPlaying = false;
}