#include <Wire.h>
#include <MPU6050.h>
#include <LiquidCrystal_I2C.h>
MPU6050 mpu;
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define POT_PIN A0 // Potentiometer pin (Alcohol Sensor)
#define BUZZER_PIN 9 // Buzzer pin
#define LED_PIN 8 // LED pin
#define BUTTON_PIN 7 // Button to simulate accident
void setup() {
Serial.begin(9600);
Wire.begin();
mpu.initialize();
pinMode(POT_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Using pull-up resistor
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Smart Helmet");
delay(2000);
lcd.clear();
}
void loop() {
int16_t ax, ay, az;
int alcoholLevel = analogRead(POT_PIN); // Read potentiometer value
// Read MPU6050 accelerometer values
mpu.getAcceleration(&ax, &ay, &az);
float acceleration = sqrt(ax * ax + ay * ay + az * az) / 16384.0; // Normalize value
Serial.print("Acceleration: "); Serial.println(acceleration);
Serial.print("Alcohol Level: "); Serial.println(alcoholLevel);
lcd.setCursor(0, 0);
lcd.print("Acc: ");
lcd.print(acceleration);
lcd.print("g ");
lcd.setCursor(0, 1);
lcd.print("Alcohol: ");
lcd.print(alcoholLevel);
// Accident Detection - If acceleration exceeds a threshold
if (acceleration > 2.5 || digitalRead(BUTTON_PIN) == LOW) { // Button simulates accident
Serial.println("Accident Detected!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Accident Alert!");
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
delay(3000);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
lcd.clear();
}
// Alcohol Detection - If potentiometer value exceeds threshold
if (alcoholLevel > 600) { // Adjust threshold as needed
Serial.println("Alcohol Detected!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alcohol Alert!");
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
delay(3000);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
lcd.clear();
}
delay(500);
}