#include #include Servo myservo; // create servo object to control a servo. int angle = 0; // variable to store the servo position. int distance; // variable to store the distance from sensor to the object. LiquidCrystal lcd(2, 3, 10, 11, 12, 13);// Pinout, RS=2, E=3, DB4=10, DB5=11, DB6=12, DB7=13. int numRows = 2; // Number of rows of the LCD. int numCols = 16; // Number of Columns of the LCD. unsigned long echo = 0; // variable to store the echo time. int ultraSoundPin = 5; // Ultrasound signal pin. unsigned long ultrasoundValue = 0; // variable to store the final distance calculation. void setup() // We write the initial configuration of our program. { Serial.begin(9600); myservo.attach(4); // attaches the servo on pin 9 to the servo object. myservo.write(angle); lcd.begin(numRows, numCols); // Allocates or initializes the number of rows and columns of the LCD. lcd.clear(); // Clean the LCD screen lcd.setCursor(0,0); // Place the cursor at column 0, row 0. lcd.print(" ((( Moving ))) "); // Write on the screen that is indicated between quotes. lcd.setCursor(0,1); // Place the cursor at column 0, row 1. lcd.print(" ((( Radar ))) "); // Write on the screen that is indicated between quotes. delay(2000); // Wait 2 seconds. lcd.clear(); // Clean the LCD screen. pinMode(ultraSoundPin,OUTPUT); // set pin 5 as an output. } int meassure(){ pinMode(ultraSoundPin, OUTPUT); // Switch signalpin to output. digitalWrite(ultraSoundPin, HIGH); // High pulse on Signal pin. delayMicroseconds(5); // Wait for 5 microseconds. digitalWrite(ultraSoundPin, LOW); // Wait for. pinMode(ultraSoundPin, INPUT); // Switch signalpin to input. echo = pulseIn(ultraSoundPin, HIGH);// Listen for echo, read high pulse. ultrasoundValue = (echo / 58); //convert to CM then to inches. return ultrasoundValue; // return the result of meassure. } void loop() { for (int i=1; i<180;i++){ // loop to move the motor shaft. myservo.write(i); // asing position value to the servomotor. delay(20); // wait 20 ms. distance = meassure(); // call subroutine "meassure()" Serial.print("Angle: "); // sent "Angle: " to the serial monitor. Serial.println(i); // sent position motor shaft to the serial monitor. Serial.print("Meassure: "); // sent "Meassure: " to the serial monitor. Serial.println(ultrasoundValue); // sent the result of meassure to the serial monitor. lcd.setCursor(0,0); // Place the cursor at column 0, row 0. lcd.print(" Object Dist: "); // Write on the screen that is indicated between quotes. lcd.setCursor(9,1); // Place the cursor at column 9, row 1. lcd.print("cms"); // Write on the screen that is indicated between quotes. lcd.setCursor(5,1); // Place the cursor at column 5, row 0. lcd.print(ultrasoundValue); // It is written on the screen indicated between quotes. if (ultrasoundValue < 100) // if variable value is < 100, then clear the 7 column on LCD. { lcd.setCursor(7,1); // Place the cursor at column 7, row 1. lcd.print(" "); // Clear the column 7 } } for (int i=180; i>1;i--){ // loop to move the motor shaft. myservo.write(i); // asing position value to the servomotor. delay(20); // wait 20 ms. distance = meassure(); // call subroutine "meassure()" Serial.print("Angle: "); // sent "Angle: " to the serial monitor. Serial.println(i); // sent position motor shaft to the serial monitor. Serial.print("Meassure: "); // sent "Meassure: " to the serial monitor. Serial.println(ultrasoundValue); // sent the result of meassure to the serial monitor. lcd.setCursor(0,0); // Place the cursor at column 0, row 0. lcd.print(" Object Dist: "); // Write on the screen that is indicated between quotes. lcd.setCursor(9,1); // Place the cursor at column 9, row 1. lcd.print("cms"); // Write on the screen that is indicated between quotes. lcd.setCursor(5,1); // Place the cursor at column 5, row 0. lcd.print(ultrasoundValue); // It is written on the screen indicated between quotes. if (ultrasoundValue < 100) // if variable value is < 100, then clear the 7 column on LCD. { lcd.setCursor(7,1); // Place the cursor at column 7, row 1. lcd.print(" "); // Clear the column 7 } } }