Arduino Mega 2560 Rotary Encoder Control Code

Rotary encoders are designed for infinite circular turns in both direction. You can press the top button to send out an extra event. Encoders can sense if they are turned in full, half or quarter rounds depending on the type of the encoder. They send out these turns in bit arrays. To see the prompt bit arrays you should receive, please check the datasheet of the encoder you are using. You can find datasheets on the internet. When the rotary encoder is turned by someone or the button is pressed an event is sent from the device.

Required hardware

  • Arduino Mega 2560
  • Rotary encoder
  • 3 Resistors 1kΩ

Source code to install on controller


Before you upload this code to your Arduino, please format the EEPROM...
#include <OzIDManager.h>
#include <OzRotaryController.h>
 
OzIDManager* manager;
OzRotaryController* rotaryController;
 
const EncoderType encoderType = EncoderType::HALF;
const int buttonPin   = 4;
const int channelAPin = 2;
const int channelBPin = 3;
 
void setup()
{
  Serial.begin(115200);
 
  manager = new OzIDManager;
  manager->_sendACK = true;
  manager->_checksum = true;

  OzCommunication::setIDManager(manager);
 
  rotaryController =
  	new OzRotaryController(encoderType, buttonPin, channelAPin, channelBPin);
   
  int x=1;
  manager->sendLinkSetup();
  manager->PrintWelcomeLine(rotaryController, x++, "MyRotaryEncoder");
}
 
void loop()
{
    rotaryController->ownLoop();
    OzCommunication::communicate();
}

More information