Hi,
new to Arduino here. Just did most of Paul McWhoter's "most excelent" arduino tutorials and jumped in trying to use an rotary encoder as a way to control a simple menu.
Using it while displaying it on Serial is just fine, but when I, instead, print to an OLED I2C display, the counter miscounts (or skips counts). All problems go away when I comment the display part (keeping all the wiring and setup, just comment the if statements regarding the oled). Any ideas why?
Thanks in advance!
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
int inputCLK=6;
int inputDT=5;
int inputSW=4;
int counter=0;
int currentStateCLK;
int previousStateCLK;
//int SCLPin=A5;
//int SDAPin=A4;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled (SCREEN_WIDTH, SCREEN_HEIGHT,&Wire, -1);
void setup() {
// put your setup code here, to run once:
// Start serial
//Serial.begin(9600);
// Start screen
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
//define pinModes
pinMode(inputCLK, INPUT);
pinMode(inputDT, INPUT);
pinMode(inputSW, INPUT);
//current state
previousStateCLK=digitalRead(inputCLK);
//delay setup
delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
currentStateCLK=digitalRead(inputCLK);
if (currentStateCLK!=previousStateCLK){
//se se mexeu CCW
if (digitalRead(inputDT) != currentStateCLK){
counter=counter+1;
if (counter>3){
counter=3;
}
}
// se se mexeu CW
if (digitalRead(inputDT) == currentStateCLK){
counter=counter-1;
if (counter<0){
counter=0;
}
}
}
//Serial.println(counter);
if (counter==0){
oled.clearDisplay();
oled.setCursor(0,0);
oled.setTextColor(WHITE);
oled.setTextSize(2);
oled.println("Temp");
oled.println("Humidade");
oled.println("Luz");
oled.display();
previousStateCLK = currentStateCLK;
}
if (counter==1){
oled.clearDisplay();
oled.setCursor(0,0);
oled.setTextColor(WHITE);
oled.setTextSize(2);
oled.println(">Temp");
oled.println("Humidade");
oled.println("Luz");
oled.display();
previousStateCLK = currentStateCLK;
}
if (counter==2){
oled.clearDisplay();
oled.setCursor(0,0);
oled.setTextColor(WHITE);
oled.setTextSize(2);
oled.println("Temp");
oled.println(">Humidade");
oled.println("Luz");
oled.display();
previousStateCLK = currentStateCLK;
}
if (counter==3){
oled.clearDisplay();
oled.setCursor(0,0);
oled.setTextColor(WHITE);
oled.setTextSize(2);
oled.println("Temp");
oled.println("Humidade");
oled.println(">Luz");
oled.display();
previousStateCLK = currentStateCLK;
}
previousStateCLK = currentStateCLK;
}