#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// --- Hardware Pin Definitions ---
// Keypad 4x4
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {A2, 3, 1, A1}; // Connect to Arduino pins
byte colPins[COLS] = {13, 11, 10, 9}; // Connect to Arduino pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// LED for Lock Status
#define lockLedPin 12
// Seatbelt Switch Pin
#define seatbeltPin A3
// Relay Control Pin
#define relayPin 4
// Buzzer Pin
#define buzzerPin 5
// Ultrasonic Sensor Pins
#define trigPin 8
#define echoPin 7
// LCD I2C Address and Size
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
// --- Global Variables ---
const String correctCode = "1234"; // Change this to your desired code
String enteredCode = "";
bool ignitionEnabled = false;
bool isSeatbeltOn = false;
// --- Function Prototypes ---
void displayLCD(int row, String text);
void checkKeypad();
void checkSeatbelt();
void checkObstacle();
void checkAlcohol();
void checkSpeed();
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(lockLedPin, OUTPUT);
pinMode(seatbeltPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(lockLedPin, LOW);
digitalWrite(relayPin, LOW); // Initially lock is engaged (relay off)
displayLCD(0, "Enter Code:");
}
void loop() {
checkKeypad();
checkSeatbelt();
if (ignitionEnabled) {
checkObstacle();
checkAlcohol();
checkSpeed();
if (!isSeatbeltOn) {
Serial.println("Warning: Seatbelt not on!");
displayLCD(0, "Warning: Seatbelt!");
displayLCD(1, "Please fasten!");
// Optionally trigger buzzer
tone(buzzerPin, 1000, 500);
}
}
delay(100);
}
void displayLCD(int row, String text) {
lcd.setCursor(0, row);
lcd.print(text);
}
void checkKeypad() {
char key = keypad.getKey();
if (key) {
enteredCode += key;
displayLCD(1, enteredCode);
if (enteredCode.length() == correctCode.length()) {
if (enteredCode == correctCode) {
displayLCD(0, "Vehicle Unlocked");
digitalWrite(lockLedPin, HIGH);
digitalWrite(relayPin, HIGH); // Disengage lock (relay on)
ignitionEnabled = true;
} else {
displayLCD(0, "Incorrect Code");
digitalWrite(lockLedPin, LOW);
digitalWrite(relayPin, LOW); // Engage lock (relay off)
ignitionEnabled = false;
}
enteredCode = "";
delay(2000);
displayLCD(0, "Enter Code:");
displayLCD(1, "");
}
}
}
void checkSeatbelt() {
if (digitalRead(seatbeltPin) == LOW) {
isSeatbeltOn = true;
} else {
isSeatbeltOn = false;
}
}
void checkObstacle() {
long duration;
int distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2
if (distance < 50) { // Obstacle detected within 50 cm
Serial.println("Obstacle Detected!");
displayLCD(0, "Obstacle Ahead!");
tone(buzzerPin, 1500, 500);
delay(1000);
}
}
void checkAlcohol() {
int alcoholValue = analogRead(A4); // Assuming MQ6 is connected to A4
Serial.print("Alcohol Value: ");
Serial.println(alcoholValue);
if (alcoholValue > 200) { // Adjust threshold based on your MQ6 sensor
Serial.println("Alcohol Detected! Engine Disabled.");
displayLCD(0, "Alcohol Detected!");
displayLCD(1, "Engine Disabled!");
digitalWrite(relayPin, LOW); // Engage lock (disable engine)
ignitionEnabled = false;
tone(buzzerPin, 2000, 1000);
delay(5000);
}
}
void checkSpeed() {
// Simulate speed sensor using a button connected to A5
int speedSensorValue = digitalRead(A5);
static unsigned long lastPulseTime = 0;
static unsigned int pulseCount = 0;
unsigned long currentTime = millis();
static unsigned long lastSpeedTime = 0; // Declare lastSpeedTime
if (speedSensorValue == LOW) { // Simulate a pulse
if (currentTime - lastPulseTime >= 50) { // Debounce
pulseCount++;
lastPulseTime = currentTime;
}
}
if (currentTime - lastSpeedTime >= 1000) {
// Assuming each pulse corresponds to a certain distance traveled
float speed = (pulseCount / (float)(currentTime - lastSpeedTime)) * 3.6; // km/h (adjust calculation)
Serial.print("Speed: ");
Serial.print(speed);
Serial.println(" km/h");
displayLCD(1, String(speed) + " km/h");
lastSpeedTime = currentTime;
pulseCount = 0;
if (speed > 60) {
Serial.println("Speed Limit Exceeded! Alerting...");
displayLCD(0, "Speed Limit Exceeded!");
tone(buzzerPin, 2500, 1000);
}
}
}