/*
Ultrasonic Sensor HC-SR04 and Arduino Tutorial
by Dejan Nedelkovski,
www.HowToMechatronics.com
*/
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
const int trigPin = 9;
const int echoPin = 10;
const int out_1 = 11;
const int out_2 = 12;
int i = 0; //interger i is defined to store the value of buttons
int j; //interger j is defined to store the value of buttons
int h; //interger h is defined to store the value of buttons
int b1 = A0; //button1 pin connecetd to pin 5 of Arduino UNO
int b2 = A1; //button1 pin connecetd to pin 6 of Arduino UNO
int b3 = A2; //button1 pin connecetd to pin 4 of Arduino UNO
int b4 = A3; //button1 pin connecetd to pin 3 of Arduino UNO
int b5 = A4; //button1 pin connecetd to pin 2 of Arduino UNO
long duration;
int distanceCm, distanceInch;
void setup() {
lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode( b1 , INPUT_PULLUP); //Set button1 as INPUT device
pinMode( b2 , INPUT_PULLUP); //Set button2 as INPUT device
pinMode( b3 , INPUT_PULLUP); //Set button3 as INPUT device
pinMode( b4 , INPUT_PULLUP); //Set button4 as INPUT device
pinMode( b5 , INPUT_PULLUP); //Set button5 as INPUT device
pinMode(trigPin, OUTPUT);
pinMode(out_1, OUTPUT);
pinMode(out_2, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int v1 = digitalRead(b1); //Reads the value from a button1 and store the value in V1
int v2 = digitalRead(b2); //Reads the value from a button2 and store the value in V1
int v3 = digitalRead(b3); //Reads the value from a button3 and store the value in V1
int v4 = digitalRead(b4); //Reads the value from a button4 and store the value in V1
int v5 = digitalRead(b5); //Reads the value from a button5 and store the value in V1
if (( v1 == LOW)&&(v2 == HIGH)&&(v3== HIGH)&&(v4==HIGH)&&(v5 == HIGH))
{
j = (i += (1 - (i >= 150))); //incrementing the i value
lcd.setCursor(0,1);
lcd.print("Speed: ");
delay(100);
lcd.println(i);
}
if (( v1 == HIGH)&&(v2 == LOW)&&(v3== HIGH)&&(v4==HIGH)&&(v5 == HIGH))
{
i-- ; //decrement the i value
lcd.setCursor(0,1);
lcd.print("Speed: ");
delay(100);
lcd.println(i);
}
if (( v1 == HIGH)&&(v2 == HIGH)&&(v3== LOW)&&(v4==HIGH)&&(v5 == HIGH))
{
if (i>5){
lcd.setCursor(0,0);
lcd.print("Adaptive cruise");
delay(500);
lcd.setCursor(0,0);
lcd.print("Acitvated");
delay(500);
lcd.println(i);
h=i;
}
else{
lcd.setCursor(0,0);
lcd.print("Speed Low");
delay(500);
lcd.println(i);
}
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm = duration * 0.034 / 2;
distanceInch = duration * 0.0133 / 2;
lcd.setCursor(0, 0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
delay(10);
lcd.setCursor(0, 1);
lcd.print("Speed");
delay(10);
if( distanceCm < 200)
{
j = (i -= (1- (i <= 0))); //incrementing the i value
digitalWrite(out_1, HIGH);
digitalWrite(out_2, LOW);
lcd.setCursor(0,1);
lcd.print("Speed: ");
delay(100);
lcd.println(i);
}
else
{
j = (i += (1 - (i >=h ))); //incrementing the i value
digitalWrite(out_2, HIGH);
digitalWrite(out_1, LOW);
lcd.setCursor(0,1);
lcd.print("Speed: ");
delay(100);
lcd.println(i);
}
}