// Forum: https://forum.arduino.cc/t/counter-misfunctioning-with-oled-i2c-display/1009288
#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;
}Loading
ssd1306
ssd1306