Hello all,
I am trying to figure out why the rotary encoder does not increment or decrement.
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
hd44780_I2Cexp lcd;
// Const Variables and pin assinged
const byte ENC_SW = 3; // Encoder Switch
const byte ENC_PinA = 6; // PIN A of Encoder
const byte ENC_PinB = 8; // PIN B of Encoder
const byte buttonPin = 9; // Weld button
// Variables
byte prevENC_SWstate = HIGH;
byte ENC_SWstate = HIGH;
byte Inconfig; // Display config
int ENC_PinAState = LOW;
int ENC_PinALastState = LOW;
int Counter1;
unsigned long TimeStamp;
unsigned long ENC_SWpressDuration;
unsigned long delayTime = 50;
void setup()
{
lcd.begin(16, 2);
lcd.clear();
pinMode(ENC_PinA, INPUT_PULLUP);
pinMode(ENC_PinB, INPUT_PULLUP);
pinMode(ENC_SW, INPUT_PULLUP);
lcd.setCursor(5, 0);
lcd.print("Start");
delay(1000);
lcd.clear();
Serial.begin(115200);
}
void loop()
{
byte InMenu = false;
byte select;
lcd.setCursor(0, 0);
lcd.print("W1:10");
lcd.setCursor(0, 1);
lcd.print("W2:20");
lcd.setCursor(9, 0);
lcd.print("T:29");
lcd.setCursor(9, 1);
lcd.print("P:20");
lcd.print("%");
ENC_SWstate = digitalRead(ENC_SW);
if (ENC_SWstate == LOW && prevENC_SWstate == HIGH)
{
delay(20);
ENC_SWstate = digitalRead(ENC_SW);
if (ENC_SWstate == LOW && prevENC_SWstate == HIGH)
{
TimeStamp = millis();
}
}
if (ENC_SWstate == HIGH && prevENC_SWstate == LOW)
{
delay(20);
ENC_SWstate = digitalRead(ENC_SW);
if (ENC_SWstate == HIGH && prevENC_SWstate == LOW)
{
ENC_SWpressDuration = (millis() - TimeStamp);
}
}
if (ENC_SWpressDuration > 0 && ENC_SWpressDuration >= delayTime)
{
prevENC_SWstate = ENC_SWstate;
Inconfig = 1;
lcd.setCursor(5, 0);
lcd.print("<");
}
while (Inconfig == 1)
{
int select;
select = readEncoder() % 4;
prevENC_SWstate = ENC_SWstate;
Serial.println(select);
switch (select)
{
case1://W1
clearSelectPointer(1);
Serial.println(select);
lcd.setCursor(6, 0);
lcd.print("<");
prevENC_SWstate = ENC_SWstate;
break;
case2://W2
clearSelectPointer(2);
lcd.setCursor(6, 1);
lcd.print("<");
prevENC_SWstate = ENC_SWstate;
break;
case3://P
clearSelectPointer(3);
lcd.setCursor(15, 1);
lcd.print("<");
prevENC_SWstate = ENC_SWstate;
Inconfig = 0;
break;
}
}
// Serial.println(prevENC_SWstate);
prevENC_SWstate = ENC_SWstate;
prevENC_SWstate = ENC_SWstate;
}
void rotaryEncoder()
{
ENC_PinAState = digitalRead(ENC_PinA);
if (digitalRead(ENC_PinB) != ENC_PinAState)
{
Counter1--;
}
else
{
Counter1++;
}
}
int readEncoder()
{
noInterrupts();
int copyCounter = Counter1;
interrupts();
return (copyCounter) >> 1;
}
int clearSelectPointer(int checkPointer)
{
switch (checkPointer)
{
case 1:// for select 1
lcd.setCursor(6, 1); // 2
lcd.print(" ");
lcd.setCursor(15, 1); // 3
lcd.print(" ");
break;
case 2:// for select 2
lcd.setCursor(6, 0); // 1
lcd.print(" ");
lcd.setCursor(15, 1); // 3
lcd.print(" ");
break;
case 3:// for select 3
lcd.setCursor(6, 0); // 1
lcd.print(" ");
lcd.setCursor(6, 1); // 2
lcd.print(" ");
break;
}
}