
149Chapter 8: Rotary Encoder Control
STEP 1: WIRING OUR ROTARY ENCODER
Now that we have wired our encoder, we can write a quick Arduino sketch
that will allow us to read the data coming from our encoder. This includes
the distance the encoder has been rotated in ticks, the direction in which
encoder’s knob was rotated, and the status of the internal button.
STEP 2: WRITING OUR ROTARYENCODER ARDUINO SKETCH
int DT = 11;
int CLK = 12;
int SW = 13;
int counter = 0;
int currentStateCLK;
int lastStateCLK;
void setup() {
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
lastStateCLK = digitalRead(CLK);
Serial.begin(9600);
}
void loop() {
currentStateCLK ...