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);}
}