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