Buen dia con todos,esperando se encuentren bien. Mi problema es el siguiente.
Quiero hacer un proyecto de un menu con encoder rotativo para aplicarlo a un huerto automatizado. Pero mi problema es que cuando quiero mandar los datos del encoder a la oled claramente se puede reflejarlos, sin embargo nose a que se deb pero hace que se trabe o no responda el encoder de la mejor manera (Ojo: solo al poner los datos para imprimir en el OLED, ya que solo por serial anda perfectamente inclusive con los datos de tiempo y fecha con la funcion NTP y los datos del encoder a la vez). La tarjeta que uso es una WEMOS D1 mini y una OLED 128X64 con comunicacion I2C.
A continuacion pongo a disposicion el codigo que uso.
/////////////////////////////////////////////////////////////////
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Button2.h" // https://github.com/LennartHennigs/Button2
#include "ESPRotary.h"
#include "Ticker.h"
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define ROTARY_PIN1 12
#define ROTARY_PIN2 13
#define BUTTON_PIN 14
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define CLICKS_PER_STEP 4 // this number depends on your rotary encoder
#define SERIAL_SPEED 115200
#define SPEEDUP_STEPS 5
/////////////////////////////////////////////////////////////////
//Claves de Usuario
const char *ssid = "DARIO CHUQUITARCO";
const char *password = "Dariojavier02";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ec.pool.ntp.org", -18000, 60000);
// NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval);
char dayWeek [7][12] = {"DOMINGO", "LUNES", "MARTES", "MIERCOLES", "JUEVES", "VIERNES", "SABADO"};
ESPRotary r;
Ticker t;
Button2 b;
/////////////////////////////////////////////////////////////////
enum STATES
{
MEASURE_REGULATION, //measuring and temperature regulation
SETUP, // setup
HUMIDITY,
HIGHEST,
LOWER,
};
STATES state;
int hl = 55;
int hh = 60;
double encoder = 37.5;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
WiFi.begin(ssid, password);
Serial.begin(SERIAL_SPEED);
delay(50);
Serial.println("\n\nSimple Counter");
timeClient.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextColor(SSD1306_WHITE);
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.println(" MI HUERTO");
display.println(" LETTUCE");
display.println(" BY SAM");
display.write(2);
display.display();
delay(3000);
r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP);
r.setChangedHandler(rotate);
r.setLeftRotationHandler(showDirection);
r.setRightRotationHandler(showDirection);
r.setSpeedupStartedHandler(speedupStarted);
r.setSpeedupEndedHandler(speedupEnded);
b.begin(BUTTON_PIN);
b.setTapHandler(click);
r.retriggerEvent(false);
r.enableSpeedup(true);
r.setSpeedupIncrement(SPEEDUP_STEPS);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
t.attach_ms(1, handleLoop);
timeClient.begin();
}
int pos;
void handleLoop() {
r.loop();
}
void loop() {
b.loop();
r.loop();
if (state == MEASURE_REGULATION) {
Serial.println(hl);
Serial.println(dayWeek[timeClient.getDay()]);
Serial.println(timeClient.getFormattedTime());
display.clearDisplay();
//Configuracion del tiempo
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(4, 1);
display.setTextSize(1.8);
display.println(dayWeek[timeClient.getDay()]);
display.setCursor(20, 10);
display.setTextSize(2.2);
display.println(timeClient.getFormattedTime());
display.println(hl);
display.display();
}
else if (state == SETUP) {
Serial.println(encoder);
/*display.clearDisplay();
display.setCursor(20, 10);
display.setTextSize(2.2);
display.print(encoder);
display.display();*/
}
timeClient.update();
}
/////////////////////////////////////////////////////////////////
// on change
void rotate(ESPRotary& r) {
//Serial.println(r.getPosition());
pos = r.getPosition();
}
// on left or right rotation
void showDirection(ESPRotary& r) {
//Serial.println(r.directionToString(r.getDirection()));
if (state == MEASURE_REGULATION) {
if (r.directionToString(r.getDirection()) == "right") {
//Serial.println("Hola");
hl += 1;
}
else {
//Serial.println("No");
hl -= 1;
}
hl = min(hh, max(10, hl));
}
if (state == SETUP) {
if (r.directionToString(r.getDirection()) == "right") {
//Serial.println("Hola1");
encoder += 0.1;
}
else {
//Serial.println("No1");
encoder -= 0.1;
}
encoder = min(60.0, max(20.0, encoder));
}
}
// single click
void click(Button2& btn) {
//Serial.println("Click!");
switch (state)
{
case MEASURE_REGULATION:
if (btn.wasPressedFor())
{
state = SETUP;
//Serial.println("MENU 1");
}
break;
case SETUP:
if (btn.wasPressedFor())
{
//set = encoder;
state = HUMIDITY;
//Serial.println("MENU 2");
}
break;
case HUMIDITY:
if (btn.wasPressedFor())
{
//sout = encod;
state = HIGHEST;
//Serial.println("MENU 3");
}
break;
case HIGHEST:
if (btn.wasPressedFor())
{
//cof = hh;
state = LOWER;
//Serial.println("MENU 4");
}
break;
case LOWER:
if (btn.wasPressedFor())
{
//cof1 = hl;
state = MEASURE_REGULATION;
//Serial.println("MENU 0");
}
break;
}
}
// long click
void speedupStarted(ESPRotary& r) {
//Serial.println("Speedup started");
}
void speedupEnded(ESPRotary& r) {
//Serial.println("Speedup ended");
}
Esperando una pronta solucion o un arreglo que se pueda dar, les anticipo mis agradecimientos