Help on code to activate a motor by a ping sensor

I am a novice to working with arduino, but I would like to know if there are any code ideas to figure out when a ping sensor detects something, that if would activate a motor. I just need the motor to move in one direction and at a certain speed. Can anyone please help? Thank you! At least someone help direct me to sources on how to code this so that I can learn also. I also have another problem, when I try to upload my program to my arduino, it is very slow and then it wouldn't upload, so what should I do with that?

would like to know if there are any code ideas to figure out when a ping sensor detects something, that if would activate a motor.

Yes. The usual way to approach this is to write a sketch that gets data from a ping sensor, and prints the distance to the serial port (that the Serial Monitor is connected to).

When that reports reasonable data (i.e. you know that the hardware works and that the calculations are correct), you write another sketch that simply moves the motor the way you want.

When that works, it's pretty simple to combine the two, so that the motor turns on and does it's thing, whenever the ping sensor returns a value in the desired range.

So how should i code it? I am new at coding. So can you help me?

I am new at coding

You’ll never get better at it unless you try. If you want someone to write the code for you, go to Gigs and Collaboration, and get your wallet out.

Do you know any sources of where I can learn to code this?

Do you know any sources of where I can learn to code this?

Of course. There are examples galore on the playground. I know for a fact that there is one to read from an ultrasonic sensor. It isn't the best example, because all the code is in loop(), rather than a function, but it works.

It's relatively short, only a dozen lines or so. What each one does should be easy to understand, since it uses nothing but functions documented on the reference page.

The motor part is a bit more difficult, since the Arduino can not directly provide power for a motor. Therefore, you need additional hardware. There are several shields that can be purchased that make it easy to drive a motor. Then, basically, all you need to do is set a pin HIGH to make the motor go one way, and LOW to make it go the other way. Then, speed is defined using a call to analogWrite() with the motor speed pin and the speed (a value between 0 (stopped) and 255 (full speed)). Again, there are plenty of examples around.

Adding an if statement to the ping sensor example should be relatively easy.

if(distance > 4)
{
   // put the motor control code here...
}

Of course, you may have different ideas about what the threshold should be, and you might want to use a range (if the distance is more than 8 and less than 22) or you may want a discontinuous range (less than 3 or greater than 18).

If you have never programmed before, there are online classes, tutorials, etc. Local community colleges have courses, too. The money, time, and effort spent won't be wasted.

the external power source used is a 9V battery

If you're thinking about powering a motor from the same source, you may wish to reconsider your choice of power supply.