Issue when adding using While loop

Hi all,

I'm relatively new to programing, I'm trying to write a code that would work with Cloud Iot on/off switch and also be able to turn pump motor on/off for certain amount of time. However, when I add the While loop shown below the Arduino board starts to disconnect from the computer. I'm using the MKR WiFI 1010 board. Any help or ideas would be greatly appreciated.

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/cfba017b-b562-44e9-9f27-a4ba2992a7ee 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  CloudLight pump;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

const unsigned long OnTime = 60000;
const unsigned long OffTime = 60000;
unsigned long PreviousTime = 0;
unsigned long CurrentTime = 0;

void setup() {
  // Initialize serial and wait for port to open:
  pinMode(2,OUTPUT);
  digitalWrite(2,HIGH);
  
  
  
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  
  CurrentTime = millis();
  //Serial.println(CurrentTime);
  
    
  if (CurrentTime - PreviousTime <= OffTime) {
    digitalWrite(2,HIGH);
    //Serial.println("off");

   } else {
   PreviousTime = CurrentTime;
   //Serial.println("PreviousTime");

   while (CurrentTime - PreviousTime <= OnTime) {
    digitalWrite(2, LOW);
    CurrentTime = millis();
   }

   PreviousTime = CurrentTime;

   }

   }
   

/*
  Since Pump is READ_WRITE variable, onPumpChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPumpChange()  {
  // Add your code here to act upon Pump change
  if (pump == 1)  { digitalWrite(2, LOW);}
  else {digitalWrite (2, HIGH);}
}






Try putting parenthesis around the part you want evaluated first.ie:
while ((CurrentTime -PreviousTime) <= OnTime){

In addition to what @Paul_KD7HB said the while will completely stall your program for 60 seconds, i.e., your code can't do anything else for 60 seconds.

The above code is essentially equivalent to:

    digitalWrite(2, LOW);
    delay(OnTime);

it change nothing

Suggest you avoid while( ) loops like the plague.

Always strive to write your sketches in a non-blocking fashion.

Only use while loops if you know why you shouldn’t.

All IoT Cloud sketches use a Watchdog Timer (WDT) by default. The WDT can be used to automatically recover from hardware faults or unrecoverable software errors. If you don't call ArduinoCloud.update() frequently enough the the WDT can timeout and reset your board. Your while loop causes ArduinoCloud.update() to not get called for 60 seconds.

void onPumpChange()  {
  digitalWrite(2, !pump);
}

thank you all, I'll have to find a different method of turning the motor on and keeping it on for x amount of time and not use any loops

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