I have a small problem that I really cant figure out how to solve after some time googling. My project consists of a rasberry pi and a rotary encoder that I will use as a sensor to see how fast a shaft is spinning. This is then coded in Python. My problem then is that I have no clue how to calculate this. The rotary encoder is connected directly to the shaft and will spin at the same speed as the shaft, and if I use this code the variable "counter" will increment with 1 per "click". Assuming thats what I have to go on, I need to know how I can calculate how fast counter increases so I can get the speed of the shaft. This is because the shaft will be spinning at diffrent speeds at all times. The reason I need to know how fast the shaft is spinning is because I have a UI that will display the speed.
I would also appreciate if you have any other approuches that I have missed. Thank you in advance.
This is how I would calculate the speed :
first add this at the top
import time
then a start time variable
clk = 17
dt = 18
starttime = time.time() ## used to calculate a duration
if you subtract starttime from time.time() you will get how long it has been since you defined the start time variable ( time.time() - starttime )
therefore you then add this
if dtState != clkState:
counter += 1
timetakentospinshaft = time.time() - starttime ## this is how long the shaft took to spin 360 degrees
starttime = time.time() # resetting the duartion
now you have the time taken, so you can calculate the speed by dividing the distance by the time ( speed = distance / time )
the distance can be calculated by multiplying the length of the shaft by 2π (calculation for circumference) ( distance = length of shaft * 2π )