#include "AiEsp32RotaryEncoder.h"
#include "Arduino.h"
#include <LiquidCrystal_I2C.h>
#define ROTARY_ENCODER_A_PIN 34
#define ROTARY_ENCODER_B_PIN 35
#define ROTARY_ENCODER_BUTTON_PIN 19
#define ROTARY_ENCODER_VCC_PIN -1
#define ROTARY_ENCODER_STEPS 4
#define DAC_CH1 25
#define DAC_CH2 26
//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);
LiquidCrystal_I2C lcd(0x27,20,4);
int buttonState = 0;
int positionCurrent = 0;
int positionPrevious = 0;
int valueDAC1 = 0;
int valueDAC2 = 0;
bool selectCurDAC = 0;
bool selectPreDAC = 0;
bool valueReset = false;
char s = 126;
static unsigned long lastTimePressed =0;
bool buttonPress = false;
void rotary_onButtonClick()
{
}
void rotary_loop()
{
//dont print anything unless value changed
}
void IRAM_ATTR readEncoderISR()
{
rotaryEncoder.readEncoder_ISR();
if (rotaryEncoder.encoderChanged()) {
positionCurrent = rotaryEncoder.readEncoder();
}
}
void updateDACValue() {
if (valueReset == true) {
lcd.setCursor(17,0);
lcd.print(" ");
lcd.setCursor(17,0);
lcd.print(valueDAC1);
lcd.setCursor(17,1);
lcd.print(" ");
lcd.setCursor(17,1);
lcd.print(valueDAC2);
dacWrite(DAC_CH1, valueDAC1);
dacWrite(DAC_CH2, valueDAC2);
valueReset = false;
}
else if (selectCurDAC == 0) {
lcd.setCursor(17,0);
lcd.print(" ");
lcd.setCursor(17,0);
valueDAC1 = valueDAC1 + positionCurrent - positionPrevious;
lcd.print(valueDAC1);
dacWrite(DAC_CH1, valueDAC1);
}
else if (selectCurDAC == 1) {
lcd.setCursor(17,1);
lcd.print(" ");
lcd.setCursor(17,1);
valueDAC2 = valueDAC2 + positionCurrent - positionPrevious;
lcd.print(valueDAC2);
dacWrite(DAC_CH2, valueDAC2);
}
}
void updateDACSelect() {
if (selectCurDAC == 0) {
lcd.home();
lcd.print(s);
lcd.print(" DAC CH1 Value: ");
lcd.print(valueDAC1);
lcd.setCursor(0,1);
lcd.print(" DAC CH2 Value: ");
lcd.print(valueDAC2);
}
else if (selectCurDAC == 1) {
lcd.home();
lcd.print(" DAC CH1 Value: ");
lcd.print(valueDAC1);
lcd.setCursor(0,1);
lcd.print(s);
lcd.print(" DAC CH2 Value: ");
lcd.print(valueDAC2);
}
}
void setup()
{
//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, 255, 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); //or set the value - larger number = more accelearation; 0 or 1 means disabled acceleration
lcd.init();
lcd.clear();
lcd.backlight();
updateDACSelect();
}
void loop() {
//in loop call your custom function which will process rotary encoder values
if (selectPreDAC != selectCurDAC) {
updateDACSelect();
selectPreDAC = selectCurDAC;
}
if (positionPrevious != positionCurrent || valueReset == true) {
updateDACValue();
positionPrevious = positionCurrent;
}
if (rotaryEncoder.isEncoderButtonClicked()) {
if (millis() - lastTimePressed < 5000) {
selectCurDAC = !selectCurDAC;
buttonState = 1;
}
else if (millis() - lastTimePressed > 5000) {
valueDAC1 = 0;
valueDAC2 = 0;
valueReset = true;
}
}
if (digitalRead(19) == HIGH){
lastTimePressed = millis();
}
lcd.setCursor(0,2);
lcd.print(millis());
lcd.setCursor(0,3);
lcd.print(lastTimePressed);
delay(10); //or do whatever you need to do...
}