Quadrature encoder interfacing code

This code will print the count of the left and right motor encoder via a serial port. The two encoders are in a 2X decoding scheme, so we will get 4,200 CPR. In the first section of the code, we are defining pins for two channel outputs of two encoders and we are declaring the count variable for two encoders. The encoder variable uses a volatile keyword before the variable data type. The main use of volatile is that the variable with the volatile keyword will store in RAM memory, whereas normal variables are in CPU registers. Encoder values will change very quickly, so using an ordinary variable will not be accurate. In order to get accuracy, we will use volatile for encoder variables, as follows:

//Encoder ...

Get Learning Robotics using Python - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.