11/*
2- This sketch demonstrates how to use cloud schedule data type.
2+ This sketch demonstrates how to use the cloud schedule variable type.
33
4- This sketch is compatible with:
4+ This sketch is compatible with the following boards :
55 - MKR 1000
66 - MKR WIFI 1010
77 - MKR GSM 1400
@@ -19,7 +19,7 @@ static int const LED_BUILTIN = 2;
1919#endif
2020
2121void setup () {
22- /* Initialize serial and wait up to 5 seconds for port to open */
22+ /* Initialize the serial port and wait up to 5 seconds for a connection */
2323 Serial.begin (9600 );
2424 for (unsigned long const serialBeginTime = millis (); !Serial && (millis () - serialBeginTime > 5000 ); ) { }
2525
@@ -35,20 +35,195 @@ void setup() {
3535 setDebugMessageLevel (DBG_INFO);
3636 ArduinoCloud.printDebugInfo ();
3737
38- /* Configure a schedule for LED. This should be done with Arduino create Scheduler widget */
39- unsigned int startingFrom = 1635786000 ; /* From 01/11/2021 17:00 */
40- unsigned int untilTo = startingFrom + ( DAYS * 28 ); /* To 29/11/2021 17:00 */
41- unsigned int executionPeriod = MINUTES * 6 ; /* For 6 minutes */
42- unsigned int scheduleConfiguration = 134217770 ; /* On monday wednesday and friday */
38+ /* Setup one shot schedule example */
39+ setupOneShotSchedule ();
4340
44- led = Schedule (startingFrom, untilTo, executionPeriod, scheduleConfiguration);
41+ /* Setup per minute schedule example */
42+ setupMinuteSchedule ();
43+
44+ /* Setup hourly schedule example */
45+ setupHourlySchedule ();
46+
47+ /* Setup daily schedule example */
48+ setupDailySchedule ();
49+
50+ /* Setup weekly schedule example */
51+ setupWeeklySchedule ();
52+
53+ /* Setup monthly schedule example */
54+ setupMonthlySchedule ();
55+
56+ /* Setup yearly schedule example */
57+ setupYearlySchedule ();
58+ }
59+
60+ /* Setup a schedule with an active period of 5 minutes that doesn't repeat
61+ * Starting from 2021 11 01 17:00:00
62+ * Until 2021 11 02 17:00:00
63+ */
64+ void setupOneShotSchedule () {
65+
66+ unsigned int startingFrom = Schedule::getTimeFromString (" 2021 Nov 01 17:00:00" );
67+ unsigned int until = startingFrom + ( DAYS * 1 );
68+ unsigned int activePeriod = MINUTES * 5 ;
69+
70+ /* Warning: there is no cross check between until and activePeriod */
71+ unsigned int scheduleConfiguration = Schedule::createOneShotScheduleConfiguration ();
72+
73+ oneShot = Schedule (startingFrom, until, activePeriod, scheduleConfiguration);
74+ }
75+
76+ /* Setup a schedule with an active period of 15 seconds that repeats each minute
77+ * Starting from 2021 11 01 17:00:00
78+ * Until 2021 11 02 17:00:00
79+ */
80+ void setupMinuteSchedule () {
81+
82+ unsigned int startingFrom = Schedule::getTimeFromString (" 2021 Nov 01 17:00:00" );
83+ unsigned int until = startingFrom + ( DAYS * 1 );
84+ unsigned int activePeriod = SECONDS * 15 ;
85+ unsigned int repetitionPeriod = 1 ;
86+
87+ /* Warning: there is no cross check between repetitionPeriod and activePeriod */
88+ unsigned int scheduleConfiguration = Schedule::createFixedDeltaScheduleConfiguration (ScheduleUnit::Minutes, repetitionPeriod);
89+
90+ minute = Schedule (startingFrom, until, activePeriod, scheduleConfiguration);
91+ }
92+
93+ /* Setup a schedule with an active period of 20 minutes that repeats each hour
94+ * Starting from 2021 11 01 17:00:00
95+ * Until 2021 11 15 13:00:00
96+ */
97+ void setupHourlySchedule () {
98+
99+ unsigned int startingFrom = Schedule::getTimeFromString (" 2021 Nov 01 17:00:00" );
100+ unsigned int until = Schedule::getTimeFromString (" 2021 Nov 15 13:00:00" );
101+ unsigned int activePeriod = MINUTES * 20 ;
102+ unsigned int repetitionPeriod = 1 ;
103+
104+ /* Warning: there is no cross check between repetitionPeriod and activePeriod */
105+ unsigned int scheduleConfiguration = Schedule::createFixedDeltaScheduleConfiguration (ScheduleUnit::Hours, repetitionPeriod);
106+
107+ hourly = Schedule (startingFrom, until, activePeriod, scheduleConfiguration);
108+ }
109+
110+ /* Setup a schedule with an active period of 2 hours that repeats each day
111+ * Starting from 2021 11 01 17:00:00
112+ * Until 2021 11 15 13:00:00
113+ */
114+ void setupDailySchedule () {
115+
116+ unsigned int startingFrom = Schedule::getTimeFromString (" 2021 Nov 01 17:00:00" );
117+ unsigned int until = Schedule::getTimeFromString (" 2021 Nov 15 13:00:00" );
118+ unsigned int activePeriod = HOURS * 2 ;
119+ unsigned int repetitionPeriod = 1 ;
120+
121+ /* Warning: there is no cross check between repetitionPeriod and activePeriod */
122+ unsigned int scheduleConfiguration = Schedule::createFixedDeltaScheduleConfiguration (ScheduleUnit::Days, repetitionPeriod);
123+
124+ daily = Schedule (startingFrom, until, activePeriod, scheduleConfiguration);
125+ }
126+
127+ /* Setup a schedule with an active period of 3 minutes with a weekly configuration
128+ * Starting from 2021 11 01 17:00:00
129+ * Until 2021 11 31 17:00:00
130+ * Weekly configuration
131+ * Sunday -> Inactive
132+ * Monday -> Active
133+ * Tuesday -> Inactive
134+ * Wednesday -> Active
135+ * Thursday -> Inactive
136+ * Friday -> Active
137+ * Saturday -> Inactive
138+ */
139+ void setupWeeklySchedule () {
140+ unsigned int startingFrom = Schedule::getTimeFromString (" 2021 Nov 01 17:00:00" );
141+ unsigned int until = startingFrom + ( DAYS * 30 );
142+ unsigned int executionPeriod = MINUTES * 3 ;
143+
144+ ScheduleWeeklyMask WeeklyMask = {
145+ ScheduleState::Inactive, /* Sunday */
146+ ScheduleState::Active, /* Monday */
147+ ScheduleState::Inactive, /* Tuesday */
148+ ScheduleState::Active, /* Wednesday */
149+ ScheduleState::Inactive, /* Thursday */
150+ ScheduleState::Active, /* Friday */
151+ ScheduleState::Inactive, /* Saturday */
152+ };
153+
154+ unsigned int scheduleConfiguration = Schedule::createWeeklyScheduleConfiguration (WeeklyMask);
155+
156+ weekly = Schedule (startingFrom, until, executionPeriod, scheduleConfiguration);
157+ }
158+
159+ /* Setup a schedule with an active period of 1 day that repeats each third day of the month
160+ * Starting from 2021 11 01 17:00:00
161+ * Until 2022 11 15 13:00:00
162+ */
163+ void setupMonthlySchedule () {
164+
165+ unsigned int startingFrom = Schedule::getTimeFromString (" 2021 Nov 01 17:00:00" );
166+ unsigned int until = Schedule::getTimeFromString (" 2021 Nov 15 13:00:00" );
167+ unsigned int activePeriod = DAYS * 1 ;
168+ unsigned int dayOfMonth = 3 ;
169+
170+ unsigned int scheduleConfiguration = Schedule::createMonthlyScheduleConfiguration (dayOfMonth);
171+
172+ monthly = Schedule (startingFrom, until, activePeriod, scheduleConfiguration);
173+ }
174+
175+
176+ /* Setup a schedule with an active period of 2 days that repeats each year on November 6th
177+ * Starting from 2021 11 06 17:00:00
178+ * Until 2041 11 15 13:00:00
179+ */
180+ void setupYearlySchedule () {
181+
182+ unsigned int startingFrom = Schedule::getTimeFromString (" 2021 Nov 06 17:00:00" );
183+ unsigned int until = Schedule::getTimeFromString (" 2041 Nov 06 13:00:00" );
184+ unsigned int activePeriod = DAYS * 2 ;
185+ unsigned int dayOfMonth = 6 ;
186+
187+ unsigned int scheduleConfiguration = Schedule::createYearlyScheduleConfiguration (ScheduleMonth::Nov, dayOfMonth);
188+
189+ yearly = Schedule (startingFrom, until, activePeriod, scheduleConfiguration);
45190}
46191
47192void loop () {
48193 ArduinoCloud.update ();
49194
50- /* Activate LED when schedule is active */
51- digitalWrite (LED_BUILTIN, led.isActive ());
195+ /* Print a message when the oneShot schedule is active */
196+ if (oneShot.isActive ()) {
197+ Serial.println (" One shot schedule is active" );
198+ }
199+
200+ /* Print a message when the per minute schedule is active */
201+ if (minute.isActive ()) {
202+ Serial.println (" Per minute schedule is active" );
203+ }
204+
205+ /* Print a message when the hourly schedule is active */
206+ if (hourly.isActive ()) {
207+ Serial.println (" Hourly schedule is active" );
208+ }
209+
210+ /* Print a message when the daily schedule is active */
211+ if (daily.isActive ()) {
212+ Serial.println (" Daily schedule is active" );
213+ }
52214
215+ /* Activate LED when the weekly schedule is active */
216+ digitalWrite (LED_BUILTIN, weekly.isActive ());
217+
218+ /* Print a message when the monthly schedule is active */
219+ if (monthly.isActive ()) {
220+ Serial.println (" Monthly schedule is active" );
221+ }
222+
223+ /* Print a message when the yearly schedule is active */
224+ if (yearly.isActive ()) {
225+ Serial.println (" Yearly schedule is active" );
226+ }
227+
53228}
54229
0 commit comments