help understanding stepper motor code
I am new to using the basic stamp and I am still learning the language. I downloaded and ran the stepper motor code for a BS2. It works fine but I would like to understand it fully. I have attached the edited code showing my comments in the code blocks that baffle me. Any help in understanding this would be greatly appreciated.
Comments
This part of the code the math that is being done either counts from 0 to 3 or 3 to 0 one value at a time. I don't know how the author came to this use but it is an elegant approach.
Here the step index is used to pick the correct phase combination from the list to move the motor.
Ok I understand that stpIdx is used as an index therefore it will increment. What I dont know is:
Why is stpIdx not initialized? I am assuming that each time the program is run stpIdx has no starting value. Seems like bad programming form. I was always instructed to initialize a variable if you want consistent results. Of course I am assuming that an unitialized variable in PBasic has no value?
If 1//4 always return 1 and stpIdx has a starting value of zero then stpIdx will have the following values: 1 , 2, 3, 4
If 3//4 always returns 3 and stpIdx has a starting value of zero then stpIdx will have the following values: 3,6,9,12
What am I missing?
The next area I am confused about is in the Do_Step block. The Read statement has (Steps + stpIdx), Phase
What is the result of (Steps + stpIdx)? I know that Steps is a vector with 4 elements and I am assuming that stpIdx is a number so what is the result of the operation within the parenthesis?
Also what is the role of Phase? I know it is a pointer to a block of memory.
I would really appreciate an explanation. I am trying to learn but these seemingly simple parts of the program have me stumped.
thanks
Douglas
If stpidx is at 0, you add 1 which makes 1 and then you get the remainder of a division by 4 which is 1. You can go throught the rest one by one.
In this fragment,
the value of 'steps' is the address where the data list resides. You add to that the 'stpidx' and you are pointing at the motor coil combination.
That value is read from the list and put into the variable 'phase' which is the 4 output pins OUTB.
As far as initializing the variables, they are all reset when the program starts anyway.
In actuality, the position of the stepper is irrelevant when the program starts because the program has no way of knowing its position.
The only exception to that would be if you are using some limit switches or homing position switches and the motor had been in a known position before the power was turned off and it hadn't been moved since.
Thanks for the explanation. I feel like a total idiot for failing to follow the correct order of operations on the calculation of stpIdx.
I tried looking in the documentation to see if I could find an example that used the Read command·in the way it is used in this code but couldnt find an example.·The use of Steps + stpIdx is new syntax to me. ·