Hi All,
I am trying to store two variables within a while loop after reading a rotary encoder but it stores the same value of w1 when switching over to w2
Mod edit: added code tags
I am trying to store two variables within a while loop after reading a rotary encoder but it stores the same value of w1 when switching over to w2
C:
#include<PinChangeInterrupt.h>
#include<Wire.h>
#include<hd44780.h>
#include<hd44780ioClass/hd44780_I2Cexp.h>
hd44780_I2Cexp lcd;
// Const Variables and pin assinged
constbyte ENC_SW =3; // Encoder Switch
constbyte ENC_PinA =6; // PIN A of Encoder
constbyte ENC_PinB =8; // PIN B of Encoder
constbyte buttonPin =9; // Weld button
// Variables
byte prevENC_SWstate;
byte ENC_SWstate;
byte Inconfig; // Display config
int ENC_PinAState;
int ENC_PinALastState;
int Counter1;
int select;
int w1Value;
int w2Value;
unsignedlong TimeStamp;
unsignedlong ENC_SWpressDuration;
unsignedlong 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:");
lcd.setCursor(1, 1);
lcd.print("W2:");
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 != 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;
}
}
prevENC_SWstate = ENC_SWstate;
switch (select)
{
case0: //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(" ");
Serial.print("Case:");
Serial.println(select);
Serial.print("W1:");
Serial.println(w1Value);
break;
case1: //W2
clearSelectPointer(2);
lcd.setCursor(0, 1);
lcd.print(">");
w2Value =readEncoder() %10*10;
lcd.setCursor(1, 1);
lcd.print("W2:");
lcd.print(w2Value);
lcd.print(" ");
Serial.print("Case:");
Serial.println(select);
Serial.print("W2:");
Serial.println(w2Value);
break;
case2: //P
clearSelectPointer(3);
lcd.setCursor(8, 1);
lcd.print(">");
break;
case3:
lcd.setCursor(0, 0); // 1
lcd.print(" ");
lcd.setCursor(0, 1); // 2
lcd.print(" ");
lcd.setCursor(8, 1);
lcd.print(" ");
break;
}
staticint w2Value = w2Value;
}
}
voidrotaryEncoder()
{
ENC_PinAState =digitalRead(ENC_PinA);
if (digitalRead(ENC_PinB) != ENC_PinAState)
{
Counter1--;
}
else
{
Counter1++;
}
}
intreadEncoder()
{
noInterrupts();
int copyCounter = Counter1;
interrupts();
return (copyCounter) >>1;
}
intclearSelectPointer(int checkPointer)
{
switch (checkPointer)
{
case1: // for select 1
lcd.setCursor(0, 1); // 2
lcd.print(" ");
lcd.setCursor(8, 1); // 3
lcd.print(" ");
break;
case2: // for select 2
lcd.setCursor(0, 0); // 1
lcd.print(" ");
lcd.setCursor(8, 1); // 3
lcd.print(" ");
break;
case3: // for select 3
lcd.setCursor(0, 0); // 1
lcd.print(" ");
lcd.setCursor(0, 1); // 2
lcd.print(" ");
break;
}
return (checkPointer);
}