Adding an else after a while?

Is there a way to have an else-type statement that will run if a while loop doesn't print its values? For example, I have this code that will check the input of potInput1, and if this is greater than 800, 1 will be added to result. If there is no potInput1>800, I'm adding -1 to the result. Is there a way to write this such that the -1 command doesn't happen if the +1 in the while loop happens?

int result = 0;
boolean lastButton = LOW;
boolean currentButton;
unsigned long delayCount = 2000
void setup() {
  Serial.begin(9600);
}

void loop() {
  currentButton = digitalRead(6);
  if(lastButton != currentButton)
  {
    delay(5);
    currentButton = digitalRead(6);
  }
  if (currentButton == LOW)
  {
  currentMillis = millis();
  while(millis()-currentMillis < delayCount)
    int potInput1 = analogRead(A1);
    if(potInput1 > 800){
      result = result + 1;
    }
  else{
    result = result - 1;
  }
}
}

is there an if(millis()-currentMillis >= delayCount && [while doesn't print])-type command I can replace my else with?

Hey mecheng,

I'm not completely sure what you're problem is, I will attempt to answer it. I don't think you need a conditional statement here, as millis() - currentMillis < delayCount will always become false at some point (given that time is continuous). To check whether potInput was > 800 at some point, you can add a bool variable than becomes true if (potInput1 >800).

I hope this helps.

Kind regards,
Alex

Regardless of the code, and just to answer the question, No the while() iteration statement does not have an else version like the if conditional statement has.

you could do something like this though:

if (condition) {
  while (condition) {
    // do something that updates condition
  }
} else {
  // you did not even enter the while loop
}

if you want to enter the while() clause at least once regardless of the status of the condition the first time, then you use the do-while iteration statement

do {
    // do something that updates condition
} while (condition);

that's what the code seems to do, but the code is confusing and has compile errors

    currentButton = digitalRead(6);
    if(lastButton != currentButton)
    {
        delay(5);
        currentButton = digitalRead(6);
    }

the above looks likes an attempt to recognize a change in button state, but lastButton is never updated and when the condition is true, currentButton is simply updated

    if (currentButton == LOW)

regardless of the change in state -- the above condition is true as long as the button is pressed

        currentMillis = millis();
        while (millis()-currentMillis < delayCount)
            int potInput1 = analogRead(A1);

the above will repeatedly read the analog input for delayCount (2 secs). but since potinput1 is defined within the while loop, it's value can't be accessed outside the loop, resulting in an error when it is referenced outside the loop.

currentMilliis is also undefined.

consider the following which compile and may do what you're attempting. when the button is pressed, it repeatedly reads the analog input for 2 seconds, updating the result with +1 when > 800 and -1 when <= 800

#undef MyHW
#ifdef MyHW
const byte PinBut  = A1;
const byte PinAnlg = A0;
#else
const byte PinBut  = 6;
const byte PinAnlg = A1;
#endif


int      result = 0;
int      potInput1;
boolean  butLst;
const unsigned long   DelayCount = 2000;

void setup() {
    Serial.begin(9600);

    pinMode (PinBut, INPUT_PULLUP);
    butLst = digitalRead (PinBut);
}

void loop() {
    byte but = digitalRead (PinBut);

    // check for button change of state
    if (butLst != but) {
        butLst = but;       // capture new state

        if (but == LOW) {   // if pressed
            // repeatedly read input and updating count
            unsigned long msec = millis();
            while ( (millis() - msec) < DelayCount)  {
                potInput1 = analogRead(PinAnlg);

                if(potInput1 > 800){
                    result = result + 1;
                }
                else{
                    result = result - 1;
                }
            }

            Serial.print   (potInput1);
            Serial.print   (" ");
            Serial.println (result);
        }
    }
}

From the code, it looks like what you are trying to do is: When a button is pressed, if the pot reads above 800 any time in the next 'delayCount' milliseconds, increment 'result', else decrement 'result'.

If that is what you wanted, here is one way to do it:

  if (currentButton == LOW)
  {
    currentMillis = millis();
    boolean increment = false;
    while (millis() - currentMillis < delayCount)
    {
      int potInput1 = analogRead(A1);

      if (potInput1 > 800)
        increment = true;
    }
    
    if (increment)
      result++;
    else
      result--;
  }