Auto Refilling Water Tank with Solenoid Valve and Ultrasonic Sensor

Hi I'm new user. I want to make an automate refilling water tank using ultrasonic sensor HC SR04 to defined the distance and using solenoid valve as an actuator. I want to use millis but i still confused how to used it. Here's I attached the code
'
const int trigPin = 4;
const int echoPin = 5;
const int waterRelayPin = 8;

unsigned long duration, distance;
unsigned long currentMillis = millis();
unsigned long distanceMillis;

bool Flag = false;

void setup()
{
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);

pinMode(waterRelayPin, OUTPUT);

pinMode(echoPin, INPUT);
Serial.begin(9600);
}

void loop()
{
if (millis() - distanceMillis > 500)
{
distanceMillis = millis();

Distance();

}
if(Flag == false && distance <= 30)
{
Flag = true;
Serial.println("Water Pump : ON");
digitalWrite(waterRelayPin, LOW);
previousMillis = millis();
}

else
{
Flag = false;
Serial.println("Water Pump : OFF");
digitalWrite(waterRelayPin, HIGH);
}
}

void Distance()
{
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
int percentage = map(distance, 24, 6, 0, 100);

if(percentage < 0){
percentage = 0;
}
else if(percentage > 100){
percentage = 100;
}

Serial.print("Water Level: ");
Serial.println(percentage);

}'

I moved your topic to an appropriate forum category @anon11537946.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

Hi @anon11537946

welcome to the arduino-forum.
You should re-edit your first posting using this method.
Scroll down to the section RE-editing a posting

If you have re-edited your posting that the code is shown as a code-section
I will answer your questions
best regards Stefan

You are one of more than a thousand users that have this problem.

This problem is caused by three things:

  1. not emphasizsing the very different nature of non-blocking timing based on millis() to using delay()
  2. a poor documented and poor written demo-code
  3. lack of an everyday analogy with easy to understand example-numbers

I claim to have written a tutorial that does it better.

best regards Stefan

Hello lalakur

Take a view here to get proper started:

Have a nice day and enjoy coding in C++.

Thank you so much for your help. It helps me a lot to understand it easily @StefanL38 @paulpaulson

1 Like

You don’t need an Arduino for such a simple task .
Just use a float switch to turn the pump on/off or go old school with a ball cock arrangement.

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