Encoder Class

The encoder class is used to program the wheel encoders. It uses infrared light transmission to determine the speed at which the wheel is turning. By having a known number of slots in the wheel, the distance which a wheel turns can be easially calculated as the beam of light is continuously interrupted, sending a high or low signal back to the microcontroller. This information when combined with the time it takes to shift from a high to a low signal is used to calculate the speed at which the wheel is turning.

The wheel encoder connected to the Arduino
The wheel encoder connected to the Arduino. The red light indicates that light is being transmitted through a slot in the encoder.

.cpp Code

#include "encoder.h"
encoder :: encoder (unsigned int slots, unsigned int diameter, unsigned int pin){ m_speed = 0.0; m_currentTime = 0.0; m_slots = slots; //number of slots in the encoder wheel is 20, but can be changed m_diameter = diameter; //diameter of the wheel m_pin = pin; }
void encoder::updateTime(unsigned int time){ m_currentTime = time; //storing the time m_speed = (62500*(207.35/(360*(18/m_currentTime))))/1000; //calculation to find speed }
double encoder::getSpeed(){ return(m_speed); //gets the speed from updateTime }
void encoder::zeroSpeed(){ m_speed = 0.0; //not moving, set to zero }

.h Code

#ifndef encoder_h
#define encoder_h
class encoder{
private: double m_speed; //float to allow the more accuracy for speed double m_currentTime; unsigned int m_slots; unsigned int m_diameter; unsigned int m_pin; double m_prescalar;
public: encoder(unsigned int slots, unsigned int diameter, unsigned int pin); //Encoder is the constructor that sets/initializes the parameters void updateTime(unsigned int time); //function to update the current time from the internal timer double getSpeed(); //calculate the speed void zeroSpeed(); //set the speed to zero };
#endif