How to make a motor react to a ultrasonic sensor

I see a board. How does it obtain power?

9v battery

As in, a "smoke detector" battery?

PP3 ?

image

I'm using a Duracell 9v, should I be using something else, and do I need to make any changes in my code?

Search results for '9 volt battery' - Arduino Forum

Search results for '9 volt battery motor' - Arduino Forum

Awesome do you think I need to change my code?

Why?

Have you changed out the power supply? Does it work now or still fails? If it fails how so?

Pretty much testing the code is moot until the project can be properly powered.

I'm still using the 9v I didn't get a chance to go battery shopping. But right now every time my sensor is 10cm away from the obstacle the motors only slow down a bit, when I want them to go backwards.

But right now, we know that the 9V battery is crap to run motors for your project. Get a new power source. Not another 9V battery.

If it only slows then what's the rest of the code doing? Serial prints would be quite an aid to your troubleshooting.
Oh and try this as an experiment:

Now it should do the avoid thing slower so you can see where it fails.

Great I'll try it thank you and did print the distance and I was getting measurements. On my first, if statement my servos are going forward which is what I want but my second if statement is the one causing me problems so it might be my power source.

Ok I did the test code the servos still went forward but they would almost stop for a few seconds ( due to delay(2000)). could it be the positions of the servos I'm giving it?

Might get you more info. And adding serial prints is something you can do on your own for troubleshooting.

So you are indicating that the thing gets to the < 10 and does a little jig but continues forward anyways, right? And you are doing this on a 9V battery right? And you are having issues with moving a motor right? And there has been mention that a 9V battery is poo for this kind of thing right? And you are going to insist you can get it to work with a 9V battery, right?

Good luck.

The thing is, @m4nthony, that PP3 battery may or may not be the source of the problem. But until you've replaced it with something that will definitely provide enough current for the motors, you can't really proceed any further with diagnosing the problem.

So get yourself a 9V power supply, or make one by stacking AA cells, or whatever. Then let us know if the fault persists.

Yes, 6 X AA batteries.

1 Like

Even when my robot is attached to the computer I get those same actions.

I think its my code because I'll test my robot when its attached to my computer.

Post a few images of the project.