How to reset automatically generated variables

Hi,
I would like to reset at each start of the program the variables contained in the "Reset()" function, but it doesn't work.
I can't figure out why.
The sketch is as follows:

#include "arduino_secrets.h"
/**************************    v3.0   *******************************************************
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/544cfaeb-9ad1-42b1-8ba0-9e866c825cea 

  Arduino IoT Cloud Variables description

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

  int erogazioni;
  bool led14;
  bool led20;
  bool led8;

  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"
#include <Stepper.h>
#define STEPS 400

int ora = 17;
bool flag = true;
bool zero = true;

// steppr motor: IN1 D2(2), IN2 D3(3), IN3 D4(4), IN4 D5(5)
Stepper stepper(STEPS, 2, 3, 4, 5);
int start = 6;  // D6 pin 6
int val = 900;
int calval = 50;

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

  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
  led8 = false;
  led14 = false;
  led20 = false;
  pinMode(6, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  
  stepper.setSpeed(100);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
}

void eroga(){
  stepper.step(val);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  erogazioni++;
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
}

void Azzera(){
  erogazioni = 0;
  led8 = false;
  led14 = false;
  led20 = false;
  zero = false;
}

void loop() {
  ArduinoCloud.update();
    
  unsigned long now = ArduinoCloud.getLocalTime();  // scarica ora e minuti correnti
  time_t now_time = (time_t)now;
  struct tm* timeinfo = localtime(&now_time);
  Serial.println("now:" + String(timeinfo->tm_hour) + ":" + String(timeinfo->tm_min)+ ":" + String(timeinfo->tm_sec));
  
  if (((timeinfo->tm_hour) == ora) && ((timeinfo->tm_min) == 53) && ((timeinfo->tm_sec) == 10)) { 
    if(flag == true){
      eroga();;
      led8 = true;
      Serial.print(" ore 8 -->  ");
      Serial.println(erogazioni);
      flag = false;
      }
    }
   
  if (((timeinfo->tm_hour) == ora) && ((timeinfo->tm_min) == 54) && ((timeinfo->tm_sec) == 10)) { 
    if(flag == false){
      eroga();
      led14 = true;
      Serial.print(" ore 14 -->  ");
      Serial.println(erogazioni);
      flag = true;
      }
    }
  
  if (((timeinfo->tm_hour) == ora) && ((timeinfo->tm_min) == 55) && ((timeinfo->tm_sec) == 10)) {
    if(flag == true){
      eroga();
      led20 = true;
      Serial.print(" ore 20 -->  ");
      Serial.println(erogazioni);
      flag = false;
      }
    }
    
 if (((timeinfo->tm_hour) == ora) && ((timeinfo->tm_min) == 56) && ((timeinfo->tm_sec) == 10)) { 
    led8 = false;
    led14 = false;
    led20 = false;
    if(flag == false){
      Serial.println(" ore 23 -->  Azzeramento");
      flag = true;  // riabilita gli orari per il giorno dopo
    }
  } 
  if (zero == true){
    Azzera();  // Initially resets variables to zero
    }
}

/*
  Since Erogazioni is READ_WRITE variable, onErogazioniChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onErogazioniChange()  {
  // Add your code here to act upon Erogazioni change
}

/*
  Since Led8 is READ_WRITE variable, onLed8Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onLed8Change()  {
  // Add your code here to act upon Led8 change
}

/*
  Since Led14 is READ_WRITE variable, onLed14Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onLed14Change()  {
  // Add your code here to act upon Led14 change
}

/*
  Since Led20 is READ_WRITE variable, onLed20Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onLed20Change()  {
  // Add your code here to act upon Led20 change
}

EzioGi

Hello @eziogi!

When your board connects to IoT Cloud, variables are set to the last values stored in the cloud (this is useful to preserve state across reboots of the device), so they will override the ones you set in your Reset() function.

Try to add this line in your setup():

void setup() {
   ...
   ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, Reset);
   ...
}

It will call your Reset() function after the remote values have been received.

2 Likes

Thank you for the advice.

EzioGi

Put in setup and compiled the IDE tells me: "Reset" was not declared in this scope.

EzioGi

You mentioned a "Reset()" function in your original post, but in your code you probably renamed it with "Azzera". You need to adapt the example I provided to your actual code, using the name of the function where you reset your values :slight_smile:

(Unrelated to your question – looking at your code, it seems like you can achieve the same thing in a much simpler way using the Scheduler widget. See this tutorial)

Thanks for the advice.
I had seen the 'Scheduler' example but could not find the solution to my problem.
I am trying to create an automatic kibble dispenser for my cat when I am away from home.
I thought of 3 daily dispensations, one at 8am, one at 2pm and one at 8pm, then the sequence is repeated the following day.
The problem is that from 8pm to 8am the next day is 12 hours while the hours between dispensations are 6.
I have not yet found a satisfactory solution.

EzioGi

Now with your help resetting the variables works.
Thanks

EzioGi

Where can I find information similar to this:

ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, Reset);

EzioGi

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