Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

360 degree pointer shortest direction calculation

Status
Not open for further replies.

dr pepper

Well-Known Member
Most Helpful Member
I have a stepper with pointer attached, I know where 0 is with a opto slotted switch.
I have a variable in a rgister which is 0-360 and is the degrees at which the pointer needs to be at.

Getting it to work was no problem, but now I'm thinking of some code that moves the pointer over the shortest distance, ie if the pointer is on 0 and wants to go to 359, at the moment with the code I have it will go clockwise the long way round not just back anticlockwise a degree.
Is there an easy way to calc which direction to go in?
 
Ok.. So the pointer must move from A to B.
Assuming both values are between 0...360

"pseudo code"

C = B-A;
if (C > 180) { C = C-360; }
else if (C < (-180)) { C = C+360; }

now C holds the degrees to move.
 
Last edited:
I recall doing something like that

As in the compass rose, you have 360º, that is 360 different positions.

I understand you should consider valid extreme values 0 to 359.
 
Thanks mt, I had got part way there.
I code in asm, however I can work of that.

What your doing is calculating the amount to move, and if the amount is greater or less than 180 degrees computing a correction.
And that is all done with compares and addition/subtraction.

I had considered handling 0 and 359 seperatly however the values dont allways go through 0 or 359 so that wouldnt work.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top