So I'm following this tutorial to test a rotary encoder, as I am a complete newbie, but have an idea for it for a future project, and am just trying to get familiar with the concept.
Using the following code:
/*
Rotary Encoder Demo
rot-encode-demo.ino
Demonstrates operation of Rotary Encoder
Displays results on Serial Monitor
DroneBot Workshop 2019
https://dronebotworkshop.com
*/
// Rotary Encoder Inputs
#define inputCLK 4
#define inputDT 5
// LED Outputs
#define ledCW 8
#define ledCCW 9
int counter = 0;
int currentStateCLK;
int previousStateCLK;
String encdir ="";
void setup() {
// Set encoder pins as inputs
pinMode (inputCLK,INPUT);
pinMode (inputDT,INPUT);
// Set LED pins as outputs
pinMode (ledCW,OUTPUT);
pinMode (ledCCW,OUTPUT);
// Setup Serial Monitor
Serial.begin (9600);
// Read the initial state of inputCLK
// Assign to previousStateCLK variable
previousStateCLK = digitalRead(inputCLK);
}
void loop() {
// Read the current state of inputCLK
currentStateCLK = digitalRead(inputCLK);
// If the previous and the current state of the inputCLK are different then a pulse has occured
if (currentStateCLK != previousStateCLK){
// If the inputDT state is different than the inputCLK state then
// the encoder is rotating counterclockwise
if (digitalRead(inputDT) != currentStateCLK) {
counter --;
encdir ="CCW";
digitalWrite(ledCW, LOW);
digitalWrite(ledCCW, HIGH);
} else {
// Encoder is rotating clockwise
counter ++;
encdir ="CW";
digitalWrite(ledCW, HIGH);
digitalWrite(ledCCW, LOW);
}
Serial.print("Direction: ");
Serial.print(encdir);
Serial.print(" -- Value: ");
Serial.println(counter);
}
// Update previousStateCLK with the current state
previousStateCLK = currentStateCLK;
}
It works perfectly fine, as expected when running it on an Arduino Uno. Hooray, meeting expectations! However, my future project calls for a smaller form factor, so I try it on an Arduino Nano, with all the same breadboard setup and same hardware. However, the encoder does not function whatsoever, and the serial monitor outputs the following ad infintium:
22:52:12.077 -> Direction: CCW -- Value: -1
22:52:12.111 -> Direction: CW -- Value: 0
22:52:12.145 -> Direction: CCW -- Value: -1
22:52:12.179 -> Direction: CW -- Value: 0
22:52:12.212 -> Direction: CCW -- Value: -1
22:52:12.212 -> Direction: CW -- Value: 0
22:52:12.247 -> Direction: CCW -- Value: -1
22:52:12.280 -> Direction: CW -- Value: 0
22:52:12.313 -> Direction: CCW -- Value: -1
22:52:12.348 -> Direction: CW -- Value: 0
22:52:12.382 -> Direction: CCW -- Value: -1
22:52:12.416 -> Direction: CW -- Value: 0
22:52:12.450 -> Direction: CCW -- Value: -1
22:52:12.450 -> Direction: CW -- Value: 0
22:52:12.484 -> Direction: CCW -- Value: -1
22:52:12.518 -> Direction: CW -- Value: 0
22:52:12.552 -> Direction: CCW -- Value: -1
22:52:12.586 -> Direction: CW -- Value: 0
22:52:12.620 -> Direction: CCW -- Value: -1
22:52:12.655 -> Direction: CW -- Value: 0
22:52:12.688 -> Direction: CCW -- Value: -1
22:52:12.688 -> Direction: CW -- Value: 0
22:52:12.721 -> Direction: CCW -- Value: -1
22:52:12.755 -> Direction: CW -- Value: 0
22:52:12.789 -> Direction: CCW -- Value: -1
22:52:12.822 -> Direction: CW -- Value: 0
22:52:12.857 -> Direction: CCW -- Value: -1
22:52:12.857 -> Direction: CW -- Value: 0
22:52:12.891 -> Direction: CCW -- Value: -1
22:52:12.891 -> Direction: CW -- Value: 0
22:52:12.924 -> Direction: CCW -- Value: -1
22:52:12.958 -> Direction: CW -- Value: 0
22:52:12.993 -> Direction: CCW -- Value: -1
Not hooray! It's just immediately flipping back and forth between values, and turning the rotary encoder does nothing! The code is exactly the same between the two boards, I remembered to change the board model in the IDE, and I have all the connectors plugged into the appropriate ports. I can't for the life of me figure this one out. Any advice would be greatly appreciated.