I have the following code that is blinking two leds at random times (random on time and random off time for each led)
the problem is that both leds starting ON at the same time when starting the programm. How can I make this not to happen?
const int ledPin1 = 9; // Pin connected to the first LED
const int ledPin2 = 10; // Pin connected to the second LED
unsigned long led1OnTime = 0; // Stores the timestamp for turning off LED1
unsigned long led1OffTime = 0; // Stores the timestamp for turning on LED1
unsigned long led2OnTime = 0; // Stores the timestamp for turning off LED2
unsigned long led2OffTime = 0; // Stores the timestamp for turning on LED2
int led1State = LOW; // Stores the current state of LED1
int led2State = LOW; // Stores the current state of LED2
unsigned long led1Interval = 0; // Time interval for LED1 on/off in milliseconds
unsigned long led2Interval = 0; // Time interval for LED2 on/off in milliseconds
unsigned long previousMillis = 0; // Stores the previous timestamp
unsigned long led1MinOnTimeSec = 1; // Minimum on time for LED1 in seconds
unsigned long led1MaxOnTimeSec = 2; // Maximum on time for LED1 in seconds
unsigned long led1MinOffTimeSec = 1; // Minimum off time for LED1 in seconds
unsigned long led1MaxOffTimeSec = 2; // Maximum off time for LED1 in seconds
unsigned long led2MinOnTimeSec = 1; // Minimum on time for LED2 in seconds
unsigned long led2MaxOnTimeSec = 2; // Maximum on time for LED2 in seconds
unsigned long led2MinOffTimeSec = 1; // Minimum off time for LED2 in seconds
unsigned long led2MaxOffTimeSec = 2; // Maximum off time for LED2 in seconds
unsigned long randomInterval(unsigned long minIntervalSec, unsigned long maxIntervalSec) {
unsigned long minIntervalMillis = minIntervalSec * 1000;
unsigned long maxIntervalMillis = maxIntervalSec * 1000;
return random(minIntervalMillis, maxIntervalMillis + 1);
}
void printOnTime(int ledNumber, unsigned long onTime) {
Serial.print("LED");
Serial.print(ledNumber);
Serial.print(" turned on at ");
Serial.println(onTime);
}
void printOffTime(int ledNumber, unsigned long offTime) {
Serial.print("LED");
Serial.print(ledNumber);
Serial.print(" turned off at ");
Serial.println(offTime);
}
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
randomSeed(analogRead(A0)); // Seed the random number generator
Serial.begin(9600); // Initialize serial communication for printing
}
void loop() {
unsigned long currentMillis = millis();
// Check if it's time to turn on/off LED1
if (led1State == LOW && currentMillis - led1OffTime >= led1Interval) {
led1State = HIGH;
digitalWrite(ledPin1, led1State);
led1OnTime = currentMillis;
printOnTime(1, led1OnTime); // Print on time for LED1
led1Interval = randomInterval(led1MinOnTimeSec, led1MaxOnTimeSec); // Choose a new interval for LED1
} else if (led1State == HIGH && currentMillis - led1OnTime >= led1Interval) {
led1State = LOW;
digitalWrite(ledPin1, led1State);
led1OffTime = currentMillis;
printOffTime(1, led1OffTime); // Print off time for LED1
led1Interval = randomInterval(led1MinOffTimeSec, led1MaxOffTimeSec); // Choose a new interval for LED1
}
// Check if it's time to turn on/off LED2
if (led2State == LOW && currentMillis - led2OffTime >= led2Interval) {
led2State = HIGH;
digitalWrite(ledPin2, led2State);
led2OnTime = currentMillis;
printOnTime(2, led2OnTime); // Print on time for LED2
led2Interval = randomInterval(led2MinOnTimeSec, led2MaxOnTimeSec); // Choose a new interval for LED2
} else if (led2State == HIGH && currentMillis - led2OnTime >= led2Interval) {
led2State = LOW;
digitalWrite(ledPin2, led2State);
led2OffTime = currentMillis;
printOffTime(2, led2OffTime); // Print off time for LED2
led2Interval = randomInterval(led2MinOffTimeSec, led2MaxOffTimeSec); // Choose a new interval for LED2
}
}
Your using the same variable to determine the counter when each light turns on or off, so of course they will come on the same time. Why don't you create a separate currentMillis for the second LED and millis() + 1000 or whatever interval you want.
so my initial code was too complicated (generated by chatgpt)
I found this topic and the code there (message number 12#) seems much easier to get
I change the variable names so the code is looks like this:
unsigned long currentTime = millis();
const int led1 = 2; // pin for 1 led
unsigned long led1On; // OFF time for 1 led
unsigned long led1Off; // ON time for 1 led
int led1State = HIGH; // starting state of 1 led
unsigned long prevLedTime1; // time used by code to keep track of led 1 time
const int ledOnMinimum = 1000;
const int ledOnMaximum = 5000;
const int ledOffMinimum = 1000;
const int ledOffMaximum = 5000;
const int led2 = 3; // pin for 2 led
unsigned long led2On; // OFF time for 2 led
unsigned long led2Off; // ON time for 2 led
int led2State = HIGH; // starting state of 2 led
unsigned long prevLedTime2; //time used by code to keep track of led 1 time
void setup() {
pinMode(led1, OUTPUT); // sets led pin as output
digitalWrite(led1, led1State); // sets initial state
pinMode(led2, OUTPUT); // define led pin as output
digitalWrite(led2, led2State); // sets initial state
}
void loop() {
currentTime = millis();
led1On = random(ledOnMinimum, ledOnMaximum);
led1Off = random(ledOffMinimum, ledOffMaximum);
led2On = random(ledOnMinimum, ledOnMaximum);
led2Off = random(ledOffMinimum, ledOffMaximum);
if (led1State == HIGH) {
if (currentTime - prevLedTime1 >= led1On) {
led1State = LOW; // changes the state of led
prevLedTime1 = currentTime; // remembers current time
}
}
else {
if (currentTime - prevLedTime1 >= led1Off) {
led1State = HIGH; // changes the state of led
prevLedTime1 = currentTime; // remembers current time
}
}
digitalWrite(led1, led1State); // turns the led ON or OFF
if (led2State == HIGH) {
if (currentTime - prevLedTime2 >= led2On) {
led2State = LOW; // changes the state of led
prevLedTime2 = currentTime; // remembers current time
}
}
else {
if (currentTime - prevLedTime2 >= led2Off) {
led2State = HIGH; // changes the state of led
prevLedTime2 = currentTime; // remembers current time
}
}
digitalWrite(led2, led2State); // turns the led ON or OFF
}
but I still don't know how to solve the fact that both will be ON at the initial start of the code (when first powering the arduino)
unsigned long currentTime = millis();
const int led1 = 9; // pin for 1 led
unsigned long led1On; // OFF time for 1 led
unsigned long led1Off; // ON time for 1 led
int led1State = HIGH; // starting state of 1 led
unsigned long prevLedTime1; // time used by code to keep track of led 1 time
const int led1OnMinimum = 100;
const int led1OnMaximum = 400;
const int led1OffMinimum = 100;
const int led1OffMaximum = 800;
const int led2OnMinimum = 1000;
const int led2OnMaximum = 2000;
const int led2OffMinimum = 1000;
const int led2OffMaximum = 3000;
const int led2 = 10; // pin for 2 led
unsigned long led2On; // OFF time for 2 led
unsigned long led2Off; // ON time for 2 led
int led2State = HIGH; // starting state of 2 led
unsigned long prevLedTime2; //time used by code to keep track of led 1 time
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT); // sets led pin as output
digitalWrite(led1, led1State); // sets initial state
pinMode(led2, OUTPUT); // define led pin as output
digitalWrite(led2, led2State); // sets initial state
}
void loop() {
currentTime = millis();
led1On = random(led1OnMinimum, led1OnMaximum);
led1Off = random(led1OffMinimum, led1OffMaximum);
led2On = random(led2OnMinimum, led2OnMaximum);
led2Off = random(led2OffMinimum, led2OffMaximum);
if (led1State == HIGH) {
if (currentTime - prevLedTime1 >= led1On) {
led1State = LOW; // changes the state of led
Serial.print("led 1 off time: ");
Serial.println(led1Off);
prevLedTime1 = currentTime; // remembers current time
}
}
else {
if (currentTime - prevLedTime1 >= led1Off) {
led1State = HIGH; // changes the state of led
Serial.print("led 1 on time: ");
Serial.println(led1On);
prevLedTime1 = currentTime; // remembers current time
}
}
digitalWrite(led1, led1State); // turns the led ON or OFF
if (led2State == HIGH) {
if (currentTime - prevLedTime2 >= led2On) {
led2State = LOW; // changes the state of led
Serial.print("led 2 off time: ");
Serial.println(led2Off);
prevLedTime2 = currentTime; // remembers current time
}
}
else {
if (currentTime - prevLedTime2 >= led2Off) {
led2State = HIGH; // changes the state of led
Serial.print("led 2 on time: ");
Serial.println(led2On);
prevLedTime2 = currentTime; // remembers current time
}
}
digitalWrite(led2, led2State); // turns the led ON or OFF
}
This did not help rather make the loop stuck. I could just assign led1 to initial state HIGH and the other to LOW so one led will start HIGH and the other LOW