#include <LiquidCrystal.h>
const int motorPin = 9; // Motor control pin
const int potPin = A0; // Potentiometer pin for throttle
const int speedPotPin = A2; // Potentiometer pin for scroll speed
const int trigPin1 = 10; // Ultrasonic sensor 1 trigger pin
const int echoPin1 = 13; // Ultrasonic sensor 1 echo pin
const int trigPin2 = 8; // Ultrasonic sensor 2 trigger pin
const int echoPin2 = 7; // Ultrasonic sensor 2 echo pin
const int alcoholPin = A1; // Alcohol sensor pin
const int buzzerPin1 = 6; // Buzzer for collision warning
const int buzzerPin2 = 5; // Buzzer for alcohol detection
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the LCD
const int maxScrollSpeed = 200; // Fastest scroll speed in milliseconds
const int minScrollSpeed = 50; // Slowest scroll speed in milliseconds
const int scrollLength = 16; // Length of the LCD display
String message = ""; // Message to display
int scrollPosition = 0; // Position for scrolling
unsigned long previousMillis = 0; // Store last time message was updated
enum State {THROTTLE, READY, NOT_READY, COLLISION, ALCOHOL};
State currentState = THROTTLE;
void setup() {
pinMode(motorPin, OUTPUT);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(alcoholPin, INPUT);
pinMode(buzzerPin1, OUTPUT);
pinMode(buzzerPin2, OUTPUT);
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop() {
long distance1 = getDistance(trigPin1, echoPin1);
long distance2 = getDistance(trigPin2, echoPin2);
int potValue = analogRead(potPin);
int alcoholValue = analogRead(alcoholPin);
int speedPotValue = analogRead(speedPotPin); // Read scroll speed potentiometer
// Display potentiometer value
lcd.setCursor(0, 0);
lcd.print("Throttle: ");
lcd.print(map(potValue, 0, 1023, 0, 100));
lcd.print("% "); // Added spaces to clear previous text
// Initialize motor state
int motorSpeed = map(potValue, 0, 1023, 0, 255);
bool isCollision = (distance2 < 50);
bool isAlcoholDetected = (alcoholValue > 400);
// Check for position detection
if (distance1 < 20) { // Object detected within 20 cm
currentState = READY;
if (!isCollision && !isAlcoholDetected) {
analogWrite(motorPin, motorSpeed); // Control motor speed
Serial.print("Motor Speed: ");
Serial.println(motorSpeed); // Debug: Show motor speed
} else {
analogWrite(motorPin, 0); // Turn off motor
Serial.println("Motor OFF: Collision or Alcohol Detected"); // Debug
}
} else {
currentState = NOT_READY;
analogWrite(motorPin, 0); // Turn off motor
Serial.println("Motor OFF: Not Ready"); // Debug
}
// Prepare the message to display
message = "";
if (currentState == READY) {
message += "Ready ";
} else if (currentState == NOT_READY) {
message += "Not Ready ";
}
if (isCollision) {
digitalWrite(buzzerPin1, HIGH);
message += "Collision ";
} else {
digitalWrite(buzzerPin1, LOW);
}
/*if (isAlcoholDetected) {
digitalWrite(buzzerPin2, HIGH);
message += "Alcohol ";
} else {
digitalWrite(buzzerPin2, LOW);
}*/
// Update the display with scrolling text
if (millis() - previousMillis >= map(speedPotValue, 0, 1023, maxScrollSpeed, minScrollSpeed)) {
previousMillis = millis();
scrollPosition = (scrollPosition + 1) % (message.length() + scrollLength); // Wrap around after the message
}
// Clear the second line and display the current scroll position
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the line
if (scrollPosition < message.length()) {
lcd.setCursor(0, 1);
lcd.print(message.substring(scrollPosition));
} else {
lcd.setCursor(0, 1);
lcd.print(" "); // Clear line if out of bounds
lcd.setCursor(scrollLength - scrollPosition + message.length(), 1); // Start scrolling in reverse
lcd.print(message.substring(0, scrollPosition - message.length()));
}
delay(100); // Short delay for loop
}
long getDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2; // Convert to cm
}