While this shows the fundamentals of how incremental encoders work, this implementation wouldn’t work so great in practice because you’re hogging CPU time to make sure you don’t miss a pulse. This would be an excellent time to use interrupts to trigger this logic, which would free up your CPU cycles to actually run the rest of your project.
I also had a version that was using interrupts. However, that was causing issues with rest of the code I was using in my project 🤔 I settled for this approach, because its more „general purpose“, I think. Thank you for the comment though. I am also not all-knowing and would love to learn more about the topic :)
You might like to look at the STM32 encoder functions, which use 2 timers to do count and direction, with the ability to "chain" the encoder counter to another timer to get the pulse width of the transitions, to generate speed values...
This one does not. There are models with 4 Pins, that include a Vin I belive. When I started playing around with it, I assumed it would be working the same as a Potentiometer and connected 1 pin to ground, one to a GPIO and one to 3.3V. This resulted in me frying my microcontroller 😂