Help with programming

Hello,
There seems to be no errors with this program but when I try to run it, only the conveyor motor and water pump works! Please help!

There is a water pump, 3 motors and an ultrasonic sensor hooked to it.
I wanted the program to run 1 motor hooked to a conveyor belt until the ultrasonic sensor detects an object and then runs the other 2 motors and then finally activates the pump, but when I upload the program, the water pump immediately runs along with the conveyor motor.

I am using L9110S motor drivers to drive the motors

Your help is greatly appreciated.

Thanks

bigchungus.ino (1.97 KB)

First thing I'd do is throw some Serial.print()s in there.

At least one to say what "distance" actually is, and one in each function so you can see what's being called.

I learned something new and extremely useful the other day.

Try this in any function to see if it's getting called:

Serial.println(__FUNCTION__);

...or:

Serial.println(__PRETTY_FUNCTION__);

(With a:

Serial.begin(9600);

.... in setup() of course.)

After checking your code, I realized a few things.

What I would do in the setup part of the code is set all of the outputs to low, for example digitalWrite(outputPin, LOW);
With this you can be sure that the output pins are all low on the beginning.

Another thing you should try is lowering the value in the if statement. Instead of 1540, try putting in a lower number and see if that works, then you can tweak it.

Other than that, I can't see any problem. Maybe it's in the hardware design?

LanternMG:
Another thing you should try is lowering the value in the if statement. Instead of 1540, try putting in a lower number and see if that works, then you can tweak it.

That's why it's an idea to print the value out: if OP doesn't know that the distance reported is correct, the rest is guesswork.

(Maybe it's a unit of measurement thing? What are the units? If you expect mm, 1500=1.5m, but if those are actually cm then you need to be 15m away.....)

I see.... Thank you all for answering! I'll try and see if it works...

Note that pulseIn() returns 0 if nothing is in range. You may want to change:

  if (distance >= 1540)

to:

  if (distance == 0 || distance >= 1540)