This is the first installment of my new Arduino series in which I show you how to implement various engineering concepts into an Arduino environment:
This post is about a part of a project I did over the summer which involves making a reaction-wheel controlled inverted pendulum.
Part of that project was to write a piece of code using Arduino IDE to be able to read the angle position of an incremental optical encoder connected to the pendulum arm in order to be controlled by a motor connected to a Arduino Mega2560 board and H-Bridge Motor Driver.
The encoder I used was at 400 ppr (pulses per revolution) which means the encoder sends 400 pulses to the Arduino micrcontroller every time a full revolution has been turned by the pendulum (365 degrees). This means our code would have to convert the PWM signal sent from the encoder into degrees through the serial monitor in the Arduino IDE software for the angle to be easily read.
I have also used two built-in interrupt pins (pin 0 and 1 for Mega2560). An Interrupt's function is to make sure that the Arduino responds quickly to changes in pulse signal. When a rising or falling edge is detected in the PWM square wave from the encoder, the pin interrupts whatever the Arduino is doing, and executes the ISR (interrupt service routine) designed to react to the change in pulses. Once that routine has finished, the processor goes back to its normal loop in the program.