help with millis

Hey guys so i decided to lookup millis and use it since delay is causing some problems in my current project. Below is my code,what i am trying to accomplish is put the led on and after 3 seconds take it off, seems to always stay on though. I'm not sure why, to me the code seems correct,any help would be deeply appreciated.

unsigned long time;
unsigned long time2;
int led = 51;
void setup(){
  
  pinMode(led,OUTPUT);
  time = millis();
  time2=time;
}
void loop(){
  time = millis();
  digitalWrite(led,HIGH);
  if(time == (time2 + 3000))
  {
    digitalWrite(led,LOW);
    time2=time;
  }
  
  }

also used

if(time >= (time2 + 3000))

You should only write the pin HIGH every other time the if statement gets called, and LOW the other half. Right now, you are writing it HIGH every time and then once every two seconds writing it LOW for as long as it takes the arduino to loop back around -- microseconds.

And yes, you should use >= and not ==

I'm not quite understanding what you mean, how is it switching every 2 seconds, from my understanding, in the loop, i set the led high, then i count for 3 seconds using the timer and set it low. wouldn't it go through the same loop, setting it high for 3 seconds then low after?

How long did you tell it to stay LOW before being allowed to go HIGH again? ;)

Time to get into the habit of subtracting rather than adding, which handles the rollover when millis wraps.

That is:

if ((timeNow - startTime) >= desiredInterval)
  {
  // do something
  }

And definitely do not compare "equal" because if you miss a tick, the comparison will never be true (or at least not for a couple of months).

navinl16: I'm not quite understanding what you mean, how is it switching every 2 seconds, from my understanding, in the loop, i set the led high, then i count for 3 seconds using the timer and set it low. wouldn't it go through the same loop, setting it high for 3 seconds then low after?

What WizenedEE was saying you are only turning it off for one loop because of that "digitalWrite(led,HIGH);" statement. If you are trying to toggle the LED, get rid off that statement and inside your if statement, toggle the LED, you probably need a state variable to toggle.

navinl16: Hey guys so i decided to lookup millis and use it since delay is causing some problems in my current project. Below is my code,what i am trying to accomplish is put the led on and after 3 seconds take it off, seems to always stay on though. I'm not sure why, to me the code seems correct,any help would be deeply appreciated.

It's not clear from your post how long you want the LED to be off for. The following will turn your LED on for 3 seconds and off for 3 seconds continuously.

unsigned long time;
unsigned long time2;
int led = 51;
int state = 1;

void setup(){
  
  pinMode(led,OUTPUT);
  time = millis();
  time2=time;
  digitalWrite(led,state); //turn led on
}

void loop(){
  
  if(time - time2 >= 3000) //wait 3 seconds
  {
    state = !state; // 1 becomes 0, 0 becomes 1
    digitalWrite(led,state); //turn led to other state
    time2=time;
  }
  
}

Your loop() is getting called over and over again, very quickly.

So as soon as you turn it off, loop() begins again, and turns it on before you even notice it is off.

Your code for turning it off is ok, in theory, it is the turning on that is not doing what you apparently want.