Displaying HC-SR04 Ultrasonic Sensor output on HD44780 type LCD (solved)

Hi, I am having difficulty getting an untrasonic sensor reading (# in cm) to display on an lcd. I am using the following setup:

  • Arduino Uno, with a protoshield attached
  • Microtivity 2x16 LCD (HD44780 type) -- using LiquidCrystalDisplay.h
  • HC-SR04 Ultrasonic Sensor -- using NewPing.h

I have tested a number of things which work fine:

  • Sending messages from the Serial Monitor to the LCD
  • Sending distances from the ultrasonic sensor to the Serial monitor
  • Doing both of these in the same sketch

These tests have convinced me that both pieces of hardware are working properly and that my problem is in my code.

When I take my value from the the ultrasonic sensor and display it on the lcd, instead of displaying it as a number it displays it as the ascii encoding of that number ( as found in this resource: http://www.asciitable.com/index/asciifull.gif ) For example, if the ultrasonic sensor is measuring 60 cm, the Serial Monitor displays "Distance: 60cm" but the LCD displays "Distance: <cm".

Here is the sketch I am using:

#include <NewPing.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
NewPing sonar(6, 7, 200);   // NewPing setup of pins and maximum distance.

int DistOriginal;
char DistChar;

void setup()
{
  Serial.begin(115200);    // Open serial monitor at 115200 baud to see ping results.
  lcd.begin(2,20);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Testing.... (V6)");
}

void loop()
{
  delay(2000);                          // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.  
  unsigned int uS = sonar.ping();     // Send ping, get ping time in microseconds (uS).
  
  //Set values to display in Serial Monitor and LCD
  DistOriginal = (uS / US_ROUNDTRIP_CM);
  DistChar = (uS / US_ROUNDTRIP_CM);

  Serial.print("DO: ");
  Serial.print(DistOriginal); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)  
  Serial.print(":DC:");
  Serial.print(DistChar);
  Serial.println("cm");

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.write("DO: ");
  lcd.write(DistOriginal);
  lcd.write(":DC:");
  lcd.write(DistChar);
  lcd.write("cm");
}

I tried various conversions that I found online, but none seem to solve the issue.

Any help is appreciated

 DistChar = (uS / US_ROUNDTRIP_CM);

"DistChar" is a "char"
Make it an "int"

and use lcd.print() instead of lcd.write?

Thank you AWOL and groundfungus!

AWOL, I had one calculation going to an int: DistOriginal and another going to a char:DistChar and neither were working.

groundfungus, your solution did the trick for me. I did some reading on the differences between lcd.print and lcd.write and I can't say that I understand them completely, but I do know now that I should use .print instead of .write. Thanks!!

why my lcd doesn't display?
this are the coding that i have used:

#include <RCSwitch.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
RCSwitch mySwitch = RCSwitch();

float level;
int Hval=2;
int Lval=12;
int BUZZER = 10 ;

void setup() {
mySwitch.enableReceive(0);
lcd.begin(16, 2);
lcd.print("WATER LEVEL INDI");

pinMode(BUZZER,OUTPUT);
}

void loop() {
if (mySwitch.available()) {

level = mySwitch.getReceivedValue();

level = level/(Lval - Hval);
level = level*100;

if (level>100) {level=100;}

lcd.setCursor(0, 1);
lcd.print("DISTANCE ");
lcd.print(level);
lcd.print(" cm ");

mySwitch.resetAvailable();

}
digitalWrite(BUZZER,HIGH);
if (level > 99) {digitalWrite(BUZZER,HIGH); delay(100); digitalWrite(BUZZER,LOW); delay(100);}
else {digitalWrite(BUZZER,LOW);}
}