// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <AiEsp32RotaryEncoder.h>
#include <Bounce2.h> //Entprellung
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
#define ROTARY_ENCODER_A_PIN 16
#define ROTARY_ENCODER_B_PIN 17
#define ROTARY_ENCODER_BUTTON_PIN 4
#define ROTARY_ENCODER_VCC_PIN -1
#define ROTARY_ENCODER_STEPS 4
bool rotaryVol = true;
unsigned int STATIONS = 7;
unsigned int maxVol = 16;
unsigned int actStation;
unsigned int actVol;
unsigned int curVol;
unsigned long delayVol;
bool btnStation = false;
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
//Button Einstellungen
byte button_pins[] = {34, 35, 33}; // button pins, 34= taster
#define NUMBUTTONS sizeof(button_pins)
Bounce * buttons = new Bounce[NUMBUTTONS];
void rotary_loop()
{
// process button press:
for (int i = 0; i<NUMBUTTONS; i++)
{
buttons[i].update(); // Update the Bounce instance
if ( buttons[i].fell() ) // If it fell
{
switch(i) {
case 0:
rotaryVol = false;
LCD.setCursor(0,1);
LCD.print(">Station:");
LCD.setCursor(10,1);
LCD.print(" ");
LCD.setCursor(10,1);
LCD.print(actStation);
rotaryEncoder.setBoundaries(0, STATIONS, true); //minValue, maxValue,
rotaryEncoder.setEncoderValue(actStation);
btnStation=true;
break;
}
}
}
//dont do anything unless value changed
if (rotaryEncoder.encoderChanged())
{
uint16_t v = rotaryEncoder.readEncoder();
if (!(rotaryVol))
{
Serial.printf("Station: %i\n",v);
actStation = v;
LCD.setCursor(10,1);
LCD.print(" ");
LCD.setCursor(10,1);
LCD.print(actStation);
}
else
{
Serial.printf("Volume: %i\n",v);
actVol = v;
LCD.setCursor(0,1);
LCD.print("Volume: ");
LCD.setCursor(10,1);
LCD.print(" ");
LCD.setCursor(10,1);
LCD.print(actVol);
delayVol = millis();
}
}
if (curVol != actVol && ((millis()-delayVol) > 2000))
{
//rotaryVol = false;
LCD.setCursor(0,1);
LCD.print("Station: ");
LCD.setCursor(10,1);
LCD.print(" ");
LCD.setCursor(10,1);
LCD.print(actStation);
Serial.println("Zeit abgelaufen");
curVol = actVol;
}
if (rotaryEncoder.isEncoderButtonClicked())
{
if (btnStation)
{
rotaryVol = true;
LCD.setCursor(0,1);
LCD.print("Station: ");
LCD.setCursor(10,1);
LCD.print(" ");
LCD.setCursor(10,1);
LCD.print(actStation);
rotaryEncoder.setBoundaries(0, maxVol, false); //minValue, maxValue,
rotaryEncoder.setEncoderValue(actVol);
}
}
}
//interrupt handling for rotary encoder
void IRAM_ATTR readEncoderISR()
{
rotaryEncoder.readEncoder_ISR();
}
void setup_rotary()
{
//start rotary encoder instance
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
if (!(rotaryVol))
{
rotaryEncoder.setBoundaries(0, STATIONS, true); //minValue, maxValue,
}
else
{
rotaryEncoder.setBoundaries(0, maxVol, false); //minValue, maxValue,
}
rotaryEncoder.disableAcceleration();
}
void setup() {
Serial.begin(115200);
// Make input & enable pull-up resistors on pushbuttons
for (int i=0; i<NUMBUTTONS; i++) {
buttons[i].attach( button_pins[i], INPUT_PULLUP); // setup the bounce instance for the current button
buttons[i].interval(25); // interval in ms
}
LCD.init();
LCD.backlight();
setup_rotary();
LCD.setCursor(0,0);
LCD.print("Webradio");
LCD.setCursor(0,1);
LCD.print("Station: ");
LCD.setCursor(10,1);
LCD.print(actStation);
rotaryEncoder.setBoundaries(0, maxVol, false); //minValue, maxValue,
delayVol = millis();
}
void loop() {
rotary_loop();
delay(250);
}