LCD display repeating

I have some difficulty with my Arudino project.
I have a lcd display and i am trying to display a specific value that updates every time it goes through the loop. But it keeps repeating the value, here is an example: 71 71 71 71 71, but what i want is only 71. Here is the code for the loop:

      for (pos = 0; pos <= 175; pos += 1) { // goes from 0 degrees to 180 degrees
        // in steps of 1 degree
        servol.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
        Serial.println(pos);

        // Send out a sound wave and calculate how far away the nearest object is to it.
        digitalWrite(trigPin, LOW);
        delayMicroseconds(2);

        digitalWrite(trigPin, HIGH);
        delayMicroseconds(10);
        digitalWrite(trigPin, LOW);

        duration = pulseIn(echoPin, HIGH);
        distance = duration * 0.034 / 2;

        //Serial.print("Distance:");
        //Serial.println(distance);
        lcd.setCursor(0, 0);
        lcd.print(distance);
        //lcd.setCursor(0, 1);
        //lcd.print(distance);


        while (distance < 10)
        {
          // If the distance is less than 98 cm, then sound the alarm and make the led flash

          digitalWrite(buzzer, HIGH);
          digitalWrite(ledPin, HIGH);
          delay(delayTime1);//wait for 1ms
          digitalWrite(buzzer, LOW);
          digitalWrite(ledPin, LOW);
          delay(delayTime1);//wait for 1ms


          digitalWrite(buzzer, HIGH);
          digitalWrite(ledPin, HIGH);
          delay(delayTime2);//wait for 2ms
          digitalWrite(buzzer, LOW);
          digitalWrite(ledPin, LOW);
          delay(delayTime2);//wait for 2ms

          // If the B button is pushed, shut the alarm off and disarm it
          if (digitalRead(buttonBpin) == LOW)
          {
            break;
          }


        }

        delay(100);

        // If b button is pressed, disarm the alarm
        if (digitalRead(buttonBpin) == LOW)
        {
          break;
        }

      } //FOR1

Here is the entire code:

#include <Servo.h>
#include <LiquidCrystal.h>
Servo servol;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int pos = 0;
const int trigPin = 8;//Pin to send out sound wave
const int echoPin = 9; //Pin to listen for sound wave coming back

int ledPin = A3;//Pin to turn Led on
int buttonApin = 10;//Pin to arm the alarm
int buttonBpin = A5;//Pin to disarm the alarm
int value_A;//Value of pin A
int value_B;//Value of pin B


int buzzer = A4;//the pin of the active buzzer
int delayTime1 = 250;
int delayTime2 = 250;

long duration; // Travel time obtained from sensor
int distance; // calculated distance of how far away the object is

void setup() {
  servol.attach(6);
  lcd.begin(16, 2);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);
  pinMode(buttonBpin, INPUT_PULLUP);

  pinMode(buzzer, OUTPUT);

  pinMode(12, INPUT);

  Serial.begin(9600);



}

