Help with equation to find a specific number.

100 Views Asked by At

I created a program to run a stepper motor with decreasing delays between each step and ends once a value is reached. My problem is that I had to plug in a bunch of numbers until I found a value that matched a desired number of steps the motor took. I was hoping some could help me calculate the value without needing to guess.

Below is the Python code I am using to have my stepper motor reach 2500 steps.

delay = 0.01
final_delay = 0.00001
while final_delay < delay:
    delay = delay - (dealy/362.412)

The value of "362.412" is the one I refined until this loop completed 2500 steps. How can I find this value without needing to plug in numbers until I reach a desired number of steps?

Thank you for reading this, and any feedback or suggestions you give.

2

There are 2 best solutions below

1
On BEST ANSWER

Note that the last line of the loop could be written as

 delay *= 1-1/362.412

so that we want to multiply $.01$ by some number $p$, $2500$ times until we get to $.00001$ $$.01\cdot p^{2500}=.00001\\ p=.001^{1/2500}\\ p=0.9972407117415495 $$ Now if you solve $$p=1-\frac1q$$ you'll get $$q=362.4122985111945$$

0
On

Ignoring floating point errors, each delay is $\frac {361.412}{362.412}$ of the previous one, so you have a geometric series. If your multiplier is $r$ and the first delay is $a$, the delays are $a,ar,ar^2,ar^3\ldots$. If we start counting terms with $0$, the $n^{th}$ term is $ar^n$ and the sum of the terms from $0$ through $n$ (which is $n+1$ of them) is $a\frac {1-r^{n+1}}{1-r}$