Hi All,
I am trying to store two variables within a while loop after reading a rotary encoder but stores same value for both variables w1Value and w2Value
#include <PinChangeInterrupt.h>
#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;
int select;
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);
attachPCINT(digitalPinToPCINT(ENC_PinA), rotaryEncoder, CHANGE);
lcd.setCursor(5, 0);
lcd.print("Start");
delay(1000);
lcd.clear();
Serial.begin(115200);
}
void loop()
{
lcd.setCursor(1, 0);
lcd.print("W1:10");
lcd.setCursor(1, 1);
lcd.print("W2:20");
lcd.setCursor(9, 0);
lcd.print("T:29");
lcd.setCursor(9, 1);
lcd.print("P:20");
lcd.print("%");
int w1Value;
int w2Value;
ENC_SWstate = digitalRead(ENC_SW);
if (ENC_SWstate != prevENC_SWstate)
{
if (ENC_SWstate == LOW)
{
Inconfig = 1;
}
}
prevENC_SWstate = ENC_SWstate;
while (Inconfig == 1)
{
ENC_SWstate = digitalRead(ENC_SW);
if (ENC_SWstate != prevENC_SWstate)
{
if (ENC_SWstate == LOW)
{
select += 1;
select = select % 4;
}
Serial.print(ENC_SWstate);
Serial.println(select);
}
prevENC_SWstate = ENC_SWstate;
switch (select)
{
case 0: //W1
clearSelectPointer(1);
lcd.setCursor(0, 0);
lcd.print(">");
w1Value = readEncoder() % 6 * 10;
lcd.setCursor(1, 0);
lcd.print("W1:");
lcd.print(w1Value);
lcd.print(" ");
break;
case 1: //W2
clearSelectPointer(2);
lcd.setCursor(0, 1);
lcd.print(">");
w2Value = readEncoder() % 6 * 10;
lcd.setCursor(1, 1);
lcd.print("W2:");
lcd.print(w2Value);
lcd.print(" ");
break;
case 2: //P
clearSelectPointer(3);
lcd.setCursor(8, 1);
lcd.print(">");
break;
case 3:
lcd.setCursor(0, 0); // 1
lcd.print(" ");
lcd.setCursor(0, 1); // 2
lcd.print(" ");
lcd.setCursor(8, 1);
lcd.print(" ");
break;
}
}
}
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(0, 1); // 2
lcd.print(" ");
lcd.setCursor(8, 1); // 3
lcd.print(" ");
break;
case 2: // for select 2
lcd.setCursor(0, 0); // 1
lcd.print(" ");
lcd.setCursor(8, 1); // 3
lcd.print(" ");
break;
case 3: // for select 3
lcd.setCursor(0, 0); // 1
lcd.print(" ");
lcd.setCursor(0, 1); // 2
lcd.print(" ");
break;
}
return (checkPointer);
}
Would it possible to store unique values to variables w1Value and w2Value ? []Q