// http://forum.arduino.cc/index.php?topic=587119
//26 dec 2018
//you_know
//lcd bar graph
// prints X's depending of proportion of actual vs max
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int maxDist = 7000; // <<<<<<< set this
int actualDist;
float howManyX; //the exact proportion as a float
int howManyXint; //proportion rounded as an int
void setup()
{
Serial.begin(9600);
Serial.println("lcd bar graph");
Serial.print("Created: ");
Serial.print(__TIME__);
Serial.print(", ");
Serial.println(__DATE__);
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.setCursor(0, 0); //move along and down
lcd.print(" Bar");
lcd.setCursor(0, 1); //move along and down
lcd.print(" Graph");
delay(1000);
lcd.clear();
} //setup
void loop()
{
lcd.setCursor(0, 0); //move along and down
//get a random distance between 0 and maxDist
actualDist = random(0, maxDist + 1);
howManyX = 16.0 * actualDist / maxDist; //exact proportion
howManyXint = round(howManyX); //actual X's
//top line
lcd.print(actualDist);
lcd.print(" ");
lcd.print(howManyX);
lcd.print(" ");
lcd.print(howManyXint);
//bottom line
lcd.setCursor(0, 1); //move along and down
for (int i = 0; i < howManyXint; i++)
{
lcd.print("X");
}
delay(1000);
lcd.clear();
} //loop
where there is random i want it to take reading from ultrasonic sensor how do that ultrasonic is attached to pin 4