|
4 | 4 | #include "Arduino_Portenta_C33_LowPower.h"
|
5 | 5 |
|
6 | 6 | LowPower lowPower;
|
7 |
| - |
8 |
| - |
9 | 7 | RTCTime initialTime(1, Month::JANUARY, 2000, 12, 10, 00, DayOfWeek::TUESDAY, SaveLight::SAVING_TIME_ACTIVE);
|
10 | 8 |
|
11 | 9 |
|
12 | 10 | void alarmCallback()
|
13 | 11 | {
|
14 |
| - digitalWrite(LED_BUILTIN, LOW); |
15 |
| - delay(1000); |
16 | 12 | digitalWrite(LED_BUILTIN, HIGH);
|
17 |
| - lowPower.deepSleep(); |
| 13 | + delay(1000); |
| 14 | + digitalWrite(LED_BUILTIN, LOW); |
| 15 | + lowPower.deepSleep(); |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +bool sleepFor(int hours, int minutes, int seconds){ |
| 20 | +{ |
| 21 | + |
| 22 | + RTCTime currentTime; |
| 23 | + if (!RTC.getTime(currentTime)) { |
| 24 | + Serial.println("Failed to get current time"); |
| 25 | + return false; // Failed to get current time |
| 26 | + } |
| 27 | + Serial.println(currentTime.toString()); |
| 28 | + |
| 29 | + // Convert current time to UNIX timestamp and add the desired interval |
| 30 | + time_t currentTimestamp = currentTime.getUnixTime(); |
| 31 | + currentTimestamp += hours * 3600 + minutes * 60 + seconds; |
| 32 | + // Convert back to RTCTime |
| 33 | + RTCTime alarmTime(currentTimestamp); |
| 34 | + // Configure the alarm match criteria |
| 35 | + AlarmMatch match; |
| 36 | + match.addMatchSecond(); // Trigger the alarm when the seconds match |
| 37 | + match.addMatchMinute(); // Trigger the alarm when the minutes match |
| 38 | + match.addMatchHour(); // Trigger the alarm when the hours match |
| 39 | + // Set the alarm callback (assuming you have a callback function named alarmCallback) |
| 40 | + if (!RTC.setAlarmCallback(alarmCallback, alarmTime, match)) { |
| 41 | + Serial.println("Failed to set the alarm"); |
| 42 | + return false; // Failed to set the alarm |
| 43 | + } |
| 44 | + |
| 45 | + // Enable the alarm |
| 46 | + Serial.println("Enabling the alarm"); |
| 47 | + return true; |
| 48 | + } |
18 | 49 | }
|
19 | 50 |
|
20 | 51 |
|
21 | 52 | void setup(){
|
| 53 | + Serial.begin(9600); |
| 54 | + //while(!Serial); |
| 55 | + |
22 | 56 | lowPower = LowPower();
|
23 |
| - // lowPower.enableWakeupFromRTC(&initialTime, &callback); |
24 | 57 | lowPower.enableWakeupFromRTC();
|
25 | 58 |
|
26 | 59 | pinMode(LED_BUILTIN, OUTPUT);
|
27 |
| - digitalWrite(LED_BUILTIN, HIGH); |
| 60 | + digitalWrite(LED_BUILTIN, LOW); |
28 | 61 |
|
29 | 62 | RTC.begin();
|
30 | 63 |
|
31 |
| - if (!RTC.isRunning()) RTC.setTime(initialTime); |
32 |
| - |
33 |
| - // Trigger the alarm every time the seconds are zero |
34 |
| - RTCTime alarmTime; |
35 |
| - alarmTime.setSecond(0); |
36 |
| - |
37 |
| - // Make sure to only match on the seconds in this example - not on any other parts of the date/time |
38 |
| - AlarmMatch matchTime; |
39 |
| - matchTime.addMatchSecond(); |
40 |
| - |
41 |
| - //sets the alarm callback |
42 |
| - RTC.setAlarmCallback(alarmCallback, alarmTime, matchTime); |
| 64 | + if (!RTC.isRunning()) { |
| 65 | + RTC.setTime(initialTime); |
| 66 | + sleepFor(0, 0, 1); |
| 67 | + } |
43 | 68 |
|
44 | 69 | }
|
45 | 70 |
|
|
0 commit comments