Hi community!
I have a question about how to reset a timer while code, depending on it is running. I'll give you an example. Please see the code below.
#include <Arduino.h>
// Pin definitions
const int ledPin1 = 4; // GPIO 4
const int ledPin2 = 2; // GPIO 2
const int ledPin17 = 17; // GPIO 17
unsigned long previousMillis = 0;
void setup() {
// Initialize LED pins as output
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin17, OUTPUT);
// Code executed only once
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin17, LOW);
delay(1000);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin17, HIGH);
delay(5000);
// Turn off the main lights, blinking blue indicates it starts soon
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
for (int i = 0; i < 1; i++) {
digitalWrite(ledPin17, LOW);
delay(1000);
digitalWrite(ledPin17, HIGH);
delay(3000);
}
for (int i = 0; i < 3; i++) {
digitalWrite(ledPin17, LOW);
delay(1000);
digitalWrite(ledPin17, HIGH);
delay(1000);
}
for (int i = 0; i < 10; i++) {
digitalWrite(ledPin17, LOW);
delay(500);
digitalWrite(ledPin17, HIGH);
delay(500);
}
previousMillis = millis(); // Start the timer for the sequence
}
void loop() {
unsigned long currentMillis = millis();
// Calculate the time elapsed since the code started
unsigned long elapsedMillis = currentMillis - previousMillis;
// Turn on LedPin1 for 20 seconds, every after 50 seconds
if (elapsedMillis >= 50000 && elapsedMillis <= 70000 ) {
digitalWrite(ledPin1, HIGH); // Turn on LedPin1
} else if (elapsedMillis < 50000 || elapsedMillis > 70000) {
digitalWrite(ledPin1, LOW); // Turn off LedPin1
}
// Turn on LedPin2 for 20 seconds after 110 seconds
if (elapsedMillis >= 110000 && elapsedMillis <= 130000) {
digitalWrite(ledPin2, HIGH); // Turn on LedPin2
} else if (elapsedMillis < 110000 || elapsedMillis > 130000) {
digitalWrite(ledPin2, LOW); // Turn off LedPin2
}
// Reset the timer after 120 seconds
if (elapsedMillis >= 120000) {
previousMillis = currentMillis;
}
}
Basically, I have a separate ESP32-CAM that takes images every 60 seconds and I want to fix the lighting. On a separate ESP32-Dev I have installed two LEDs at separate GPIOs. One Led (LedPin1) should turn on 10 seconds before the camera takes an image and turn off 10 seconds after. The other LED (LedPin2) should do the same for the second image and the loop continues.
The first part of the code (in the Setup()) should only be executed once (this is so I have time to prepare the camera). After this part, the timer should start and 50 seconds after it enters the loop LedPin1 should be turned on.
This part of the code works. However, I get into problems when I have to restart the timer. If I restart it after 120 seconds so that LedPin1 is turned on again 10 seconds before the camera takes an image - the LedPin2 is turned off 10 seconds before it should (after 120 seconds instead of 130 seconds.
How can I restart the timer even if the code running is dependent on it?
Unrelated to this topic but I'm just curious, do you know any way that I can link my ESP32-Dev (that controls the light) with my ESP32-CAM (that takes images) so that I have one code for everything?
Best Regards!