#define pinCLK 1 // CLK pin connected to GPIO 1 #define pinDT 2 // DT pin connected to GPIO 2 #define pinSW 3 // SW pin connected to GPIO 3 #include #include // OLED display constructor U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 6, /* data=*/ 5); int count = 0; // Var to know how many clicks occured (increment clockwise and decrement counter_clockwise) int previousStateSW; // Var to memorize previous SW state to compare it with actual state int previousStateCLK; // Var to memorize previous CL state to compare it with actual state void setup() { Serial.begin(9600); // Initialize the OLED display u8g2.begin(); u8g2.setFont(u8g2_font_ncenB14_tr); u8g2.setCursor(0, 20); // Set position (x, y) // Write text to the screen u8g2.print("Starting Display"); pinMode(pinSW, INPUT_PULLUP); // INPUT_PULLUP to avoid 'floating' pinMode(pinDT, INPUT); pinMode(pinCLK, INPUT); previousStateSW = digitalRead(pinSW); // initial value for SW previousStateCLK = digitalRead(pinCLK); // initial value for CLK delay(200); // delay to stabilize signal before the loop } void loop() { int actualStateCLK = digitalRead(pinCLK); // reading CLK value int actualStateDT = digitalRead(pinDT); // reading DT value int actualStateSW = digitalRead(pinSW); // reading SW value // Clear the screen u8g2.clearBuffer(); // ************************* // Push Button verification // ************************* if (actualStateSW != previousStateSW) { // Checking if SW state has changed previousStateSW = actualStateSW; // Save new state if (actualStateSW == LOW) { Serial.println(F("SW Button pushed")); // Display State on serial monitor // Set font and position u8g2.setFont(u8g2_font_ncenB14_tr); u8g2.setCursor(0, 20); // Set position (x, y) // Write text to the screen u8g2.print("SW Button :"); u8g2.setCursor(0, 40); // Set position (x, y) // Write text to the screen u8g2.print("Pushed"); } else { Serial.println(F("SW Button released")); u8g2.setFont(u8g2_font_ncenB14_tr); u8g2.setCursor(0, 20); // Set position (x, y) // Write text to the screen u8g2.print("SW Button :"); u8g2.setCursor(0, 40); // Set position (x, y) // Write text to the screen u8g2.print("Released"); } // Send the buffer to the display u8g2.sendBuffer(); delay(10); // Delay to avoid rebounce } // *************************** // Rotary encoder verification // *************************** if (actualStateCLK != previousStateCLK) { // Checking if SW state has changed previousStateCLK = actualStateCLK; // Save new state if (actualStateCLK == LOW) { if (actualStateCLK != actualStateDT) { // comparing CLK and DT, if they're different, direction is counter-clockwise count--; // decrement counter Serial.print(F("Direction = counter-clockwise | Counter value = ")); // Display value on Serial Monitor Serial.println(count); u8g2.setFont(u8g2_font_ncenB10_tr); u8g2.setCursor(0, 15); // Set position (x, y) // Write text to the screen u8g2.print("Direction :"); u8g2.setFont(u8g2_font_ncenB08_tr); u8g2.setCursor(0, 28); // Set position (x, y) u8g2.print("counter-clockwise"); u8g2.setFont(u8g2_font_ncenB10_tr); u8g2.setCursor(0, 45); // Set position (x, y) // Write text to the screen u8g2.print("Counter value = "); u8g2.setCursor(0, 60); // Set position (x, y) u8g2.println(count); // Send the buffer to the display u8g2.sendBuffer(); } else { // when CLK and DT are similar, direction is clockwise count++; // increment counter Serial.print(F("Direction = clockwise | Counter value = ")); // Display value on Serial Monitor Serial.println(count); u8g2.setFont(u8g2_font_ncenB10_tr); u8g2.setCursor(0, 15); // Set position (x, y) // Write text to the screen u8g2.print("Direction :"); u8g2.setCursor(0, 30); // Set position (x, y) u8g2.print("clockwise"); u8g2.setCursor(0, 45); // Set position (x, y) // Write text to the screen u8g2.print("Counter value = "); u8g2.setCursor(0, 60); // Set position (x, y) u8g2.println(count); // Send the buffer to the display u8g2.sendBuffer(); } delay(1); // delay to avoid CLK rebounce } } }