I'm working on automatic planting system where the a pump will supply water when the humidity of the sand are low. In this project, I'm also using ultrasonic sensor to control the water inside the tank. Once the tank already reach its minimum water level, a buzzer will ring. However, the system seems only can running properly (with delay) in a few minutes. After that, the system suddenly has no delay which make the serial monitor prints the output too fast (without delay). It's that the problem coming from the arduino board itself of my codes?
Here is the code:
int water; //random variable
const int TRIG_PIN = 9; // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN = 10; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int BUZZER_PIN = 13; // Arduino pin connected to Piezo Buzzer's pin
const int DISTANCE_THRESHOLD = 20; // centimeters
float duration_us, distance_cm;
void setup(){
Serial.begin(9600);
pinMode(3,OUTPUT); //output pin for relay board, this will sent signal to the relay
pinMode(6,INPUT); //input pin coming from soil sensor
pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
water = digitalRead(6); // reading the coming signal from the soil sensor
Serial.print(F("Water Level = "));
Serial.println(water);
if(water == 10) // if water level is full then cut the relay
{
digitalWrite(3,LOW); // low is to cut the relay
}
else
{
digitalWrite(3,LOW); //high to continue proving signal and water supply
}
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin
duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance
distance_cm = 0.017 * duration_us;
Serial.print("distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
if(distance_cm > DISTANCE_THRESHOLD)
{
digitalWrite(BUZZER_PIN, HIGH);
delay(800);
digitalWrite(BUZZER_PIN, LOW);
}
else
{ digitalWrite(BUZZER_PIN, LOW); // turn off Piezo Buzzer
}
delay(800);
}