Modulus Division Helppppppp!

CudaBlu

New Member
I clipped the piece of code below from one of the forum threads that was addressing the topic of controlling stepper motors. This piece of code helped me to get my cheap steppers running , for which I am very grateful. But, as I am trying to figure out how this works I have hit a wall on how modulus division operates.

I have looked through the forum for modulus examples and can not find one with the use of “max” which is one of the things that is hanging me up.

Code:
#Picaxe 08m2
'Stepper motor driver test
'Boris Burke May 2012

setfreq m8
dirs=%00010111
do
	b1=b1+2 max 3//3		'b1 is direction control, 2=CW 0=CCW
	for w1=1 to 2060		        'seems to be about the right number of steps for 360 degrees
		b0=b0+b1-1//4		'b0 cycles from 0 to 3 for CW and from 3 to 0 for CCW
		lookup b0,(%00011,%00110,%10100,%10001),pinsC
		pause 1
	next w1
	pause 4000
loop
The first time through the code this is how I believe b1 is being calculated:
- b1 = b1 + 2 max 3//3
- b1 = 0 + 2 = 2 max 0
- b1 = 2
Why is b1 equaling 2 and not 0?

The second time through this part of the code:
- b1 = b1 + 2 max 3//3
- b1 = 2 + 2 = 4 max 0
- b1 = 0
This one makes sense to me but I still can’t understand the result on the first time through, it seems inconsistent to me to have max 0 result in 2 the first time through and 0 the second time.

Now for the w1 loop:
- b0 = b0 + b1 – 1//4
- b0 = 0 + 2 = 2 – 1 = 1//4
- b0 = 1
How does b0 end up equaling 1? I noticed that b0 does the following:
- loop 1 b0 = 1
- loop 2 b0 = 2
- loop 3 b0 = 3
- loop 4 b0 = 0
Are the results of the first three loops because the modulus division has a decimal as a result and the fourth loop has a whole number as a result?

I am fascinated at how someone thought up this way of controlling a loop to cut down on the amount of code used and would just like to understand how and why this works.

I hope I wrote this out clearly enough. Thanks for the help.
 

Technical

Technical Support
Staff member
You need to work purely left to right, 3//3 is not the same as 0 in this case.
- b1 = b1 + 2 max 3//3
is not
- b1 = 0 + 2 = 2 max 0

it is
- b1 = 0 + 2 (= 2) max 3 (= 2) modulus 3 (= 2)
 

CudaBlu

New Member
I see. I was thinking that the "max" was somehow separating the equation into two different equations so I never even tried to calculate it any differently.

How about b0?

Thanks for the help. hope this was not too dumb a question.
 

Buzby

Senior Member
1//4 is 1 divided by 4 = 0 remainder 1
So the modulus (remainder) value is 1.
I would put a little punctuation in, just to make it clearer :

"1//4" is "1 divided by 4" = "0 remainder 1"
So the modulus (remainder) value is 1
 

boriz

Senior Member
I wrote that for my own experiments. Only later did I decide to put it up on the forum and add comments. So it was not written with any consideration for educational merit. (usually the case I'm afraid)

Hope it's all sorted now. Any problems, ask away.
 

CudaBlu

New Member
@boriz

I found the code in a post when I was searching on stepper motors. So glad that I did. I was not able to get my stepper working until I found your code. I am new to electronics and programming and did not know how your code worked, hence this thread. I really appreciate the fact that you posted it. Thanks!!!
 
Top