#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 accelerometer placed above the helmet to detect the motion of
// the helmet . This is like when we use helmet our head shakes and there will be some movements
// due to that accelerometer will give record those values and if that value is
// higher than the threshold value we say that person wore helmet .
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);
// we are checking the value given by the potentiometer as the value given by alcohol
// detector. Basically when the person who has consumed alcohol breathes near the sensor
//the value increases significantly from 200 to 800 so we kept threshold value as 400
//so that if the value greater than 400 says that the person is consumed alchohol
// and this sends the message that the rider can not start the bike and this also sends
// data to stop the bike to bike sensors provdied in the bike using bluetooth.
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
}
// when the helmet is not worn by the person the accelrometer remains idle and no
// movement is detected .That concludes that the person is not wearing helmet and gives
// the necessary message that person is not wearing helment and sprevent the bike from starting.
//similarly when there is a movement in helmet we say we tilt our head or bend our neck
// down the accelerometer records the value and if that value is greater than threshold
// value then the person wearing helment and checks for alcohol. if person not consumed
// alcohol then we can ride the bike safely.
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(250);
}
// Function to simulate Bluetooth communication via Serial Monitor
// this bluetooth communicates with the bike's electronic control unit and says whether to start the bike or stop it
void sendBluetoothSignal(String command) {
Serial.println(command); // Send command to Serial Monitor
}