Please help newbie struggling for 5days! about using millis

I've been trying many solutions and I can't get over it. Please help. I'd really appreciated.

I'd like to turn on and off the LED on the specific time like at the 2 second has passed turn on the LED1 then stay until at 56 second, turn it off, etc.

I don't wanna use delay cause it only do 1 work at a time.

......
There are 3 LEDs in this case.

LED1 : turn on at time 00.02s ( 2 second hass passed) then turn OFF at 00.56s (stay ON until 56 second has passed then turn it OFF ).

LED2 : turn on at the beginning at 0 second then turn it OFF at the very end of the process which is at 56 second later ( means stay ON for 56 second)

LED3 :

  • turn on at 00.20s to 00.22s then turn if OFF at 00.39s (means stay ON for 2 second)
  • turn on at 00.37s to 00.39s then turn if OFF at 00.39s (means stay ON for 2 second)
  • turn on at 00.54s to 00.56s then turn if OFF at 00.56s (means stay ON for 2 second)

The best thing i can do is only 2 led the third LED I don't how it'll work because it take 2 different timeframe the first one wait aroud 18 second to turn LED3 ON, the second event need only 10 second to turn LED3 ON.

I can't figure it out pleaseeeee, i'm begging you guys. Thank you

// These variables store the flash pattern
// and the current state of the LED
     
int led1 =  1;      // the number of the LED pin
int ledState1 = LOW;             // ledState used to set the LED
unsigned long previoustime1 = 0;        // will store last time LED was updated
long OnTime1 = 56000;           // milliseconds of on-time
long OffTime1 = 2000;          // milliseconds of off-time
     
int m2 =  2;      // the number of the LED pin
int ledState2 = LOW;             // ledState used to set the LED
unsigned long previoustime2 = 0;        // will store last time LED was updated
long OnTime2 = 2000;           // milliseconds of on-time
long OffTime2 = 0;          // milliseconds of off-time



void setup() 
{
  // set the digital pin as output:
  pinMode(led1, OUTPUT);      
  pinMode(led2, OUTPUT);
  pinMode(led3, 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 - previoustime1 >= OnTime1))
  {
    ledState1 = LOW;  // Turn it off
    previoustime1 = currentMillis;  // Remember the time
    digitalWrite(led1, ledState1);  // Update the actual LED
  }
  else if ((ledState1 == LOW) && (currentMillis - previoustime1 >= OffTime1))
  {
    ledState1 = HIGH;  // turn it on
    previoustime1 = currentMillis;   // Remember the time
    digitalWrite(led1, ledState1);	  // Update the actual LED
  }
      
  if((ledState2 == HIGH) && (currentMillis - previoustime2 >= OnTime2))
  {
    ledState2 = LOW;  // Turn it off
    previoustime2 = currentMillis;  // Remember the time
    digitalWrite(led2, ledState2);  // Update the actual LED
  }
  else if ((ledState2 == LOW) && (currentMillis - previoustime2 >= OffTime2))
  {
    ledState2 = HIGH;  // turn it on
    previoustime2 = currentMillis;   // Remember the time
    digitalWrite(led2, ledState2);	  // Update the actual LED
  }
  
  
}

Use the blink without delay example and create 3 instances

1 Like

@bing23

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.

It will help you get the best out of the forum in the future.

1 Like

Please post your best effort at doing this so people can see what you've achieved and what you are struggling with. Don't forget the code tags.

And welcome!

1 Like

This is not very clear. Do these cycles repeat every minute? Does , for example, 'turn on at 00.20s' mean that it turns on at 20 seconds into each new minute?

2 seconds has passed - but since when? 2 seconds since program started? 2 seconds past midnight?

I.e. when is "time 0"?

Steve

first, good recognizing delay() is a garbage way to code.

couple general suggestions:

everything should be unsigned in your case, you have literally no need for negative values. and the rollovers may cause you problems, though they are will happen eventually anyways.

and since you are new, I suggest the "uint8_t" system, thats an 8bit unsigned int. an unsigned long would be "uint32_t". this system works much better cross platforms. You dont need to add any libraries, or do anythiing special in arduino.

you may consider making an array of nextOn & nextOff time, tiddy it up a bit.

why is offtime2 set to 0? the end of your last else if will always be true.

what kind of board are you running?

1 Like

when he calls millis() at the start of the loop.

1 Like

another general suggestions, use #define for information that doesn't need to change. (note the absence of a ";" on define lines)

#define led1 1
#define onTime1 5600

if you do that, you may want to force it into a long:

#define onTime1 5600L

and simulate it in serial. print the times to the screen, so you can see how the logic is flowing.

1 Like

Even better, declare variables that will not change as const so that they have a data type

1 Like

I recon "How to use millis()" might be worth a Guiness Book of Records entry ... :wink:

1 Like

I appologize for unclear, do only once, no repeat.

for example, it's like a clock but it doesn't relate to real time. Time start when arduino start at the same time. In 1 minutes (60seconds) when the time passed by 2 seconds from the beginning led the LED1 ON, after that the clock is still running 3 seconds 4 seconds, 5seconds,....,etc.

then the clock reaches 20 seconds which realate to arduino clock, LED3 is ON for only 2 seconds then the if OFF, the clock is still runing 22 seconds, 23 seconds,......

until it reaches 37 seconds, let LED3 ON again for 2 seconds and then turn it OFF .

Node mcu V3 and Arduino Uno both

is:
#define onTime1 5600L

functionally any different then:

const uint16_t 5600;

?

i know there times where I had to use const but, i've always done much more with #define. but I'm no code master.

1 Like

could you draw us a crude graph of ledState vs time graph...something is getting lost in the words

1 Like

here

https://ibb.co/qC5Jxy0

Maybe it would be clearer if you draw a time line that shows the state of each led.
On the Y axis show seconds starting a 0 and progressing to say 60.
On the X axis show the ON and OFF states for each of the 3 leds.
Do it by hand and post a picture of it.

** edit **
Ok. In the meantime it has been clarified.

1 Like

are you simply trying to turn LEDs on/off for different periods? or at specific times?

consider


enum { Off = HIGH, On = LOW };

struct Timer {
    byte    pin;
    const unsigned long   MsecOn;
    const unsigned long   MsecOff;
    unsigned long   msecPeriod;
    unsigned long   msecLst;
};

Timer timers [] = {
    { 10,  1000, 1000 },
    { 11,  3000, 2000 },
    { 12,  2000, 3000 },
};
#define N_TIMER  (sizeof(timers)/sizeof(Timer))

// -----------------------------------------------------------------------------
void
loop ()
{
    unsigned long msec = millis ();

    Timer *t = timers;
    for (unsigned n = 0; n < N_TIMER; n++, t++)  {
        if ((msec - t->msecLst) > t->msecPeriod)  {
            t->msecLst = msec;
            if (On == digitalRead (t->pin))  {
                digitalWrite (t->pin, Off);
                t->msecPeriod = t->MsecOff;
            }
            else  {
                digitalWrite (t->pin, On);
                t->msecPeriod = t->MsecOn;
            }
        }
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    Timer *t = timers;
    for (unsigned n = 0; n < N_TIMER; n++, t++)  {
        pinMode (t->pin, OUTPUT);
        digitalWrite (t->pin, Off);
        t->msecPeriod = t->MsecOff;
    }
}
1 Like

No functional difference, but by declaring a variable it automatically has a data type, whereas using a #define it doesn't, which is all too easy to forget and have it default to being an int

2 Likes

oh man have I been there! why do ya think I mentioned it? :wink:

1 Like