So basically what I'm trying to do is display how far an object is to an ultrasonic sensor in cm. It works, but it always displays 4 digits so it makes 4 cm look like 4000. So how can I make it so that it displays 1 digit until 9, and also limit it to 99? Here's the code:
// include the library code:
#include <LiquidCrystal.h>
int trigPin = 2;
int echoPin = 5;
int distance;
int time;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.begin(16, 2);
}
void loop() {
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
time = pulseIn (echoPin, HIGH);
distance = (time * 0.034) / 2;
// set the cursor to column 0, line 1
lcd.setCursor(0, 0);
lcd.print("distanceCM ");
lcd.print((int) (distance));
}
If you want to limit a variable, use the constrain() function.
After you print the distance, you will have to print some trailing spaces or previous (larger) numbers will remain on the lcd. For example, you prinnt "1000" and then the next time you print "999" your display will look like "9990"
econjack:
Is this all of your code? If so, where's pulseIn()?
What happens if you to this:
lcd.print( distance / 1000);
This is the only time I use pulseIn: time = pulseIn (echoPin, HIGH); It seems like what happens is that when it doesn't have something in front of the sensor it will give some random negative number, then if you put something in front it will replace the old number with the new one and leave the rest. For example if it doesn't have something in front of it it might display -208 then if I put something 4 cm in front of the sensor it will display 408.
// C R
lcd.setCursor(0, 0);
// 111111
// 0123456789012345
lcd.print("distanceCM ");
. . .
// C R
lcd.setCursor(11,0);
// 11111
// 12345
lcd.print(" "); //clear out old number
lcd.setCursor(11,0);
lcd.print((int) (distance));""
Adam777:
This is the only time I use pulseIn: time = pulseIn (echoPin, HIGH); It seems like what happens is that when it doesn't have something in front of the sensor it will give some random negative number, then if you put something in front it will replace the old number with the new one and leave the rest. For example if it doesn't have something in front of it it might display -208 then if I put something 4 cm in front of the sensor it will display 408.
The point of my question is that your posted code does not show the function. If that's the case, did it come from a library that you included in your program? As Larryd pointed out, using lcd.clear is an H-bomb to kill an ant. He shows you a simple way to fix it.
PulseIn is a built-in Arduino function. No extra library required.
econjack:
The point of my question is that your posted code does not show the function. If that's the case, did it come from a library that you included in your program? As Larryd pointed out, using lcd.clear is an H-bomb to kill an ant. He shows you a simple way to fix it.
PulseIn is a built-in Arduino function. No extra library required.