Problem with do...while statement

So I’ve been trapped with this code a while, i first discovered a hardware problem i had which burned out 2 analog pins for me and perhaps an lcd as well. Im now sure my hardware is connected perfectly well and I’m getting all the proper analog readings.

The purpose is having a menu interface with the lcd for a time-lapse motorised dolly I’m working on for my brother. As he moves a 3-way switch downwards he goes from one phase of the menu to another(thats the goal).

So i setup analog reading to tell me if the analog pins are reading the proper values(1023 and 0), and they are.

When the serial gives me its reading the proper conditions the lcd does execute the code inside the {} properly, it only gives me the second phase where the condition is: while(stage1 == 0 && stage2 == 20 ) ;
Yet the analogReading vary properly when moving the switch as i mentioned before.

And just a note, ignore the contents inside the {}, i still didn’t modify them to the proper formulas these are just here to test the lcd interface. And after the lcd burned out i added the Serial.prints.

Any help please, Thankyou.

Code:

#include <LiquidCrystal.h>
#include <Stepper.h>

    LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
      int const stepsPerRevolution = 200;
    Stepper motor(stepsPerRevolution, 7, 8, 9, 10);
      int stepCount = 0;
      int knob = A1;
      int confirmBut = A3; // Confirm Button
      int stage2 = 2; // second stage on switch
      int stage1 = 3; // 1st stage on switch
      int shutterInterval = 0;
      int offShutterInterval = 0;
      int offShutterSpeed = 0;
      int const high = 1023;

void setup() {
  lcd.begin(16,2);
  lcd.print("Welcome Mohamad");
  delay(1000);
  lcd.clear();
  Serial.begin(9600);
  motor.setSpeed(60); 
  pinMode(stage1, INPUT);
  pinMode(stage2, INPUT);
}

void loop() {

 do{
  int shutterIntervalVal = analogRead(knob);
  int shutterInterval = map(shutterIntervalVal,0,1023,0,120);
  shutterInterval = int(shutterInterval +0.5);
  lcd.setCursor(0,0);
  lcd.print("Set interval");
  Serial.print("Set interval");
  delay(500);
  lcd.setCursor(0,1);
  lcd.print(shutterInterval);
  Serial.print(shutterInterval);
  delay(1);
  Serial.print("    ");
  Serial.print(analogRead(A3));
  Serial.print("    ");
  Serial.print(analogRead(A4));
  Serial.print("    ");
  Serial.println(analogRead(A5));
  delay(1);
  if(confirmBut == 1023){  offShutterInterval = shutterInterval;
                          lcd.clear();
 }
 }while(stage1 == 1023 && stage2 == 0);


 do{
  int shutterSpeedVal = analogRead(knob);
  int shutterSpeed = map(shutterSpeedVal,0,1023,0,10);
  shutterSpeed = int(shutterSpeed +0.5);
  lcd.setCursor(0,0);
  lcd.print("Set shutter speed");
  Serial.print("Set shutter speed");
  delay(500);
  lcd.setCursor(0,1);
  lcd.print(shutterSpeed);
  Serial.print(shutterSpeed);
  delay(1);
  Serial.print("    ");
  Serial.print(analogRead(A3));
  Serial.print("    ");
  Serial.print(analogRead(A4));
  Serial.print("    ");
  Serial.println(analogRead(A5));
  delay(1);
  if(confirmBut == 1023){ offShutterSpeed = shutterSpeed; lcd.clear();}
  } while(stage1 == 0 && stage2 == 20 ) ;
  
do { int convShutterInterval = map(offShutterInterval,0,120,0,200); 
   convShutterInterval = int(convShutterInterval +0.5);
   motor.step(stepsPerRevolution);
  delay(offShutterSpeed + 100);
}while(stage2 == 1023 && stage1 == 0);
}

You have a common problem. Your WHILE loop is waiting for the variable stage1 to change but you have no code inside the WHILE to change the value.

And you have another common problem. You are mixing up the number of the input pin (which you call stage1) which will never change and the data collected from the pin which you don't seem to have any variable for.

Within your WHILE you need a line like
stage1Val = digitalRead(stage1);
and then your WHILE line should be
while(stage1Val == 1023 && stage2Val == 0)

I will leave it to you to figure out how to deal with stage2.

Alas, there is almost certainly another less easily solved problem. I would not use a WHILE loop at all because it blocks until it completes. If you look at the examples in planning and implementing a program and several things at a time you will see that they repeat lots of things without blocking.

...R

Also if either 1023 or 20 are analog readings, it's usually best to use greater than, less than, or a range. there is usually variation in analog signals and if either value is off by just one count, the condition won't be met. Even with digital counts, sometimes it's best to avoid the requirement of an exactly-equal value (just to make extra-sure you don't get stuck in a loop).

And, using Serial.print to display your variables (such as stage1 and stage2) can be very helpful when debugging. If you could see those values, I think you would have figured-out why the while-statement is "failing" and why your program is exiting the loop.

Robin2:
You have a common problem. Your WHILE loop is waiting for the variable stage1 to change but you have no code inside the WHILE to change the value.

And you have another common problem. You are mixing up the number of the input pin (which you call stage1) which will never change and the data collected from the pin which you don't seem to have any variable for.

Within your WHILE you need a line like
stage1Val = digitalRead(stage1);
and then your WHILE line should be
while(stage1Val == 1023 && stage2Val == 0)

I will leave it to you to figure out how to deal with stage2.

Alas, there is almost certainly another less easily solved problem. I would not use a WHILE loop at all because it blocks until it completes. If you look at the examples in planning and implementing a program and several things at a time you will see that they repeat lots of things without blocking.

...R

Thanks for the help you helped clear things up!