While loop crashing IOT 33

I have my code running in a loop for main but I want to add a while loop so I can "pause" the program when a switch is flipped on the cloud dashboard.

This works:

void loop() {
  ArduinoCloud.update();
  // Your code here
   
    if (i < 10)
    {
      i++;
      humidity = i;
    }
    if (i > 9)
    {
      i = 0 ;
      humidity = i;
    }
    if (temp > 75)
    {
      temp = 0;
    }
  }

This does not work:

void loop() {
  ArduinoCloud.update();
  // Your code here
   while (cycleRunning){
    if (i < 10)
    {
      i++;
      humidity = i;
    }
    if (i > 9)
    {
      i = 0 ;
      humidity = i;
    }
    if (temp > 75)
    {
      temp = 0;
    }
  }
}

cycleRunning is a bool
The only change is the while loop. The only thing outside of main is the auto generated variables for accessing the cloud. I have already read this thread. Any Ideas?

Before the last "}" matching the loop() "{" add

do {
} while (condition);

In your case, select a free digital pin.
Make the pin an input.
Turn on the pullup.

Ex:

const int buttonPin = 2;     // switch pin BEFORE setup()
 pinMode(buttonPin, INPUT_PULLUP);   //  added to setup()

Now:

do {
     delay(1);
} while ( ! buttonPin) ;

When the switch is OPEN, the code will run, when the switch is CLOSED, the code will temporarily hang.

void loop() {
  ArduinoCloud.update();
  // Your code here
   if (cycleRunning) {
    if (i < 10)  {
      i++;
      humidity = i;
    }
    if (i > 9)   {
      i = 0 ;
      humidity = i;
    }
    if (temp > 75)  {
      temp = 0;
    }
  }
}

Assuming the value of cycleRunning is set elsewhere...
simply change the while to an if()

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.