Timer displayed on LCD not working properly (repeating)

Hello all,

I am trying to have a soap dispenser that displays a timer when the user gets soap (soap is dispensed via a servo pulling down on soap). Currently my LCD displays the timer repeated many times on the LCD instead of where I have commanded it to with lcd.setCursor(0,0). Any advice on how to fix this or different approach?

My ultimate goal is to have the ultrasonic sensor trigger the servo, have a delay for user to get soap, strat timer on LCD and after 30 seconds the display to clear and display “Thank you”. Then the code would be ready to run again if the ultrasonic sensor detects another hand,


// defines pins numbers
const int servo = 9;
const int trigPin = 10;
const int echoPin = 11;
const int rs = 1, en = 2, d4 = 4, d5 = 5, d6 = 6, d7 = 7;

// defines variables
long duration;
int distance;
int StartTimer;
int Timing;
int TimeStart;
int Timer;
int TimeNow;

#include <Servo.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Servo myservo;  // servo object to control a servo
int pos = 0;    // stores the servo position
char timeline[16];

void setup() {
  lcd.begin(16, 2);
  lcd.print("Stop The Spread");
  lcd.setCursor(0, 1);
  lcd.print("Wash Your Hands");
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  myservo.attach(servo);  // attaches the servo on pin 9 to the servo object
  myservo.write(0);   // Sets Servo to initially 0 degrees
  Serial.begin(9600); // Starts the serial communication

void loop() {
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH); // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
  distance = duration * 0.034 / 2; // Calculating the distance
  Serial.print("Distance: "); // Prints the distance on the Serial Monitor

  if (distance < 10) { //Check distance is less than 10cm
    myservo.write(0); // Reset the servo to 0 Degrees

    StartTimer = 1;
    Timing = 1;

    while (Timing == 1)
      if (StartTimer)
        TimeStart = millis(); // capture time at start of timer
        StartTimer = 0;
        TimeNow = millis();
        Timer = (TimeNow - TimeStart) / 1000; // diff in start time and now is time for timer
        lcd.setCursor(0, 0);
        sprintf(timeline, "%0.2d Secs", Timer);
      if (Timer > 30.0) {
        Timing = 0;

Do post an image of what you see on the LCD. From the code I don't see anything obviously wrong, or causing the time to display multiple times.