Help blink leds with Millis() function

const int led1 = 10;
const int led2 = 11;
const int led3 = 12;
const int led4 = 13; // the pin numbers for the LEDs

int Reset = 4;
int Received = 0;

const int led_Interval = 2300; // number of millisecs Motor is ON
const int blinkDuration = 2500; // number of millisecs that Motor is OFF
const int led_Interval2 = 2100;
const int blinkDuration2 = 2700;
const int led_Interval3 = 1800;
const int blinkDuration3 = 3000;
//------- VARIABLES (will change)

byte led1_State = LOW; // used to record whether the LEDs are on or off
byte led2_State = HIGH; // LOW = off
byte led3_State = HIGH; //
byte led4_State = HIGH;
//------- Time
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousled1Millis = 0; // will store last time the LED was updated
unsigned long previousled2Millis = 0;
unsigned long previousled3Millis = 10;
void setup() {
Serial.begin(9600);

//Serial.println("Starting SeveralThingsAtTheSameTimeRev1.ino"); // so we know what sketch is running

//------- set the Led pins as output:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led4, LOW);
digitalWrite(Reset, HIGH);
delay(200);
pinMode(Reset, OUTPUT);
Serial.println("wait");
}

void loop() {
if (Serial.available()>0){
Received = Serial.read();
}
// Notice that none of the action happens in loop() apart from reading millis()
// it just calls the functions that have the action code
currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
// use the same time for all LED flashes to keep them synchronized
// call the functions that do the work
updateled1_State();
updateled2_State();
switchLeds();
}
//========
void updateled1_State() {
if (led1_State == LOW) {
if (currentMillis - previousled1Millis >= blinkDuration) {
led1_State = HIGH;
previousled1Millis += blinkDuration;
}
}
else {
if (currentMillis - previousled3Millis >= led_Interval) {
led1_State = LOW;
previousled3Millis += led_Interval;
}
}
if (Received == 'a' || Received == 'b'){
led1_State = HIGH;
led2_State = HIGH;
led4_State = HIGH;
}
}
//=========
void updateled2_State(){
if (led2_State == HIGH) {
if (currentMillis - previousled3Millis >= led_Interval) {
led2_State = LOW;
previousled3Millis += led_Interval;
}
}
else {
if (currentMillis - previousled2Millis >= blinkDuration) {
led2_State = HIGH;
previousled2Millis += blinkDuration;
}
}
if (Received == 'a' || Received == 'b'){
led1_State = HIGH;
led2_State = HIGH;
led4_State = HIGH;
}
}

//=======
void switchLeds() {
if (Received == '1' || Received == 'c'){
// this is the code that actually switches the LEDs on and off
digitalWrite(led1, led1_State);
digitalWrite(led2, led2_State);
}
else{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led4, HIGH);
}

//-------- Program Reset both app and arduino program
if (Received == '9'){
Serial.println("wait");
digitalWrite(Reset, LOW);
digitalWrite(Reset, HIGH);
delay(200);

}
}

Code needs to go in the timer “if” statement.

Your best bet to understand this would not to be to try to integrate into another (more complex) sketch yet.

Start with the steps I outlined from Blink Without Delay.

Once you understand how to modify that sketch to produce the blink patterns you want, then you are ready to move on to modifying more complex sketches.

unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds
const byte ledPin = 13; //using the built in LED

void setup()
{
Serial.begin(115200); //start Serial in case we need to print debugging info
pinMode(ledPin, OUTPUT);
startMillis = millis(); //initial start time
}

void loop()
{
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
digitalWrite(ledPin, !digitalRead(ledPin)); //if so, change the state of the LED. Uses a neat trick to change the state
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
}
}

74younus:
For your simplicity I posted only necessary working portion(loop section) of it . Do check if code or syntax is correct.

Does that sketch blink the LED every 2 seconds ?

I found this code posted by someone . This code blinks led . I tried to blink two LEDs with this code but I failed to do so . I am unable to understand this.

I found this code posted by someone .

EDIT
It is the Blink example from the IDE
Actually it is the BlinkWithoutDelay example from the IDE

Does it turn the LED on for 2 seconds and off for 2 seconds ?

I am unable to understand this

Do you mean that you don't understand this sketch ? Which parts are you stuck on ?

No every second as period is set to 1000 milliseconds.
I tried to blink two LEDs with this code but I failed...
I also tried to change the Off time of the built in led but failed.
I am using this forum on mobile so I have some problems while I write reply.

I does make it On for 1sec and Off for one sec . I want to elongate its off time and also I want to use another led that will also blink but out of period with first led. OK

Sorry for my English .I use mobile for all these replies.

