LCD code snippet
#include <Wire.h> #include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
const int tleft1 = 2; const int eleft1 = 3; const int tleft2 = 4; const int eleft2 = 5; const int tright1 = 6; const int eright1 = 7; const int tright2 = 8; const int eright2 = 9;
int buz=10; int m1=14; int m2=15; int m3=16; int m4=17;
// defines variables long durl1; int disl1; long durl2; int disl2; long durr1; int disr1; long durr2; int disr2; long duration; int distance;
String pos=""; void setup() { lcd.init(); // initialize the lcd lcd.init(); // Print a message to the LCD. lcd.backlight(); lcd.setCursor(0,0); lcd.print(" Smart "); lcd.setCursor(0,1); lcd.print(" Movable"); lcd.setCursor(0,2); lcd.print(" Road Divider"); lcd.setCursor(0,3); lcd.print(" Project");
pinMode(tleft1, OUTPUT); // Sets the trigPin as an Output pinMode(eleft1, INPUT); // Sets the echoPin as an Input pinMode(tleft2, OUTPUT); // Sets the trigPin as an Output pinMode(eleft2, INPUT); // Sets the echoPin as an Input pinMode(tright1, OUTPUT); // Sets the trigPin as an Output pinMode(eright1, INPUT); // Sets the echoPin as an Input pinMode(tright2, OUTPUT); // Sets the trigPin as an Output pinMode(eright2, INPUT); // Sets the echoPin as an Input pinMode(m1, OUTPUT); pinMode(m2, OUTPUT); pinMode(m3, OUTPUT); pinMode(m4, OUTPUT); pinMode(buz, OUTPUT); digitalWrite(m1,HIGH); digitalWrite(m2,HIGH); digitalWrite(m3,HIGH); digitalWrite(m4,HIGH); delay(2000); lcd.clear(); Serial.begin(9600); // Starts the serial communication } void loop() { // Clears the trigPin lcd.setCursor(0,0); lcd.print("Traffic Density:"); int disl1=ultrasonic(tleft1,eleft1); int disl2=ultrasonic(tleft2,eleft2); int disr1=ultrasonic(tright1,eright1); int disr2=ultrasonic(tright2,eright2); lcd.setCursor(0,1); lcd.print("L1"+String(disl1)+" || R1"+String(disr1)); lcd.setCursor(0,2); lcd.print("L2"+String(disl2)+" || R2"+String(disr2)); Serial.print("Left Side 1:"); Serial.println(disl1); Serial.print("Left Side 2:"); Serial.println(disl2); Serial.print("Right Side 1:"); Serial.println(disr1); Serial.print("Right Side 2:"); Serial.println(disr2); if(disl1<10&&disl2<10) { if(pos!="RIGHT") { Serial.println("Traffic detected on left side"); digitalWrite(buz,HIGH); delay(300); digitalWrite(buz,LOW); lcd.setCursor(0,3); lcd.print("Waiting.."); if(disr1>10&& disr2>10) {
digitalWrite(buz,HIGH);
delay(1000);
digitalWrite(buz,LOW);
Serial.println("Right Side is Free");
Serial.println("Moving Right");
lcd.setCursor(0,3);
lcd.print("Moving Right");
digitalWrite(m1,LOW);
digitalWrite(m2,LOW);
digitalWrite(m3,LOW);
digitalWrite(m4,LOW);
pos="RIGHT";
delay(1000);
}
} else { Serial.println("Already in Right!!!"); }
} if(disr1<10&&disr2<10) { if(pos!="LEFT") { Serial.println("Traffic detected on right side"); digitalWrite(buz,HIGH); delay(300); digitalWrite(buz,LOW); lcd.setCursor(0,3); lcd.print("Waiting.."); if(disl1>10&& disl2>10) {
digitalWrite(buz,HIGH);
lcd.setCursor(0,3);
lcd.print("Waiting..");
delay(1000); digitalWrite(buz,LOW); Serial.println("Left Side is Free"); Serial.println("Moving Left"); lcd.setCursor(0,3); lcd.print("Moving Left"); digitalWrite(m1,HIGH); digitalWrite(m2,HIGH); digitalWrite(m3,HIGH); digitalWrite(m4,HIGH);
pos="LEFT";
delay(1000);
}
}
else
{ Serial.println("Already in Left!!!"); }
} if(disr1<10&&disl2<10) { if(pos!="LEFT TILT") { Serial.println("Traffic detected on right side");
digitalWrite(buz,HIGH);
delay(300);
digitalWrite(buz,LOW);
lcd.setCursor(0,3);
lcd.print("Waiting..");
if(disl1>10&& disr2>10) {
digitalWrite(buz,HIGH);
delay(1000); digitalWrite(buz,LOW); Serial.println("Left Side is Free"); Serial.println("tilting Left"); lcd.setCursor(0,3); lcd.print("Tilting Left"); digitalWrite(m1,HIGH); digitalWrite(m2,HIGH); digitalWrite(m3,LOW); digitalWrite(m4,LOW); pos="LEFT TILT"; delay(1000); } } else { Serial.println("Already in Left Tilted!!!"); }
} if(disl1<10&&disr2<10) { if(pos!="RIGHT TILT") { Serial.println("Traffic detected on right side"); digitalWrite(buz,HIGH); delay(300); digitalWrite(buz,LOW); lcd.setCursor(0,3); lcd.print("Waiting.."); if(disr1>10&& disl2>10) {
digitalWrite(buz,HIGH);
delay(1000); digitalWrite(buz,LOW); Serial.println("Right Side is Free"); Serial.println("Tilting Right"); lcd.setCursor(0,3); lcd.print("Tilting Right"); digitalWrite(m1,LOW); digitalWrite(m2,LOW); digitalWrite(m3,HIGH); digitalWrite(m4,HIGH); pos="RIGHT TILT"; delay(1000); } } else { Serial.println("Already tilted right!!!"); }
} lcd.setCursor(0,3); lcd.print(" "); lcd.setCursor(7,3); lcd.print(pos); delay(1000); Serial.println(pos); Serial.println("<=================>"); lcd.clear(); }
int ultrasonic(int trigPin,int echoPin) { digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor //Serial.print("Distance: "); //Serial.println(distance); return distance; }