I am doing fish feeder project with level detection for the feeds but lcd display and ultrasonic sensor did not function.
#include <LiquidCrystal.h>
#define trigPin 12 // the trig pin to digital pin 12
#define echoPin 13 // the Echo pin to digital pin 13
#define L 8
#define M 9
#define H 10
// #define trigmotor 1 //closelid
int led = A1; // LED indicator to digital pin A1
#include <Servo.h>
Servo MyServo;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
const int thresholdH = 0;
const int thresholdM = 70;
const int thresholdL = 90;
// defines variables
long duration;
int distance;
float percentLV;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600); // Starts the serial communication
pinMode(led,OUTPUT);
digitalWrite(led,LOW); //set the led off(initial condition)
lcd.begin(16, 2);
}
void loop()
{
int degreesOfRotation = 8;
int servoPinNumber = 11;
float hoursBetweenMeals = 12;
float mealDelayTime = 0;
{
MyServo.attach(11);
MyServo.write(degreesOfRotation);
mealDelayTime = hoursBetweenMeals * 1800000;
delay(20);
while(degreesOfRotation < 152) {
MyServo.write(degreesOfRotation);
delay(10);
degreesOfRotation += 1;
}
while(degreesOfRotation > 8) {
MyServo.write(degreesOfRotation);
delay(10);
degreesOfRotation -= 1;
}
delay(mealDelayTime);
}
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = (duration * 0.034 / 2);
float percentLV = (40 - ( distance - 16 )) * 100 / 40;
if (percentLV > thresholdL && percentLV < 100) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PLEASE REFILL!");
lcd.setCursor(0, 1);
//lcd.print(distance);
lcd.print(percentLV);
lcd.print(" %");
digitalWrite(H, HIGH);
digitalWrite(M, LOW);
digitalWrite(L, LOW);
//digitalWrite(trigmotor, HIGH);
//delay(3000);
//digitalWrite(trigmotor, LOW);
//delay(1000);
digitalWrite (led, HIGH); // Turn on the led indicator
delay(20);
} else if (percentLV <= thresholdL && percentLV > thresholdM) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("U May Refill Now");
lcd.setCursor(0, 1);
//lcd.print(distance);
lcd.print(percentLV);
lcd.print(" %");
digitalWrite(H, LOW);
digitalWrite(M, HIGH);
digitalWrite(L, LOW);
//digitalWrite(trigmotor, LOW);
digitalWrite (led, HIGH); // Turn on the led indicator
delay(20);
} else if (percentLV <= thresholdM && percentLV > thresholdH) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Take it easy");
lcd.setCursor(0, 1);
//lcd.print(distance);
lcd.print(percentLV);
lcd.print(" %");
digitalWrite(H, LOW);
digitalWrite(M, LOW);
digitalWrite(L, HIGH);
//digitalWrite(trigmotor, LOW);
digitalWrite (led, LOW); // Turn off the led indicator
delay(20);
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("FULL!!");
lcd.setCursor(0, 1);
//lcd.print(distance);
lcd.print(percentLV);
lcd.print(" %");
//digitalWrite(trigmotor, LOW);
digitalWrite (led, LOW); // Turn off the led indicator
delay(20);
Serial.print("PercentLV: ");
Serial.print(percentLV);
Serial.println("%");
}
delay(1000);
}
}