Servo as a course follower

Hello.. I have arranged a sketch using a magnetometer and a button to fix any 0 to 360 value as a "storedCourse". After that, any change CW/CCW rotation goes to a variable called "delta". So, "delta" (degrees) can be positive if I rotate CW or negative if rotating CCW. My next goal is to use a Servo that rotates CW/CCW accordingly with "delta". I need some guidance about how to scale servo "attach" to follow changes and stop if "delta"=0 degrees. Thanks in advance

What type of servo do you have? Is it continuous? If not what is the range of movement you want? Code might also be helpful.

Hello .. You´re right... the servo is a DS04-NFC continuous.

The code portion I use to calculate "delta" is below..

int buttonState = digitalRead(buttonPin); // Button to store heading
if (buttonState == HIGH)
    storedHeading = heading;
int delta = heading - storedHeading;// delta heading
if(delta > 180) delta -= 360;     // delta limit
Serial.print("DELTA: ");

As you see, I have a "heading" from magnetometer and a button to create a "storedHeading". After that, I check for variations "delta" in heading around "storedHeading". Imagin I fix the sensor on flat top of the servo wheel: If I press the button, any CW/CCW (positiveº or negativeº) the servo could counter-act angular turn of both (servo+magnetometer). The sensor keeps pointing to the stored angular position... I guess a "delta" limits are needed to set full speed.. OK: Let´s set "delta" limits = +3º (or higher) for CW and -3º (or higher) for CCW. Any +/- value closer to 0º the servo could rotate smoother until stop... I need set delta limits to +/- 3º an scale them to control servo speed and direction. Many reads on this forums but I can´t find a definitive answer... Thans sir... PD.. I forgot to mention: the 180º limits on delta calculation are an over-all formula... not for servo use...

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Although your continuous servo can turn 360degrees to give you full rotation to track your magnetometer offset, you will have now way of knowing where it is pointing.

Servo commands only signal position to regular servos or winch servos.

Servo commands signal speed and direction of rotation to continuous rotation servos.

You will need some way of knowing where you continuous servo is pointing.

Tom... :)

Thanks for your advices… I think I post my code using the </> … It doesn´t scroll because only a few lines are included.
Back to the servo…
When I press the button, delta=0º.
Could I send that initial value servo to stop movement with an enough delay time?..
Or… if necessary, #define servo initial position to 0º from the beggining…


If your magnetometer is physically connected to the servo shaft and turns with it, then yes it should work. The magnetometer will be the angular position feedback needed.

Tom.... :)

Thanks TomGeorge. Well.. As I mention on post#3, "I fix the sensor on flat top of the servo wheel".. My knowledge on sofware made me reach the fundaentals of the Honeywell hmc5883l to get heading and correct it for declination in order to make a hobby boat. Circular calculations drive crazy for a few days to get "delta" changes around a desired heading ("storedHeading" on the code I post). Now, translating the +/- "delta" errors into an scaled servo range, is above my capacity... That´s why I need some kind of directions to start from.. A web site to learn.. Thanks again...


Google continuous rotation servo arduino

Tom.... :)

Hello again…
Here I am with little or no progress on my project.
I attach a pic of the hardware. I´ve searching on all servo-related projects for hours to find something that suits my purpouses.
As a continuous servo, the only feedbacko is the magnetic sensor.
To simplify things, let´s say we want to point to North.
How can I tell to the servo wheel to re-aligng to North if I rotate all the pieces around the axis?

How can I tell to the servo wheel to re-aligng to North if I rotate all the pieces around the axis?

Use the magnetometer to get the heading (the current direction [u]it[/u] is pointing, relative to North), in degrees.

Call that reading D.

Rotate the servo arm -D degrees.

Be sure to calibrate the magnetometer, or the readings will be wrong.