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.
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.
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
- 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.