void loop() {

  // Check to see the state of button A and B
  value_A = digitalRead(buttonApin);
  value_B = digitalRead(buttonBpin);

  delay(500);


  if (value_A == LOW)
  {
    //If the alarm is armed, turn the LED on
    digitalWrite(ledPin, HIGH);
    while (ledPin, HIGH)
    {
      for (pos = 0; pos <= 175; pos += 1) { // goes from 0 degrees to 180 degrees
        // in steps of 1 degree
        servol.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
        Serial.println(pos);

        // Send out a sound wave and calculate how far away the nearest object is to it.
        digitalWrite(trigPin, LOW);
        delayMicroseconds(2);

        digitalWrite(trigPin, HIGH);
        delayMicroseconds(10);
        digitalWrite(trigPin, LOW);

        duration = pulseIn(echoPin, HIGH);
        distance = duration * 0.034 / 2;

        //Serial.print("Distance:");
        //Serial.println(distance);
        lcd.setCursor(0, 0);
        lcd.print(distance);
        //lcd.setCursor(0, 1);
        //lcd.print(distance);


        while (distance < 10)
        {
          // If the distance is less than 98 cm, then sound the alarm and make the led flash

          digitalWrite(buzzer, HIGH);
          digitalWrite(ledPin, HIGH);
          delay(delayTime1);//wait for 1ms
          digitalWrite(buzzer, LOW);
          digitalWrite(ledPin, LOW);
          delay(delayTime1);//wait for 1ms


          digitalWrite(buzzer, HIGH);
          digitalWrite(ledPin, HIGH);
          delay(delayTime2);//wait for 2ms
          digitalWrite(buzzer, LOW);
          digitalWrite(ledPin, LOW);
          delay(delayTime2);//wait for 2ms

          // If the B button is pushed, shut the alarm off and disarm it
          if (digitalRead(buttonBpin) == LOW)
          {
            break;
          }


        }

        delay(100);

        // If b button is pressed, disarm the alarm
        if (digitalRead(buttonBpin) == LOW)
        {
          break;
        }

      } //FOR1

      for (pos = 175; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
        servol.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
        Serial.println(pos);

        // Send out a sound wave and calculate how far away the nearest object is to it.
        digitalWrite(trigPin, LOW);
        delayMicroseconds(2);

        digitalWrite(trigPin, HIGH);
        delayMicroseconds(10);
        digitalWrite(trigPin, LOW);

        duration = pulseIn(echoPin, HIGH);
        distance = duration * 0.034 / 2;

        Serial.print("Distance:");
        Serial.println(distance);

        while (distance < 10)
        {
          // If the distance is less than 98 cm, then sound the alarm and make the led flash

          digitalWrite(buzzer, HIGH);
          digitalWrite(ledPin, HIGH);
          delay(delayTime1);//wait for 1ms
          digitalWrite(buzzer, LOW);
          digitalWrite(ledPin, LOW);
          delay(delayTime1);//wait for 1ms


          digitalWrite(buzzer, HIGH);
          digitalWrite(ledPin, HIGH);
          delay(delayTime2);//wait for 2ms
          digitalWrite(buzzer, LOW);
          digitalWrite(ledPin, LOW);
          delay(delayTime2);//wait for 2ms

          // If the B button is pushed, shut the alarm off and disarm it
          if (digitalRead(buttonBpin) == LOW)
          {
            break;
          }


        }

        delay(100);

        // If b button is pressed, disarm the alarm
        if (digitalRead(buttonBpin) == LOW)
        {
          break;
        }

      } //FOR2




    } //WHILE


  }
  // If the alarm is disarmed, turn the LED off
  if (value_B == LOW)
  {
    digitalWrite(ledPin, LOW);
  }





}


Please copy-paste the actual sketch and use the </> icon.

No one want to look at screenshots of text. Especially when they do not contain the whole program.

David.

Ok, thanks for the tips!

It is also wise to press ctrl-T when writing your code. This will ensure that blocks get indented nicely and your code is easy to read.

It also shows you how the code works. e.g. which logical block you are in.
Search for the Serial.print statements and the lcd.print statements.

You will see why they are repeated.

Oh, it is a good idea (tm) to say when you have edited a prior message.
You can tidy up message#1 e.g. format with ctrl-T and replace the messy code.
Just say say "I have updated #1". Because this horrible new Forum software does not show readers (like the old Forum did).

David.

Like this, and where should i type "I have updated #1". Also i couldn't find the reason it was repeated, i am new to programming so everything is not that obvious for me..

You have done fine. message#5 will show up as a new message. Readers will see that you have updated #1.

Regarding the repeat.

        while (ledPin, HIGH)
        {
            for (pos = 0; pos <= 175; pos += 1) { // goes from 0 degrees to 180 degrees
                ...
                Serial.println(pos);
                ...
                lcd.setCursor(0, 0);
                lcd.print(distance);
                ...
            }
            ...
        } //FOR1

that for() loop will Serial.println(pos) 176 times
it also lcd.print(distance) 176 times but since you re-set the cursor you do not notice.

You can see a similar repeat in the FOR2 block.

Personally, I try to keep blocks short and sweet. Preferably all on one screen. Otherwise it is difficult to follow the logic.

If I have a problem, I print that section of program on real paper. Then I can study it with a nice cup of tea (and a biscuit).
Most importantly, I can draw arrows on the paper with a pencil.

David.

Yeah, i know it will repeat it 176 times. That is because it is a value i want to update, I don't want it to keep writing on the lcd display.. I want the distance value to overwrite the previous value so the lcd display prints for example 71 and not 7171717171. Either way i appreciate the help you have given me, thanks!

There are a number of rules to printing to an LCD. You have apparently followed some but not all of them.

  1. Do not use lcd.clear unless you wish to write something entirely different to the previous. In particular, you generally do not want to use it in loop().
  2. Do not re-write a value unless it actually differs from the previous. Check it against the previous value.
  3. Do not update the display more than five times per second. You cannot read that fast. :grin:
  4. Use cursor positioning to write a new value in the same place. Know how long the new value is and add spaces (before or after) to make it the same length as previous.
  5. Do not use lcd.println.

Ok, thanks a lot. There was a lot of this that i didn't know, i really appreciate it!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.