Help with project

I am very new to Arduino and programming languages in general. I have taken two sketches that I found and tried to put them together to make one. I am having trouble getting the results I want.

I have taken a HC-SR04 and a 16x2 lcd screen and written/modified a sketch to display the distance measured by the sensor on the lcd. I have tried the sketches individually and they work fine. The sensor would display the correct values on the serial monitor. What I did was swap the serial monitor for the lcd.

I can get values to display, but they don't seem correct. At first, I included code for the serial monitor and the lcd. The values displayed on them were different. I took out the serial.print code thinking that it was slowing down the sensor, but it didn't change anything.

The other problem is that when the values are displayed on the lcd, there are always 3 numbers (009, 025, etc.).

I would like for someone to take a look at my sketch and get me a clue as to what I might be doing wrong. I learn best by reverse engineering things and seeing what makes them work.

I know this is a silly little project, but I am really interested in Arduino and this is a stepping stone to some other projects I have in mind.

Thanks for the feedback and help!

Sonar_test_with_lcd_output.ino (1.04 KB)

The only thing that I can think of is that maybe the LiquidCrystal_I2C library doesn't handle whatever the return type from ping_cm() is. Why that should be anything other than int, I can't imagine, but, IIRC, it isn't.

Try storing the value in an int, and printing the int.