0% found this document useful (0 votes)
8 views2 pages

Arduino Rotary Encoder Control Code

This document contains an Arduino sketch for reading a rotary encoder and controlling LEDs based on its direction. The code tracks the encoder's position, increments or decrements it based on clockwise (CW) or counterclockwise (CCW) movement, and updates the corresponding LED and serial output. It includes a damper mechanism to prevent rapid direction changes within a threshold limit.

Uploaded by

DStavenau
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Arduino Rotary Encoder Control Code

This document contains an Arduino sketch for reading a rotary encoder and controlling LEDs based on its direction. The code tracks the encoder's position, increments or decrements it based on clockwise (CW) or counterclockwise (CCW) movement, and updates the corresponding LED and serial output. It includes a damper mechanism to prevent rapid direction changes within a threshold limit.

Uploaded by

DStavenau
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#define inputCLK 4

#define inputDT 5

#define ledCW 8
#define ledCCW 9

int encoderPos = 0;
int lastEncoded = 0;

String encdir = "";


int direction = 0; // 1 = CW, -1 = CCW, 0 = unknown
int damperCounter = 0;
const int damperThreshold = 2;

const int encoderMin = 0;


const int encoderMax = 255;

void setup() {
pinMode(inputCLK, INPUT);
pinMode(inputDT, INPUT);

pinMode(ledCW, OUTPUT);
pinMode(ledCCW, OUTPUT);

[Link](9600);

int clk = digitalRead(inputCLK);


int dt = digitalRead(inputDT);
lastEncoded = (clk << 1) | dt;

digitalWrite(ledCW, LOW);
digitalWrite(ledCCW, LOW);
}

void loop() {
int clk = digitalRead(inputCLK);
int dt = digitalRead(inputDT);
int encoded = (clk << 1) | dt;
int sum = (lastEncoded << 2) | encoded;

// CW movement
if (sum == 0b0001 || sum == 0b0111 || sum == 0b1110 || sum == 0b1000) {
if (direction == -1) {
damperCounter++;
if (damperCounter >= damperThreshold) {
direction = 1;
incrementEncoder();
damperCounter = 0;
}
} else {
direction = 1;
incrementEncoder();
damperCounter = 0;
}
}

// CCW movement
else if (sum == 0b0010 || sum == 0b0100 || sum == 0b1101 || sum == 0b1011) {
if (direction == 1) {
damperCounter++;
if (damperCounter >= damperThreshold) {
direction = -1;
decrementEncoder();
damperCounter = 0;
}
} else {
direction = -1;
decrementEncoder();
damperCounter = 0;
}
}

lastEncoded = encoded;
delay(1);
}

void incrementEncoder() {
if (encoderPos < encoderMax) {
encoderPos++;
showDirection("CW");
}
}

void decrementEncoder() {
if (encoderPos > encoderMin) {
encoderPos--;
showDirection("CCW");
}
}

void showDirection(String d) {
encdir = d;
if (d == "CW") {
digitalWrite(ledCW, HIGH);
digitalWrite(ledCCW, LOW);
} else {
digitalWrite(ledCW, LOW);
digitalWrite(ledCCW, HIGH);
}

[Link]("Direction: ");
[Link](encdir);
[Link](" -- Value: ");
[Link](encoderPos);
}

You might also like