I am trying to build an etch a sketch with and oled display and 2 encoders but only one encoder is being read. The y isn't being updated and I have checked that the encoders and wires work by switching the wires.
Here is the code:
#include <U8g2lib.h> //for display
#include <a21.hpp> //for rotary encoder
#define OLED_SDA 4 //
#define OLED_SCL 5 // OLED
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0); //
using namespace a21;
EC11 encoder1;
EC11 encoder2;
const byte encoder1PinA = 2; // encoder 1
const byte encoder1PinB = 3; //
const byte encoder2PinA = 4; // encoder 2
const byte encoder2PinB = 5; //
int cursorX = 1;
int cursorY = 1;
void setup() {
u8g2.begin();
u8g2.clearBuffer();
Serial.begin(9600);
pinMode(encoder1PinA, INPUT_PULLUP);
pinMode(encoder1PinB, INPUT_PULLUP);
pinMode(encoder2PinA, INPUT_PULLUP);
pinMode(encoder2PinB, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoder1PinA), pinDidChange1, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoder1PinB), pinDidChange1, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoder2PinA), pinDidChange2, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoder2PinB), pinDidChange2, CHANGE);
}
void loop() {
CheckEncoder();
Serial.println("Y= " + String(cursorY));
Serial.println("X= " + String(cursorX));
Serial.println();
}
void SendDisplayBuffer() {
u8g2.setFont(u8g2_font_courR18_tf); // Choose a font
u8g2.setFontMode(1); // Set font mode to transparent
u8g2.setCursor(cursorX, cursorY);
u8g2.drawPixel(cursorX, cursorY);
u8g2.sendBuffer();
}
void CheckEncoder() {
EC11Event encoderE1;
EC11Event encoderE2;
if (encoder1.read(&encoderE1)) {
if (encoderE1.type == EC11Event::StepCW) {
// Clockwise rotation
cursorX += encoderE1.count; // Adjust the cursor's X position
} else {
// Counterclockwise rotation
cursorX -= encoderE1.count; // Adjust the cursor's X position
}
SendDisplayBuffer();
}
if (encoder2.read(&encoderE2)) {
if (encoderE2.type == EC11Event::StepCW) {
// Clockwise rotation
cursorY -= encoderE2.count; // Adjust the cursor's Y position
} else {
// Counterclockwise rotation
cursorY += encoderE2.count; // Adjust the cursor's Y position
}
SendDisplayBuffer();
}
}
void pinDidChange1() {
encoder1.checkPins(digitalRead(encoder1PinA), digitalRead(encoder1PinB));
}
void pinDidChange2() {
encoder2.checkPins(digitalRead(encoder2PinA), digitalRead(encoder2PinB));
}
I have tried a lot of things and i realize that there is an issue in the CheckEncoder() function. other libraries dont work for some reason with my arduino uno r3 and the ec11 encoder.