#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
// usage of ESP32 over aurdino nano is this has inbuilt wifi and bluetooth
// Potentiometer is being used for alcohol detection and once the sample is taken and
// that is greater than the thereshold value it detects that person consumed alcohol
const int alcoholPin = 34; // Analog pin for ESP32
const int buzzerPin = 19; // Buzzer pin
const int ledPin = 18; // LED pin
const int threshold_value = 400; // Alcohol level threshold value
void setup() {
Serial.begin(115200);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// Initialize accelerometer to detect the movement of the helmet
Wire.begin(21, 22); // SDA is connected to GPIO21 and SCL is being connected to GPIO22
mpu.initialize();
if (!mpu.testConnection()) {
Serial.println("Accelerometer connection failed!");
} else {
Serial.println("Accelerometer connected successfully!");
}
}
void loop() {
int alcohol_Value = analogRead(alcoholPin); // Read potentiometer value (simulating alcohol level)
// Getting 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
digitalWrite(buzzerPin, HIGH); // Sound buzzer
sendBluetoothSignal("Stop"); // Send "Stop" command via Bluetooth
delay(1000);
}
// Helmet detection:
//the helmet will be idle until we take and place it in head. during this process we displace that
//and keep in head and tilt that due to that its position changes and movement will be
//greater than the threshold value . if so we say that the person is wearing helmet and
//if the movement is less than threshold we consider that helmet is moved just from
//one place to another and not wore by the rider.
//if the person wears helmet the engine is allow to start and this information is given to bike sensor using bluetooth
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
digitalWrite(buzzerPin, HIGH); // Sound buzzer
sendBluetoothSignal("Stop"); // Send "Stop" command via Bluetooth
delay(1000);
} else {
// Both helmet worn and no alcohol detected the rider is safe to ride the bike and
//the information to start the bike will pass to the bike sensor using bluetooth.
Serial.println("Helmet worn and no alcohol detected.");
digitalWrite(ledPin, LOW); // Turn off LED
digitalWrite(buzzerPin, LOW); // Turn off buzzer
sendBluetoothSignal("Start"); // Send "Start" command via Bluetooth
}
delay(250);
}
// Bluetooth communication via Serial Monitor
void sendBluetoothSignal(String command) {
Serial.println(command);
}