// YWROBOT
// Compatible with the Arduino IDE 1.0
// Library version:1.1
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
// set the LCD address to 0x27 for a 16 chars and 2 line display
float cm_gauche,cm_droite;
float ratio;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns
// the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.print("--> Distance <--");
delay(1000);
lcd.clear();
}
void loop()
{
ratio = 9.0/400;
cm_gauche = 0.0342/2 * readUltrasonicDistance(5, 4);
cm_droite = 0.0342/2 * readUltrasonicDistance(3, 2);
int croix_gauche = cm_gauche*ratio;
int croix_droite = cm_droite*ratio;
// Gauche
lcd.setCursor(0,0);
lcd.print(cm_gauche, 0);
for (int i=0;i<=croix_gauche;i++)
{
lcd.setCursor(7+i,0);
lcd.print("x");
}
for (int i=0;i<=croix_droite;i++)
{
lcd.setCursor(7+i,1);
lcd.print("x");
}
// Droite
lcd.setCursor(0,1);
lcd.print(cm_droite, 0);
lcd.setCursor(4,0);
lcd.print("cm");
lcd.setCursor(4,1);
lcd.print("cm");
delay(500);
lcd.clear();
}