Skip to content

Commit 07c6a95

Browse files
authored
Create esp32ontime.md
1 parent 8c65c86 commit 07c6a95

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

Programs/esp32sleep/esp32ontime.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
If you need to track the device's on-time, you can utilize the `millis()` function in Arduino to measure the elapsed time since the device was powered on. The `millis()` function returns the number of milliseconds that have passed since the device started running.
2+
3+
Here's an example code snippet that demonstrates how to use `millis()` to track the on-time of the device:
4+
5+
```cpp
6+
unsigned long startTime; // Variable to store the start time
7+
8+
void setup() {
9+
Serial.begin(115200); // Initialize the serial communication
10+
startTime = millis(); // Record the start time
11+
}
12+
13+
void loop() {
14+
unsigned long elapsedTime = millis() - startTime; // Calculate the elapsed time
15+
16+
// Print the on-time in different units (seconds, minutes, hours)
17+
Serial.print("On-time: ");
18+
Serial.print(elapsedTime / 1000); // Convert milliseconds to seconds
19+
Serial.print(" seconds (");
20+
Serial.print(elapsedTime / (1000 * 60)); // Convert milliseconds to minutes
21+
Serial.print(" minutes or ");
22+
Serial.print(elapsedTime / (1000 * 60 * 60)); // Convert milliseconds to hours
23+
Serial.println(" hours)");
24+
25+
delay(1000); // Delay for 1 second before printing the on-time again
26+
}
27+
```
28+
29+
In the above code, the `startTime` variable is initialized with the value of `millis()` in the `setup()` function. Then, in the `loop()` function, the elapsed time is calculated by subtracting `startTime` from the current value of `millis()`. The elapsed time is then printed in seconds, minutes, and hours using the Serial communication. The `delay(1000)` function is used to introduce a 1-second delay between each printout.
30+
31+
By running this code on your ESP32 device, you can monitor the on-time of the device through the serial monitor in your Arduino IDE or a serial communication program.
32+
33+
------------------------------------------------------------------
34+
35+
If you want to display the time on an ESP32 device only when the minute value changes, you can modify the code to keep track of the previous minute value and compare it with the current minute value. Here's an example:
36+
37+
```cpp
38+
unsigned long startTime; // Variable to store the start time
39+
int previousMinute = -1; // Variable to store the previous minute value
40+
41+
void setup() {
42+
Serial.begin(115200); // Initialize the serial communication
43+
startTime = millis(); // Record the start time
44+
}
45+
46+
void loop() {
47+
unsigned long elapsedTime = millis() - startTime; // Calculate the elapsed time
48+
49+
// Convert milliseconds to minutes
50+
int minutes = elapsedTime / (1000 * 60);
51+
52+
// Check if the minute value has changed
53+
if (minutes != previousMinute) {
54+
previousMinute = minutes; // Update the previous minute value
55+
56+
// Print the current time
57+
Serial.print("Time: ");
58+
Serial.print(minutes / 60); // Print hours
59+
Serial.print(":");
60+
if (minutes % 60 < 10) {
61+
Serial.print("0"); // Print leading zero if minute value is less than 10
62+
}
63+
Serial.print(minutes % 60); // Print minutes
64+
Serial.println();
65+
}
66+
67+
// Other code and operations here...
68+
69+
delay(1000); // Delay for 1 second before checking the time again
70+
}
71+
```
72+
73+
In this code, the variable `previousMinute` is initially set to -1 to ensure that the first time check will always be true. Inside the `loop()` function, the `minutes` variable is calculated by dividing the elapsed time by the number of milliseconds in a minute. Then, it compares the current minute value with the `previousMinute` value. If they are different, it means that the minute has changed, and the current time is printed. The `previousMinute` is then updated to the current minute value.
74+
75+
You can modify the code according to your requirements and add additional functionality or conditions as needed within the loop.

0 commit comments

Comments
 (0)