lcd.setCursor(0,1) makes wrong value

Hello everybody! Can you please help me with my code? I attach a copy below.
So I am trying to make an Automatic Water Pump with a water level indicator with a schematic like in this picture.

#define trigPin 6
#define echoPin 7
#define motorPin 0
#define buzzer 8
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorPin, OUTPUT);
pinMode(buzzer, OUTPUT);
}

void myTone(byte pin, uint16_t frequency, uint16_t duration){ 
  unsigned long startTime=millis();
  unsigned long halfPeriod= 1000000L/frequency/2;
  pinMode(pin,OUTPUT);
  while (millis()-startTime< duration)
  {
    digitalWrite(pin,HIGH);
    delayMicroseconds(halfPeriod);
    digitalWrite(pin,LOW);
    delayMicroseconds(halfPeriod);
  }
  pinMode(pin,INPUT);
}

void loop() {
long duration, distance;
float capacity=0.0;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance=(duration/2)/29.1;
capacity=(1-(distance-30)/300.0)*100.0;

lcd.setCursor(0,0);
if(capacity>100){
  lcd.print("OVERFLOW!!!     ");
      for (unsigned i=400; i<1600; i++){
    myTone(buzzer, i, 1);
} 
}
if(capacity<0){
  lcd.print("ERROR!!!        ");
  myTone(buzzer, 370, 500);
  myTone(buzzer, 466, 500);
  myTone(buzzer, 554, 500);
  myTone(buzzer, 740, 1000);
}
else{
  lcd.print("STATUS: ");
  lcd.print(capacity);
  lcd.print("%   ");
  lcd.setCursor(0,1); ///Causing Overflow
  lcd.print("Test");
}
if(capacity<=20){
  digitalWrite(motorPin, HIGH);
}
if(capacity>=97){
  digitalWrite(motorPin, LOW);
}
delay(100);
}
 

But when i put the line

 lcd.setCursor(0,1);

and I put object in the range of the ultrasonic sensor,
The value of capacity becomes 110% and in LCD shows text "OVERFLOW" which it should not. If I remove that line, the program works fine.

Any help is appreciated. Thanks.

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.

It will help you get the best out of the forum in the future.

Hi, @riki17
Have you got code that JUST has the ultrasonic and LCD in it.
If you wrote this code in stages, you should have;

  • code just for the ultrasonic
  • code just for the LCD
  • code just for the motor control
  • code just for the buzzer

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Hi,

Is this a Tinkercad or other simulation, or are you using a real Arduino and other real components?

If it's a simulation, then we can't really help here, you need to go ask the forum for whatever software you are using.

If it's a real Arduino, I can't think of any reason why removing that line would cause the effect you describe. It seems more likely something else is causing the problem to come and go and it just seems like it's happening when you remove that line.

I am using Tinkercad

Sorry, I am new to Arduino Program. Could you please explain to me how to separate the code for each component that I am using?

I already tried to separate the conditional code for each component.

#define trigPin 6
#define echoPin 7
#define motorPin 0
#define buzzer 8
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorPin, OUTPUT);
pinMode(buzzer, OUTPUT);
}


void myTone(byte pin, uint16_t frequency, uint16_t duration){ 
  unsigned long startTime=millis();
  unsigned long halfPeriod= 1000000L/frequency/2;
  pinMode(pin,OUTPUT);
  while (millis()-startTime< duration)
  {
    digitalWrite(pin,HIGH);
    delayMicroseconds(halfPeriod);
    digitalWrite(pin,LOW);
    delayMicroseconds(halfPeriod);
  }
  pinMode(pin,INPUT);
}

void loop() {
long duration, distance;
float capacity=0.0;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance=(duration/2)/29.1;
capacity=(1-(distance-30)/300.0)*100.0;


if(capacity>100){
  lcd.setCursor(0,0);
  lcd.print("OVERFLOW!!!     ");
} 
  
else if(capacity<0){
  lcd.setCursor(0,0);
  lcd.print("ERROR!!!        ");
}

else{
  lcd.setCursor(0,0);
  lcd.print("STATUS: ");
  lcd.print(capacity);
  lcd.print("%   ");
  lcd.setCursor(0,1); ///Causing Overflow
  lcd.print("Test");
}
  
if(capacity>100){
  for (unsigned i=400; i<1600; i++){
    myTone(buzzer, i, 1);
  }
}
  
else if(capacity<0){
  myTone(buzzer, 370, 500);
  myTone(buzzer, 466, 500);
  myTone(buzzer, 554, 500);
  myTone(buzzer, 740, 1000);
}
    
if(capacity<=20){
  digitalWrite(motorPin, HIGH);
}
else if(capacity>=97){
  digitalWrite(motorPin, LOW);
}
delay(100);
}
 

Its outcome is still the same. But if I remove this part

if(capacity>100){
  for (unsigned i=400; i<1600; i++){
    myTone(buzzer, i, 1);
  }
}
  
else if(capacity<0){
  myTone(buzzer, 370, 500);
  myTone(buzzer, 466, 500);
  myTone(buzzer, 554, 500);
  myTone(buzzer, 740, 1000);
}

it works just like I wanted but, I need the buzzer work as well. is this what you meant? If not, please tell me what to do.

If you have to do that, then you're doing it backwards.
As you develop a project you should write code to make each component work on its own first. Then, once you have good working code for each component you can begin to integrate the code piece by piece.

If you write a bunch of code that takes into account every concept and something goes wrong, where is the problem? Your guess is as good as mine.

However, if you write one line of code or one simple concept at a time and it works, then you can move on. If it doesn't work, then your mistake is almost certainly right there in the last line of code or the last simple concept.

I suspect the strange effect you saw is a bug in Tinkercad. Try it on a real Arduino, LCD, sensor etc and let us know if you still get the effect.

1 Like

Thank you for the advice. But, right now I don't have Arduino Uno, so I am trying to simulate it here. One day, if I have it, I will give it a try.

Don't buy an Uno if you want to build circuits on breadboard. I know most tutorials show Uno used with breadboard, but it is a stupid idea. If you want to buy a "shield" PCB to fit on the top of an Uno, then get an Uno. If you want to build circuits on breadboard, get an Arduino that is breadboard compatible like a Nano 3, Pro Mini or Pro Micro.

1 Like