Use of rotary encoder

Rotary encoder is a device combining precise rotation measurement and click. It is much more accurate to control the steering gear than to use potentiometer. At the same time, it is much more convenient to use it to select menu and modify the value of setting items than to use multiple buttons.

It's easy to find these cheap devices on the Internet, just search the rotary encoder on TB directly.
The rotary encoder module I bought has 5 pins, which are GND, VCC (+), SW, DT, CLK. Among them, VCC and GND are used to connect power supply and ground. According to the abbreviation SW, it should be Switch, CLK, Clock and DT, Data. The detailed introduction of this encoder can be seen from the information on the Internet. There are many examples on the Internet to introduce the use of this encoder, but in my opinion, these examples are used to explain the principle. I still think it is easier and clearer to encapsulate these codes into a class for use. Here are two examples I will write after encapsulating them.

General use:

#include "flexmisc.h"

//---------------------------------------------------------------------------------
/*
Rotary encoder example 1, general polling use
TFlexRotaryEncoder re(CLK, DT, SW);Respectively corresponding to the Arduino pin connected to the corresponding rotary encoder;
If it is not convenient to determine the corresponding parameters during declaration, you can also use bind after instantiation to do post binding.
Written by: flexitime
2019-5-12
*/
//---------------------------------------------------------------------------------

TFlexRotaryEncoder re(2, 3, 4);

void reRotateEvent(int direct){
  Serial.print("Position: ");
  Serial.println(re.getCount());
  if (direct == 1){ Serial.println("Turn Right");}
  if (direct == -1){ Serial.println("Turn Left");};
}

void reButtonClickEvent(){
  Serial.print("Click ");
  Serial.println(re.getCount());  
}

void setup() {
  Serial.begin (9600);
  //Bind rotation response events. Unbound means no response events.
  re.attachRotateEvent(reRotateEvent);
  //Bind keys to respond to events. Not binding means not responding to events.
  re.attachButtonClickEvent(reButtonClickEvent);
  //Set the counter to 0 when the key is pressed. The default value is not to 0;
  re.setClickResetCount(true);
}
 
void loop() { 
  //Check the status of the rotary encoder.
  re.checkState();
}

The following is to use hardware interrupt to meet high response requirements:

#include "flexmisc.h"

//---------------------------------------------------------------------------------
/*
Example 2 of rotary encoder is called by interrupt mode, which is used when the system is under high load and cannot respond to polling well
TFlexRotaryEncoder re(CLK, DT, SW);Respectively corresponding to the Arduino pin connected to the corresponding rotary encoder;
If it is not convenient to determine the corresponding parameters during declaration, you can also use bind after instantiation to do post binding.
Written by: flexitime
2019-5-12
*/
//---------------------------------------------------------------------------------
//CLK of rotary encoder is connected to 2 pins of Arduino, which correspond to Arduino interrupt 0
TFlexRotaryEncoder re(2, 3, 4);

void reRotateEvent(int direct){
  Serial.print("Position: ");
  Serial.println(re.getCount());
  if (direct == 1){ Serial.println("Turn Right");}
  if (direct == -1){ Serial.println("Turn Left");};
}

void Int0Event(){
  // Note that key events cannot be responded to when the call is interrupted
  re.checkState();
}

void setup() {
  Serial.begin (9600);

  //Bind rotation response events. Unbound means no response events.
  re.attachRotateEvent(reRotateEvent);
  
  //Set the processing function of interrupt 0, which is triggered by the level change, but the interrupt cannot respond to the key event
  attachInterrupt(0, Int0Event, CHANGE);
  
}
 
void loop() { 
  //Detect key events
  if (re.isClick()){Serial.println("click");};
  
  //Assume that the system response is very slow ~~
  delay(100);
}

As for flexmisc.h, I will upload it to the download area later.

Posted by nightowl on Tue, 12 Nov 2019 10:16:01 -0800