Skip to content

Instantly share code, notes, and snippets.

@glyons
Created July 15, 2018 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glyons/ceafab78fc38527a146f6cf9eee8358c to your computer and use it in GitHub Desktop.
Save glyons/ceafab78fc38527a146f6cf9eee8358c to your computer and use it in GitHub Desktop.
Rotary Encoder and SSD 1306
/* Rotary Encoder and SSD 1306
*/
#include <ESP_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility
#include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file)
#include <SPI.h> // For SPI comm (needed for not getting compile error)
#include <Wire.h> // For I2C comm, but needed for not getting compile error
// Pin definitions
#define OLED_RESET 16 // Pin 15 -RESET digital signal
#include <Encoder.h>
//Display
ESP_SSD1306 display(OLED_RESET); // FOR I2C
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(D5, D6);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
// SSD1306 Init
display.begin(SSD1306_SWITCHCAPVCC); // Switch OLED
display.display();
// Clear the buffer.
display.clearDisplay();
}
long oldPosition = 0;
long value=0;
void loop() {
GetEncoderValue();
// Display Value
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(4);
display.setTextColor(WHITE);
display.println(value);
display.display();
}
void GetEncoderValue()
{
long _newPosition = myEnc.read();
if (_newPosition != oldPosition) {
oldPosition = _newPosition;
Serial.println(_newPosition);
value=_newPosition/4;
value=abs(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment