#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
//Potentiometer is being used to simulate the alcohol sensor (MQ-3)
const int alcoholPin = A0;
const int buzzerPin = 12; // Buzzer pin
const int ledPin = 13; // LED pin
const int threshold_value = 400; // Alcohol level threshold value
void setup() {
Serial.begin(9600);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// Initialize the MPU6050
Wire.begin();
mpu.initialize();
if (!mpu.testConnection()) {
Serial.println("MPU6050 connection failed!");
} else {
Serial.println("MPU6050 connected successfully!");
}
}
void loop() {
int alcohol_Value = analogRead(alcoholPin); // Read potentiometer value (simulating alcohol level)
// Get accelerometer data
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
// Check if alcohol level exceeds threshold value
if (alcohol_Value > threshold_value) {
Serial.println("Alcohol detected! Preventing bike start.");
digitalWrite(ledPin, HIGH); // Turn on LED to indicate alcohol detected
digitalWrite(buzzerPin, HIGH); // Sound buzzer
sendBluetoothSignal("Stop"); // Send signal to prevent bike start
delay(1000); // Keep the alert for 1 second
}
// Check if helmet is worn (no significant movement detected)
else if (abs(ax) < 1000 && abs(ay) < 1000 && abs(az) < 1000) {
Serial.println("Helmet not worn! Preventing bike start.");
digitalWrite(ledPin, HIGH); // Turn on LED to indicate helmet not worn
digitalWrite(buzzerPin, HIGH); // Sound buzzer
sendBluetoothSignal("Stop"); // Send signal to prevent bike start
delay(1000); // Keep the alert for 1 second
} else {
// If everything is normal, allow bike to start
Serial.println("Helmet worn and no alcohol detected.");
digitalWrite(ledPin, LOW); // Turn off LED
digitalWrite(buzzerPin, LOW); // Turn off buzzer
sendBluetoothSignal("Start"); // Send signal to allow bike start
}
delay(100); // Small delay for stable readings
}
// Function to simulate Bluetooth communication via Serial Monitor
void sendBluetoothSignal(String command) {
Serial.println(command); // Send command to Serial Monitor
// Add additional logic here if needed, e.g., send via Serial to another system
}