#include <LiquidCrystal_I2C.h> // if you don´t have I2C version of the display, use LiquidCrystal.h library instead
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Left Mid Right");
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
int dist;
lcd.setCursor(0,1); lcd.print(" ");
// delay(1000);
dist = check_distance(0); delay(1000);
lcd.setCursor(0,1); lcd.print(dist); delay(1000);
dist = check_distance(90); delay(500);
lcd.setCursor(7,1); lcd.print(dist); delay(1000);
dist = check_distance(180); delay(500);
lcd.setCursor(14,1); lcd.print(dist); delay(2000);
}
int check_distance (int angle) {
// simulate a servo that rotates to an angle and
// distance sensor (actually random number)
// which measures distance
myservo.write(angle); // tell servo to go to position in variable 'pos'
delay(150);
int distance = random(4,20);
delay(250);
return(distance);
}