Hello from Slowenia everyone!
I have a problem with displaying the position of servo motor on lcd. Every thing works fine on Serial monitor. Wheh positin goes from 0-180 is fine on LCD but when it goes from 180-1 i have problems with numbers bellow 100 where LCD is displaying 3 dig. number not two. Like this:
100
990
980
.
.
.
Here the code:
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(100);
Serial.println(pos); // waits 15ms for the servo to reach the position
lcd.setCursor(0,1);
lcd.print(pos);
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(100);
Serial.println(pos); // waits 15ms for the servo to reach the position
lcd.setCursor(0,1);
lcd.print(pos);
}
}
This is a common problem with several solutions. You have to 'erase' any three digit number before trying to display a two digit number otherwise part of the previous value remains on the screen.
In my opinion the most straightforward technique is:
One simple solution that sometimes I use is to write zeros at the left of the number, this way I don't need to clear what I wrote before (all the numbers the same length). So, for your case, I will do something like:
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(100);
Serial.println(pos); // waits 15ms for the servo to reach the position
lcd.setCursor(0,1);
if (pos<100) {
lcd.print("0");
}
if (pos<10) {
lcd.print("0");
}
lcd.print(pos);
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(100);
Serial.println(pos); // waits 15ms for the servo to reach the position
lcd.setCursor(0,1);
if (pos<100) {
lcd.print("0");
}
if (pos<10) {
lcd.print("0");
}
lcd.print(pos);
}
}