Hi there
I am building a dog feeder and I have some code where one defines the time that the dog is fed. When I try and change that variable on the dashboard it sets itself back to the default 00:00 after a few seconds. This error only occurs on the web dashboard. Here is my code:
/*
Sketch generated by the Arduino IoT Cloud Thing "Dog Feeder"
https://create.arduino.cc/cloud/things/6741eca9-a42c-4944-b823-7aa7c93f386f
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float breakfastAmount;
float dinnerAmount;
float foodLevel;
float lunchAmount;
int numberOfDogs;
bool screwLevelWarning;
CloudTime breakfast;
CloudTime dinner;
CloudTime lunch;
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 <AccelStepper.h>
// MODE, STEP, DIR
AccelStepper stepper1(1, 3, 4);
AccelStepper stepper2(1, 5, 6);
//const int screwLevelPin = 7;
//const int foodLevelPin = 8;
const int zeroPositionPin = 9;
bool isFeeding = false;
bool breakfastFed = false;
bool lunchFed = false;
bool dinnerFed = false;
bool atZeroPosition;
float screwLevel;
std::pair<int, int> addSecondsToTime(long secondsToAdd) {
int startHour = 0;
int startMinute = 0;
long totalMinutes = secondsToAdd / 60;
int newMinute = (startMinute + totalMinutes) % 60;
int newHour = (startHour + totalMinutes / 60) % 24;
return std::make_pair(newHour, newMinute);
}
void setup() {
// Initialize serial and wait for port to open:
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();
stepper1.setMaxSpeed(100);
stepper1.setAcceleration(20);
stepper2.setMaxSpeed(100);
stepper2.setAcceleration(20);
pinMode(zeroPositionPin, INPUT);
}
void loop() {
ArduinoCloud.update();
// Your code here
//screwLevel = complexDistanceCalculationsIAmTooLasyToDo();
//foodLevel = complexDistanceCalculationsIAmTooLasyToDo();
/*if (screwLevel > 10) {
screwLevelWarning = true;
} else {
screwLevelWarning = false;
}*/
checkFeedTime(breakfast, breakfastAmount, breakfastFed);
checkFeedTime(lunch, lunchAmount, lunchFed);
checkFeedTime(dinner, dinnerAmount, dinnerFed);
while (isFeeding) {
stepper1.run();
if (stepper1.distanceToGo() == 0) {
isFeeding = false;
break;
}
}
}
void checkFeedTime(CloudTime feedTime, float amount, bool& fed) {
time_t currentTime = ArduinoCloud.getLocalTime();
struct tm *currentTimeInfo = localtime(¤tTime);
std::pair<int, int> formattedFeedTime = addSecondsToTime(feedTime);
Serial.println("currentTime:");
Serial.println(currentTime); // Print raw current time
Serial.println(currentTimeInfo->tm_hour);
Serial.println(currentTimeInfo->tm_min);
Serial.println("feedTime:");
Serial.println(feedTime); // Print raw feed time
Serial.println(formattedFeedTime.first);
Serial.println(formattedFeedTime.second);
if (fed && currentTimeInfo->tm_min > formattedFeedTime.second) {
fed = false;
} else if (fed) {
} else {
if (formattedFeedTime.first == currentTimeInfo->tm_hour && formattedFeedTime.second == currentTimeInfo->tm_min) {
stepper1.moveTo(stepper1.currentPosition() + amount);
isFeeding = true;
fed = true;
}
}
}
void unloadFood() {
}
void loadFood() {
}
void calibrateStepper2() {
}
/*
Since Breakfast is READ_WRITE variable, onBreakfastChange() is
executed every time a new value is received from IoT Cloud.
*/
void onBreakfastChange() {
// Add your code here to act upon Breakfast change
checkFeedTime(breakfast, breakfastAmount, breakfastFed);
}
/*
Since Lunch is READ_WRITE variable, onLunchChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLunchChange() {
// Add your code here to act upon Lunch change
checkFeedTime(lunch, lunchAmount, lunchFed);
}
/*
Since Dinner is READ_WRITE variable, onDinnerChange() is
executed every time a new value is received from IoT Cloud.
*/
void onDinnerChange() {
// Add your code here to act upon Dinner change
checkFeedTime(dinner, dinnerAmount, dinnerFed);
}
/*
Since BreakfastAmount is READ_WRITE variable, onBreakfastAmountChange() is
executed every time a new value is received from IoT Cloud.
*/
void onBreakfastAmountChange() {
// Add your code here to act upon BreakfastAmount change
}
/*
Since LunchAmount is READ_WRITE variable, onLunchAmountChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLunchAmountChange() {
// Add your code here to act upon LunchAmount change
}
/*
Since DinnerAmount is READ_WRITE variable, onDinnerAmountChange() is
executed every time a new value is received from IoT Cloud.
*/
void onDinnerAmountChange() {
// Add your code here to act upon DinnerAmount change
}
/*
Since NumberOfDogs is READ_WRITE variable, onNumberOfDogsChange() is
executed every time a new value is received from IoT Cloud.
*/
void onNumberOfDogsChange() {
// Add your code here to act upon NumberOfDogs change
}
/*
Since FoodLevel is READ_WRITE variable, onFoodLevelChange() is
executed every time a new value is received from IoT Cloud.
*/
void onFoodLevelChange() {
// Add your code here to act upon FoodLevel change
}
/*
Since ScrewLevelWarning is READ_WRITE variable, onScrewLevelWarningChange() is
executed every time a new value is received from IoT Cloud.
*/
void onScrewLevelWarningChange() {
// Add your code here to act upon ScrewLevelWarning change
}
Dashboard:
Thank you in advance for all the help.