I do not have much experience with the ARDUINO but I have been reading posts for days to understand how to write the following code:
Led 1 is on for 0.3 s and off for 0.7 s (1 second cycle).
0.1 s after the LED1 is on, LED 2 will turn on and stays on for 0.3 s and then turns of for 0.1 s.
From my understanding using 'Delay" doesn't work because there are two LEDs working together with some time delay between them. Using "millis()" would help but I dont know how to apply the 0.1 s delay.
1988:
From my understanding using 'Delay" doesn't work because there are two LEDs working together
I think this is not correct. Since there is a sequence to be followed, it is possible to use only the Delay function, as long as it is not necessary to use the microcontroller for other things.
Led 1 is on for 0.3 s and off for 0.7 s (1 second cycle).
LED1 ON
DELAY 300
LED1 OFF
DELAY 700
0.1 s after the LED1 is on, LED 2 will turn on and stays on for 0.3 s and then turns of for 0.1 s.
LED1 ON
DELAY 100ms // 0.1 s after the LED1 is on, LED 2 will turn on
LED2 ON
DELAY 200ms // 300 - 100 = 200
LED1 = 0 // OFF
DELAY 100ms // 200 + 100 = 300 // LED 2 will turn on and stays on for 0.3 s
LED2 OFF
DELAY 100ms // LED 2 will turn on and ... then turns of for 0.1 s
LED2 ON
DELAY 500ms // 700 - 100 - 100 = 500
I just didn't understand how the state of LED2 should be in the next cycle, when LED1 restarts the cycle, because LED2 has been activated and has been activated ever since.
Thank you for your answer,
"LED1 ON
DELAY 100ms // 0.1 s after the LED1 is on, LED 2 will turn on"
This keeps the LED1 on for 0.1s but I want it to be on for 0.3 second and LED2 turns on 0.1s after the first one is on. Led 2 will be on for 0.3 s and off for 0.7s. So they both OK for 0.3s and off for 0.7 s with 0.1s delay between the two o turn on.
Thank you
The cycle is reapiting, so I want this to happen over and over. Do you know how I can make that happen?
instead of using delay() you can use millis() to capture a timestamp and compare it to a previous timestamp to determine when to do something without interfering other activities.
In the following code, is there any way I can apply a time delay between two LEDs?
// These variables store the flash pattern
// and the current state of the LED
int ledPin1 = 12; // the number of the LED pin
int ledState1 = LOW; // ledState used to set the LED
unsigned long previousMillis1 = 0; // will store last time LED was updated
long OnTime1 = 250; // milliseconds of on-time
long OffTime1 = 750; // milliseconds of off-time
int ledPin2 = 13; // the number of the LED pin
int ledState2 = LOW; // ledState used to set the LED
unsigned long previousMillis2 = 0; // will store last time LED was updated
long OnTime2 = 330; // milliseconds of on-time
long OffTime2 = 400; // milliseconds of off-time
void setup()
{
// set the digital pin as output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
{
ledState1 = LOW; // Turn it off
previousMillis1 = currentMillis; // Remember the time
digitalWrite(ledPin1, ledState1); // Update the actual LED
}
else if ((ledState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
{
ledState1 = HIGH; // turn it on
previousMillis1 = currentMillis; // Remember the time
digitalWrite(ledPin1, ledState1); // Update the actual LED
}
if((ledState2 == HIGH) && (currentMillis - previousMillis2 >= OnTime2))
{
ledState2 = LOW; // Turn it off
previousMillis2 = currentMillis; // Remember the time
digitalWrite(ledPin2, ledState2); // Update the actual LED
}
else if ((ledState2 == LOW) && (currentMillis - previousMillis2 >= OffTime2))
{
ledState2 = HIGH; // turn it on
previousMillis2 = currentMillis; // Remember the time
digitalWrite(ledPin2, ledState2); // Update the actual LED
}
}
It appears that you want LED changes to be possible on .1s boundaries
and that these changes should be repeatable every second.
Construct code using millis( ) that 1) detects an elapsed time of .1s
from the previous detection, 2) counts each detection 1 thru 10 (Tenth),
and 3) sets a flag (Frame) indicating this detection.
Your LED code (a section for each LED) looks for Frame to be true. If not,
it does nothing. If so, it tests Tenth to see what action to take. At
the end of all the LED code, remember to clear Frame for next time.
Now you can set any LED to go on or go off by looking at Tenth. The LED
code will have a variable 1 - 10 for ON, and another variable 1- 10 for OFF.
No delays anywhere and millis( ) is only in one place.
I would start with writing the timing loop and just toggling one LED on one of the Tenth values (say 10).
Look for millis tutorials, and how to drive an LED.
Once we see your attempt at that, we can go from there.