Using a rotary encoder with the function below, I would like to display some text before the user presses a button. After the user presses the button, the selected case would correspond to a function. Hopefully the attached code will explain what I'm trying to do..
Any help appreciated.
void setMenu() {
int encoderPos = 1;
while (digitalRead(sw1) == HIGH) {
boolean encoderA = digitalRead(encoderPinA);
if ((encoderALast == HIGH) && (encoderA == LOW)) {
if (digitalRead(encoderPinB) == LOW) {
encoderPos--;
encoderPos = constrain(encoderPos, 1, 2);
}
else {
encoderPos++;
encoderPos = constrain(encoderPos, 1, 2);
}
switch (encoderPos) {
case 1:
// I would like to display the word "START" and wait for the user to press the button attached to D2, then execute run_ramp(); //run_ramp() function
break;
case 2:
// I would like to display the word "LEARN" and wait for the user to press the button attached to D2, then execute learn(); //learn() function
break;
}
// do something on the TFT here
}
encoderALast = encoderA;
}
delay(100);
return;
}
// I would like to display the word "START" and wait for the user to press the button attached to D2, then execute run_ramp(); //run_ramp() function
So, what is the problem?
displaySomeHowSomeWhere("START");
while(digitalRead(2) == HIGH) // Assumes internal pullup resistors being used
{
// Do nothing but wait
}
run_ramp();
The problem is the routine gets stuck in the first case at the while.
What I really need is the display to update (tft.println(""); with the selected case statement, and change from case 1 to 2 text, but only execute the selected function upon d2 going low.
So, you want to completely separate reading the encoder/sw1 pins from reading the d1/d2 pins and the action that should occur when the d1 or d2 switch becomes pressed.
Again, what is the problem with doing that? Clearly, you do NOT want to do what your code actually says.
Your sig pretty much sums it up. The ability to ask the right question would lead inevitably to the right answer. Not knowing how to code this particular problem was the stumbling block for me, and you have provided enough feedback for the solution to be clear now. Thank you.
void setMenu() {
int encoderPos = 1;
int x = 0;
while (digitalRead(sw1) == HIGH) {
boolean encoderA = digitalRead(encoderPinA);
if ((encoderALast == HIGH) && (encoderA == LOW)) {
if (digitalRead(encoderPinB) == LOW) {
encoderPos--;
encoderPos = constrain(encoderPos, 1, 2);
}
else {
encoderPos++;
encoderPos = constrain(encoderPos, 1, 2);
}
switch (encoderPos) {
case 1:
Serial.println("START");
tft.println("START");
x = 1;
break;
case 2:
Serial.println("LEARN");
tft.println("LEARN");
x = 2;
break;
}
}
encoderALast = encoderA;
}
if (x <= 1) {
run_ramp();
return;
}
else {
learn();
return;
}
//delay(100);
return;
}