for (int i=0; i<=99; i++){ //Loops for 100 times
digitalWrite(LED1,HIGH); //Turn on LED1
delay(200); //Delay for 200ms
digitalWrite(LED1,LOW); //Turn off LED1
delay(200); //Delay for 200ms
}
for (int i=0; i<59; i++){ //Loops for 60 times
digitalWrite(LED2,HIGH); //Turn on LED2
delay(500); //Delays for 500ms
digitalWrite(LED2,LOW); //Turn off LED2
delay(500); //Delay for 500ms
digitalWrite(LED3,HIGH); //Turn on LED3
delay(500); //Delay for 500ms
digitalWrite(LED3,LOW); //Turn off LED3
delay(500); //Delays for 500ms
}
your first loop 100 times LED1 switches the LED ON/OFF 200 / 200 milliceonds.
this means periodtime 0,4 seconds = frequency 1 / period = 2,5 Hz
doing this 100 times need 0,4 * 100 = 40 seconds
LED2/3 switch on/off 500/500 millicesonds
This means peiodtime 1 second frequency 1 / periodtime = 1 Hz
doing this 60 times needs 60 seconds.
As ou have programmed it now
LED1 blink 100 times at 2,5Hz
repeat 60 times
LED2 switch on/off 500/500
after that
LED3 switch on/off 500/500
So it is still unclear what you really want to have
shall blinking LED1 100 times and blinking LED2/3 60 times finish at the same time?
This requires to change the frequency of LED1 or LED2/3
does complimentary mean if LED1 is ON LED2/3 shall be off?
does complimentary mean
LED2 ON / LED3 OFF // and then vice versa
LED2 OFF / LED3 ON
You are welcome on this forum! You are working on an informatic project and what is most needed in an informatic project is information imagine: do the other users here in the forum have a clear picture of what you are trying to do?
To speed up finishing your project you should invest some time into writing additional information I'm 100% sure that this WILL speed up finishing your project.
If you don't post all this information because you want a "quick answer" to your detail problem It is very likely to turn out that all that happens is having mutliple waiting-times with mutliple asking back for details until the other users do have a clear picture of what you want to do.
welcome to the Arduino-forum.
There are a few things to learn about how to use this forum
You should post code as a code-section. Read the link at the bottom to learn two ways how this can be done very quickly.
here is a commented code that I think is easier to understand.
const byte LED1 = 13;
const byte LED2 = 12;
const byte LED3 = 8;
// easy to use helper-function for NON-blocking timing
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - periodStartTime >= TimePeriod )
{
periodStartTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
unsigned long MyLED1Timer = 0; // variables MUST be of type unsigned long
unsigned long MyLED23Timer = 0; // variables MUST be of type unsigned long
int myTickCounter = 0;
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
}
void loop() {
// if 200 milliseconds have passed by
if ( TimePeriodIsOver(MyLED1Timer, 200) ) {
if (digitalRead(LED1) == HIGH) { // if LED is on
digitalWrite(LED1, LOW); // switch LED off
}
else { // if LED is off
digitalWrite(LED1, HIGH); // switch LED on
}
}
// 60 times on/off in 40 seconds intervall 40/60 = 0,333 seconds
if ( TimePeriodIsOver(MyLED23Timer, 333) ) {
if (digitalRead(LED2) == HIGH) { // if LED is on
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
}
else {
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
}
}
}
I will continue to post bwd in this version to make it the new defacto standard.
It is much easier to use than the original blink with_struggling_to_adapt_to_my_usecase-example that is delivered with the IDE
Why is periodStartTime = currentMillis; preferable to periodStartTime += TimePeriod; ?
I prefer the second form because it corrects for any jitter caused by a delayed invocation and remains phase-locked with millis(). The += form also only needs one copy of the millis() value, so you don't need the temporary storage or multiple millis() calls. I can see that if there was jitter in excess of TimePeriod the PeriodStartTime += TimePeriod would execute multiple times in quick succession, rather than only once. But is there something else I'm missing?
I like the bool TimePeriodIsOver(...) function in that you could use both its normal ladder-logic on-timer sense and the ladder-logic off-timer inverse:
if ( digitalRead(sensor) &&
!TimePeriodIsOver(MyLED1Timer, 200)){
falseStart = true;
}
If you want to use the function TimePeriodIsOver for timing a second thing you simply use a second / third timervariable.
if ( TimePeriodIsOver(MyBlinkTimer,1000) ) {
// do thing A once every 1000 milliseconds
}
if ( TimePeriodIsOver(MySecondTimer,250) ) {
// do thing B once every 250 milliseconds
}
if ( TimePeriodIsOver(MyThirdTimer,5000) ) {
// do thing C once every 5000 milliseconds
}
The "&"-sign returns the currentMillis-value to the "calling" Timer-variable "handed over" at the call of the function. This means the time-variables
I am in the “it depends what you want” camp. I will admit to having been surprised at a flurry of activity at loop rate, but upon analysis I saw what was happening and made a note to choose based on the circumstances.
If the missed events, so to speak, were silver dollars dropping into your hat, you pretty much would want to have them all performed…
But the real key is not overrunning the time slice, which makes the accounting choice moot; either, if coded correctly, should make the timed event happen, um, on time.
I think there’s a library that makes easy keeping track of you loop and whether you are getting around it in time (using less than 100 % of the availed slice).
I have done OK using a frequency meter with duty cycle and just watching a wiggled pin to gauge if I am succeeding.
Usually the stuff I do leaves lotsa time left over. I enjoy staying well within all budgets.
you said, that one LED should blink 100 times, with an interval of 200ms.
This means for me the blinking should end in 100 x 200 (x 2) = 40 seconds
whereof the other LED should blink 60 x 500 (x 2) = 60 seconds.
So the two other LEDs will continue blinking longer, is that correct?
What should happen after the 60 seconds? Will the LEDs never start blinking again?
Here is my proposal based on a LED library I have written:
/*
Blink three LEDs in different intervals for different time
https://forum.arduino.cc/t/trying-to-blink-led1-for-100-times-at-the-same-time-blink-led2-and-led3-complimentarily-for-60-times/963235
by noiasca
2022-02-27
*/
#include <Noiasca_led.h> // download library from http://werner.rothschopf.net/microcontroller/202202_tools_led_en.htm
#include <Noiasca_timer.h> // the library also contains a simple timer
BlinkPin ledA {13}; // this LED effect will be switched on and off with the timer
BlinkPin ledB {12};
BlinkPin ledC {8};
// here you create your timer object
// you need two parameters:
// the first parameter is the interval
// the optional second parameter is a limit how often you want that trigger
// interval, limit
LittleTimer timerA {100 * 200UL * 2, 1}; // fire once in x ms
LittleTimer timerB {60 * 500UL * 2, 1}; // fire once in y ms
void setup() {
Serial.begin(115200);
Serial.println(F("\nStart"));
ledA.begin(); // start a LED object
ledB.begin();
ledC.begin();
ledA.setOnInterval(200); // how long is the LED on
ledA.setOffInterval(200); // how long is the LED off
ledB.setOnInterval(500);
ledB.setOffInterval(500);
ledC.setOnInterval(500);
ledC.setOffInterval(500);
timerA.start(); // start the timer
timerB.start();
}
void loop() {
ledA.update(); // you must call the update() method for each object:
ledB.update();
ledC.update();
if (timerA.hasEnded()) // fires one time after the timer has ended
{
Serial.println(F("timerA has ended"));
ledA.off();
}
if (timerB.hasEnded()) // fires one time after the timer has ended
{
Serial.println(F("timerB has ended"));
ledB.off();
ledC.off();
}
}