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.