Some (unsuccessfull) encoder tests

This commit is contained in:
Oleksiy
2025-05-04 23:01:40 +03:00
parent 55bf5035fc
commit 663b723ddb
6 changed files with 304 additions and 0 deletions

View File

@ -0,0 +1,116 @@
#include <Wire.h>
#include <U8g2lib.h>
#define SCREEN_ADDRESS 0x3C
// Rev 2+ Config
#define ENC_BTN_PIN 14
#define ENC_D1_PIN 17
#define ENC_D2_PIN 4
#define START_STOP_BTN_PIN 5
#define SHIFT_BTN_PIN 12
#define EXT_INPUT_PIN 2 //needs to be an interrupt pin
#define ANALOGUE_INPUT_1_PIN A7
#define ANALOGUE_INPUT_2_PIN A6
const byte outsPins[6] = { 7, 8, 10, 6, 9, 11 };
const byte clockOutPin = 3;
bool rotateScreen = false;
bool reverseEnc = false;
//
/* Rev 1 Config
#define ENC_BTN_PIN 14
#define ENC_D1_PIN 17
#define ENC_D2_PIN 4
#define START_STOP_BTN_PIN 5
#define SHIFT_BTN_PIN 100
#define EXT_INPUT_PIN 2 //needs to be an interrupt pin
#define ANALOGUE_INPUT_1_PIN A2
#define ANALOGUE_INPUT_2_PIN A1
const byte clockOutPin = 13;
const byte outsPins[6] = {6, 11, 7, 10, 8, 9};
bool rotateScreen = true;
*/
U8G2_SSD1306_128X64_NONAME_2_HW_I2C u8g2(U8G2_R2, SCL, SDA, U8X8_PIN_NONE);
int16_t counter = 0;
void setup() {
pinMode(clockOutPin, OUTPUT);
pinMode(ENC_D1_PIN, INPUT_PULLUP);
pinMode(ENC_D2_PIN, INPUT_PULLUP);
//Enabling PinChange Interrupts for the encoder
//pins 17 (PC3/PCINT11) and 4 (PD4/PCINT20), ports C and D
PCICR |= 0b00000110;
PCMSK1 |= 0b00001000;
PCMSK2 |= 0b00010000;
u8g2.begin();
updateScreen();
}
void loop() {
checkInputs();
}
//Encoder interrupts
ISR (PCINT1_vect) {
checkEncoderStatus();
}
ISR (PCINT2_vect) {
checkEncoderStatus();
}
uint8_t encoderStatus;
uint32_t encoderCheckTime = 0;
uint32_t encoderTimeBetweenPulses = 0;
bool encoderDirectionOld; //0 = -, 1 = +, Old because current direction can be determined by encoderChange
int8_t encoderChange = 0;
//uint8_t encoderBurstCount = 0;
void checkEncoderStatus() {
//noInterrupts();
uint8_t newStatus = (digitalRead(ENC_D1_PIN) << 1) | digitalRead(ENC_D2_PIN);
switch(encoderStatus) {
case 0b00:
if (newStatus == 0b01) {
encoderChange++;
} else if (newStatus == 0b10) {
encoderChange--;
}
break;
case 0b01:
if (newStatus == 0b11) {
encoderChange++;
} else if (newStatus == 0b00) {
encoderChange--;
}
break;
case 0b11:
if (newStatus == 0b10) {
encoderChange++;
} else if (newStatus == 0b01) {
encoderChange--;
}
break;
case 0b10:
if (newStatus == 0b00) {
encoderChange++;
} else if (newStatus == 0b11) {
encoderChange--;
}
break;
}
encoderStatus = newStatus;
uint32_t currentTime = millis();
encoderTimeBetweenPulses = currentTime - encoderCheckTime;
encoderCheckTime = currentTime;
//interrupts();
}

View File

@ -0,0 +1,29 @@
void checkInputs() {
//encoder
if (encoderChange != 0) {
if (!reverseEnc) {
encoderChange = encoderChange * -1;
}
counter += encoderChange;
/*if (((encoderChange > 0) != encoderDirectionOld) && encoderTimeBetweenPulses < 60) { //filter out encoder "jumps".
encoderChange = 0; //Comented out because it seems like sometimes it was preventing normal scroll
} //if it works ok without it delete encoderDirectionOld var altogether
encoderDirectionOld = (encoderChange > 0);*/
//encoder acceleration
/*if (encoderTimeBetweenPulses < 15) { // <--
encoderBurstCount++;
} else {
encoderBurstCount = 0;
}
if (encoderBurstCount > 3) { // <--
encoderChange = encoderChange * 3; // <-- The three params need to be finetuned to feel natural
}*/
updateScreen();
//encPositionOld = encPosition;
encoderChange = 0;
}
}

View File

@ -0,0 +1,13 @@
void updateScreen() {
u8g2.firstPage();
do {
String valueStr;
u8g2.setDrawColor(1);
valueStr = String(counter);
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(0,15,valueStr.c_str());
} while ( u8g2.nextPage() );
}