How to "print" sensor data

Hi ! I was wondering how would I use print command with a 1602 display AND an HC-SR04 , so the distance would be written on the display (it is one of those 1602s that use 2 pins on arduino , use print command instead of binary) .
I had 2 ideas and was wondering which was better :
1- (idk even if it is possible) put an analogRead inside the print command .
2- write down sensor data on serial then read from serial and print to screen .

And I'd really appreciate a little sample code ?
Thanks ! :slight_smile:

I don't understand part 2 of your question, but for part 1 you could certainly put an analogRead in the print like this:

lcd.print(analogRead(pinX));

But surely when using the ultrasonic sensor you already did all the reading and calculation with the trig and echo pins and have the distance safely stored in a variable by the time you want to show it, so you just go:

lcd.print(distanceToTarget);

Or did I miss something.....

I suppose using the variable might be easier .
#include <NewPing.h>

#define TRIGGER_PIN 7 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 8 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int temp=0;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
while (!Serial) ;
}

void loop() {
delay(100); // Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping() / US_ROUNDTRIP_CM; // Send ping, get ping time in microseconds (uS).
if (uS != temp)
{
temp = uS;
Serial.print("The distance is : ");
Serial.print(temp); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}
}

I use this sample code :?