0

I'm running a water pump based on ultra sonic readings. Once the Ultra Sonic Sensor reads > 10 cm , I want the water pump to start until sensor reads < 5 cm. Would you please help me adjust the following code to meet my requirements.

void loop(){
 digitalWrite(trigPin,LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin,HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin,LOW);
 duration=pulseIn(echoPin,HIGH);
 distance=(duration*0.034/2);
 if(distance>5){
   digitalWrite(motor,HIGH);
   delay(30000);
   digitalWrite(motor,LOW);
 } else {
   digitalWrite(motor,LOW);
 } 
}

1 Answer 1

2

So what you're asking is:

  • If distance is greater than 10:
    • Turn on
  • If distance is less than 5:
    • Turn off

That's not hard to code, since you already have an "if" there that compares the distance.

 if(distance>5){
   digitalWrite(motor,HIGH);
   delay(30000);
   digitalWrite(motor,LOW);
 } else {
   digitalWrite(motor,LOW);
 } 

becomes:

if (distance > 10) {
    digitalWrite(motor, HIGH);
}

if (distance < 5) {
    digitalWrite(motor, LOW);
}

See how there's a direct 1:1 correlation between the English I wrote at the top and the code below it? That's often a good way to understand code: write it out in plain English step by step what you want and what decisions you need to make - then there's usually a simple conversion from English to code.

The hardest part of learning to program is understanding the logic. Do that in English first and the writing of the code itself becomes so much easier.

5
  • Thanks. Two points, using the code above, if distance < 10, motor won't work. I tried adding digitalWrite(motor, HIGH); before if statement but I didn't test it yet. Second, The motor keeps running on repetitive cycles I believe every time code loops. is that healthy? Oct 5, 2020 at 18:35
  • 1
    If distance < 10 then the motor shouldn't work. You haven't specified that it should. You want it on for distance > 10 not < 10. If it's cycling on and off then distance is changing from greater than 10 to less than 5 repeatedly.
    – Majenko
    Oct 5, 2020 at 18:41
  • With Majenko's code, the motor won't turn on until the distance is > 10. Then it will stay on until the distance drops to < 5. That's what you wanted.
    – Duncan C
    Oct 5, 2020 at 19:39
  • You might want to add a maximum time value as well, so that if your ultrasonic sensor fails with the pump on, it has an upper limit on how long the pump will run. That would require recording the millis() value at the moment you turn the pump on, and turning it off if the motor is on and the current value of millis() is far enough in the past.
    – Duncan C
    Oct 5, 2020 at 19:41
  • @motionchannel: For reference, the behavior described by your prose and implemented in Majenko's code is called hysteretic control. Within the hysteresis band (5 – 10 cm) the pump is supposed to stay in whatever state is was previously set. Hysteresis is a very sensible way of avoiding the motor rapidly switching on and off. See for example Wikipedia: Hysteresis in engineering. Oct 6, 2020 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.