Continue to Site

360 degree pointer shortest direction calculation

Status
Not open for further replies.

dr pepper

Well-Known 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?

misterT

Well-Known Member
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:

atferrari

Well-Known Member
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.

dr pepper

Well-Known Member
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.

Replies
9
Views
921
Replies
8
Views
2K
Replies
9
Views
2K
Replies
18
Views
5K
Replies
0
Views
796