It works fine only blinking of LEDs is causing me trouble when on goes off another goes on which I hate the most. I want to keep other off for some time whoever first one goes off.
Thank you for your reply.

74younus:
I does make it On for 1sec and Off for one sec . I want to elongate its off time and also I want to use another led that will also blink but out of period with first led. OK

Sorry for my English .I use mobile for all these replies.

"out of period with first led"--> [When 1 is OFF 2 is ON] -AND- [when 1 is ON 2 is OFF] ?

-jim lee

It is the Blink example from the IDE

Sorry for any confusion.
It is actually the BlinkWithoutDeley function from the IDE

I want to elongate its off time

Start with this

const byte ledPin =  LED_BUILTIN;

byte ledState = LOW;
unsigned long previousMillis = 0; 
unsigned long interval = 1000; 

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    if (ledState == LOW)
    {
      ledState = HIGH;
      interval = 500;
    }
    else
    {
      ledState = LOW;
      interval = 3000;
    }
    digitalWrite(ledPin, ledState);
  }
}

Now add a completely separate set of variables for a second LED, a second test for the end of period and turn the second LED on/off as required

Yes and when one goes off I want a pause and then other is on

I have some issue with your suggested code. I tried but libraries idlers or TLSlib Tools not responding and I am not able to use them i even I visited you GITHUB . I also tried after installing blinker library.

Well done...

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000;  //the value is a number of milliseconds
const byte ledPin = 13;    //using the built in LED

void setup()
{
 Serial.begin(115200);  //start Serial in case we need to print debugging info
 pinMode(ledPin, OUTPUT);
 startMillis = millis();  //initial start time
}

void loop()
{
 currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
 if (currentMillis - startMillis >= period)  //test whether the period has elapsed
 {
   digitalWrite(ledPin, !digitalRead(ledPin));  //if so, change the state of the LED.  Uses a neat trick to change the state
   startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
 }
}

Step by step....

  1. Can you see what part of this code makes the LED stay ON/OFF for 1 second?
    Hint: 1 second = 1000 milliseconds.
    Change this so the LED stats ON and OFF for 2 seconds each time.
    Test it.

  2. Can you see the statement that turns the LED ON and OFF?
    Hint: It involves the digitalWrite() function.
    Study the digitalWrite function documentation. You will need it later.
    Remove this line of code. Replace it with the code you originally wrote in post #17.
    Somehow that code has disappeared, but you can find a commented copy of it in my reply #18.

  3. Actually make your code turn the LEDs ON and OFF.
    Hint: It involves the digitalWrite() function you studied earlier and the “if” statements you already wrote.
    Test it.
    Probably it doesn’t work yet because you have not fixed the other issues with that code as you wrote it...

  4. Remove the count = 0 statement.
    Instead, where my comment is about resetting the counter to 0 when it reaches 4, make that happen correctly.
    Hint: It involves another “if” statement to check for count == 4, and then the count = 0 statement you misplaced previously.
    Test it.

Now does the code do what you want?
If not please post back the code you ended up with and we will help you correct it.

When Jim goes down I am already down waiting him.
When he reaches to me , we stay down for some time.
Now I go up and come back down to Jim and wait sometime with Jim . Now Jim again goes up and come backed to me and wait some time

This is what I want from this forum to solve with Millie() and two LEDs thank you.

It is actually the BlinkWithoutDeley function from the IDE
Start with this

const byte ledPin =  LED_BUILTIN;

byte ledState = LOW;
unsigned long previousM

void loop()
{
  unsigned long currentMillis = millis();
 

Yes your code is very good and it worked for one led and I will declare new variables for led2 . But should I also declare a new currentMillis for led 2??


Now add a completely separate set of variables for a second LED, a second test for the end of period and turn the second LED on/off as required

Yes your code worked

I will declare all new variables for led 2
but should I also declare a new " current Milli()" for led 2 or not

void loop()
{
unsigned long current Milli= milli()

Now add a completely separate set of variables for a second LED, a second test for the end of period and turn the second LED on/off as required

@pcbbc

Not sure what was changed in the post.
Report closed.

I want this function in milli()

//blink two LEDs
digital write(led 1, HIGH);
Delay(2000);

Digital write (led 1, LOW);

Delay(2000);

Digital write (led 2, HIGH);

Delay(2000);

Digital write (led 2, LOW);

Delay (2000);
Please suggest your codes that do this in milli(). :o

should I also declare a new " current Milli()" for led 2 or not

You only need one currentMillis variable. The best place to give it a value from millis() is at the start of loop(). You could have 2 variables but it is not necessary and could be confusing