#include #include #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(); }