#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);//crear un objeto lcd (DIRECCIÓN pantalla, Tamaño x, Tamño y)
//ENCODER ROTARY-------------------------
#include "AiEsp32RotaryEncoder.h"
#include "Arduino.h"
#define ROTARY_ENCODER_A_PIN 23
#define ROTARY_ENCODER_B_PIN 22
#define ROTARY_ENCODER_BUTTON_PIN 1
#define ROTARY_ENCODER_VCC_PIN -1
#define ROTARY_ENCODER_STEPS 4
//instead of changing here, rather change numbers above
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
int buzzer=13;
void rotary_onButtonClick()
{
static unsigned long lastTimePressed = 0; // Soft debouncing
if (millis() - lastTimePressed < 500)
{
return;
}
lastTimePressed = millis();
Serial.print("button pressed ");
Serial.print(millis());
Serial.println(" milliseconds after restart");
}
void rotary_loop()
{
//dont print anything unless value changed
if (rotaryEncoder.encoderChanged())
{
tone(buzzer,2000,10);
Serial.print("Value: ");
Serial.println(rotaryEncoder.readEncoder());
}
if (rotaryEncoder.isEncoderButtonClicked())
{
rotary_onButtonClick();
}
}
void IRAM_ATTR readEncoderISR()
{
rotaryEncoder.readEncoder_ISR();
}
void setup() {
pinMode(buzzer, OUTPUT);
lcd.init();//inicializar la pantalla lcd
lcd.backlight();//Encender la luz de fondo
lcd.setCursor (0, 0);//poner el cursor en las coordenadas (x,y)
lcd.print(" ya casi jett");//muestra en la pantalla max 20 caracteres
lcd.setCursor (0, 1);//poner el cursor en las coordenadas (x,y)
lcd.print("cuidado con los ojos");//muestra en la pantalla max 20 caracteres
//encoder--------------------
Serial.begin(115200);
//we must initialize rotary encoder
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
//set boundaries and if values should cycle or not
//in this example we will set possible values between 0 and 1000;
bool circleValues = false;
rotaryEncoder.setBoundaries(0, 1000, circleValues); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
/*Rotary acceleration introduced 25.2.2021.
* in case range to select is huge, for example - select a value between 0 and 1000 and we want 785
* without accelerateion you need long time to get to that number
* Using acceleration, faster you turn, faster will the value raise.
* For fine tuning slow down.
*/
//rotaryEncoder.disableAcceleration(); //acceleration is now enabled by default - disable if you dont need it
rotaryEncoder.setAcceleration(250);
}
void loop() {
lcd.setCursor (0, 3);//poner el cursor en las coordenadas (x,y)
lcd.print("Tiempo activo:");//muestra en la pantalla max 20 caracteres
//La funcion millis() regresa los ms que lleva encendido
//Lo dividimos entre 1000 para que nos muestre en segundos.
lcd.print(millis()/1000);
lcd.print("s");
delay(1000);//Esperamos 1 segundo antes de repetir el loop
//encoder rotary--------------------
//in loop call your custom function which will process rotary encoder values
rotary_loop();
delay(50);
}