i have this code i need help to modify it

// 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

Have you tried googling and looking at one of the gajillion tutorials/examples about ultrasonic sensors? My magic 8-ball tells me that's the root of the problem.

Give it a go...

// 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);

//#define IMPERIAL    1
#define METRIC      1

int maxDist = 7000; // <<<<<<< set this
float actualDist;
float howManyX;     //the exact proportion as a float
int   howManyXint;  //proportion rounded as an int

//typical pin assignments; change to suit your implementation
const int trigPin = 9;
const int echoPin = 10;

void setup()
{
    Serial.begin(9600);
    Serial.println("lcd bar graph");
    Serial.print("Created: ");
    Serial.print(__TIME__);
    Serial.print(", ");
    Serial.println(__DATE__);

    //init ultrasound pins
    pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPin, INPUT); // Sets the echoPin as an Input
    
    // 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 an actual distance between 0 and maxDist
    actualDist = ReadUltrasound();

    //NB: actualDist will be zero if nothing in the time-out field...
    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

float ReadUltrasound( void )
{
    unsigned long
        echotime;
        
    //pulse trigger pin L-H-L
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    
    //determine the travel time of the echo (uS)
    //limit the distance to something reasonable
    //like, say, 2m
    //  at 343m/s, 2x2m (there and back) requires
    //  11660uS
    echotime = pulseIn( echoPin, HIGH, 11660L );
    
    //sound travels in air at about 343 meters/second
    //or about 0.0343 cm/uS. The echo time describes the
    //time for the sound pulse to travel to and return
    //from the object being sensed. We want half of this
    //value (i.e. distance to, not distance to and back)
    //so we multiply uS by 0.01715

#ifdef  METRIC
    //return cm
    return( echotime * 0.01715 );
#endif

#ifdef  IMPERIAL
    //return inches
    return( echotime * 0.006752 );  // 0.01715 / 2.54 cm/in
#endif

}//ReadUltrasound

random() is a builtin function. random() - Arduino Reference

Presumably the use of random() was just to test the bar graph works in the absence of an actual sensor.

It shows the importance and usefulness of using functions: random() just acts a sort of black-box placeholder for an actual function such as Blackfin's ReadUltrasound().

And if OP comes up with a better way than Blackfin's just call it ReadUltrasoundNew() and keep the old one there for testing and comparison.

i want it to take reading from ultrasonic sensor how do that ultrasonic is attached to pin 4

I'm wondering though if OP actually tried to read his or her sensor and just get a reading to display in serial monitor, regardless of the bar graph side of things?

Willpatel_Kendmirez:
Presumably the use of random() was just to test the bar graph works in the absence of an actual sensor.

It shows the importance and usefulness of using functions: random() just acts a sort of black-box placeholder for an actual function such as Blackfin's ReadUltrasound().

And if OP comes up with a better way than Blackfin's just call it ReadUltrasoundNew() and keep the old one there for testing and comparison.

I'm wondering though if OP actually tried to read his or her sensor and just get a reading to display in serial monitor, regardless of the bar graph side of things?

yeah i took reading from ultrasonic but i cant just combine code i used a website called circuito io to test but how do i make to take it reading from sensor just writing sensor reading something instead of random won work tho

Well seems that Blackfin has a way: s/he wrote a function to read the ultrasound which just gives you the real value not the random test value.

Then your existing code just uses that real value to produce a line of XXXXs

Blackfin:
Give it a go...

// 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);

//#define IMPERIAL    1
#define METRIC      1

int maxDist = 7000; // <<<<<<< set this
float actualDist;
float howManyX;    //the exact proportion as a float
int  howManyXint;  //proportion rounded as an int

//typical pin assignments; change to suit your implementation
const int trigPin = 9;
const int echoPin = 10;

void setup()
{
    Serial.begin(9600);
    Serial.println("lcd bar graph");
    Serial.print("Created: ");
    Serial.print(TIME);
    Serial.print(", ");
    Serial.println(DATE);

//init ultrasound pins
    pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPin, INPUT); // Sets the echoPin as an Input
   
    // 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 an actual distance between 0 and maxDist
    actualDist = ReadUltrasound();

//NB: actualDist will be zero if nothing in the time-out field...
    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

float ReadUltrasound( void )
{
    unsigned long
        echotime;
       
    //pulse trigger pin L-H-L
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
   
    //determine the travel time of the echo (uS)
    //limit the distance to something reasonable
    //like, say, 2m
    //  at 343m/s, 2x2m (there and back) requires
    //  11660uS
    echotime = pulseIn( echoPin, HIGH, 11660L );
   
    //sound travels in air at about 343 meters/second
    //or about 0.0343 cm/uS. The echo time describes the
    //time for the sound pulse to travel to and return
    //from the object being sensed. We want half of this
    //value (i.e. distance to, not distance to and back)
    //so we multiply uS by 0.01715

#ifdef  METRIC
    //return cm
    return( echotime * 0.01715 );
#endif

#ifdef  IMPERIAL
    //return inches
    return( echotime * 0.006752 );  // 0.01715 / 2.54 cm/in
#endif

}//ReadUltrasound

hey i gave it a try however it is only showing 0.00 after it displayed bargraph and yes ultrasonic sensor i tested it separately after that 0.00

NINZJAZZ:
yes ultrasonic sensor i tested it separately after that 0.00

What does that even mean? Do you mean you have a separate sketch in which you tested the sensor and it gives the correct value?

If so, replace Blabkfin's sensor function with your code (as a function) if you know yours works.

edit:
Are you using the correct pin numbers by the way?

NINZJAZZ:
hey i gave it a try however it is only showing 0.00 after it displayed bargraph and yes ultrasonic sensor i tested it separately after that 0.00

How far away is the object? pulseIn() returns zero (0) if the timeout value is exceeded while waiting.

I guessed at a limit of 11660uS which is ~2m away.

Did you verify that the pins I arbitrarily assigned for trigger and echo match yours:

//typical pin assignments; change to suit your implementation
const int trigPin = 9;
const int echoPin = 10;

?

You also need to set maxDist, since that's how it works out the proportion of how many of 16 X's to show. If it can never go over 2000 but you leave maxDist as 7000 you'll get more than 2/7 of 16 which is about 5.