Go Down

Topic: Ping-Pong with a DC motor & a pot (Read 2 times) previous topic - next topic


Could you give me a hint on how to code that?

You compute the absolute value of the difference between the current position and the middle of the range:

Code: [Select]
int midPoint = (MAX_RIGHT + MIN_LEFT)/2; // 525
int delta = abs(headRot - midPoint);

Then, map that difference to the desired speed value:
Code: [Select]
int maxVal = MAX_RIGHT - midPoint; // 750 - 525 = 225
int speed = map(delta, maxVal, 0, minSpeed, maxSpeed);

The minSpeed and maxSpeed variables need to be declared and valued before this code. The minimum will, presumably, be 0 and the maximum will be whatever you think appropriate based on you hardware.

When the value in delta is largest (the actual position is at the left or right limit), the mapping will result in the smallest value in the to range (minSpeed). When the value in delta is smallest (the actual position is at the midpoint), the mapping will result in the largest value in the to range (maxSpeed). So, you will be going fastest in the middle and slowest on the ends.

It might be necessary to constrain headRot to be in the range MIN_LEFT to MAX_RIGHT, and set the minimum speed to be something other than 0, so that if an overshoot occurs, the device will slowly rotate back to the correct position, rather than just stopping.


Thank you very much for this detail explanation.
This is exactly how a noob like myself learns from the experts. :-)


I was just wondering, instead of only having the maxSpeed at the exact midpoint, how could I get it to stay at the maxSpeed for the center 1/3 of the rotation?
So the 1/3's would look something like this:  minSpeed -> maxSpeed | maxSpeed -- maxSpeed | maxSpeed -> minSpeed


I was just wondering, instead of only having the maxSpeed at the exact midpoint, how could I get it to stay at the maxSpeed for the center 1/3 of the rotation?

Use a larger value for maxSpeed, then constrain the result of the map to be between lower (reasonable) limits.

What is it you are turning? What values have you picked for minSpeed and maxSpeed? How fast does that result in whatever you are actually turning moving?

With a valid range of only about half the potentiometer's actual range, it looks like you will only accomplish a 180 degree (approximately) rotation of whatever you are moving. I don't see how running full speed for more than a little of that range will make much difference in how long it takes to rotate that 180 degrees.


You won't believe what I'm building. :-)
It's a giant R2-D2 sculpture built out of canned food.
It's for a charity event called CANstruction. http://www.sfcanstruction.org
R2 will be 7' tall with a 3' dia. body.
All this coding is to get his head to turn.
I know R2's head should turn 360 deg. but my pot. only turns 270 deg. & I don't want to deal with winding/unwinding the wires.
Unfortunately my current motor is under sized & can't turn the head with much load on it, so I can't answer your questions about have long it's taking to turn it.
I have 2 larger motors on order now. Maybe you'll be right & I can put it back to only have the maxSpeed at the midPoint...

Go Up

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.

shortcuts: alt+s submit/post or alt+p preview