Implementation of mills() for delay()

Hello!

I know this has already been discussed above with regard to beginners guide to mills(), but to be honest, I went through it and I could not find the answer.

I am using mills() instead of delay() for fading my LED, which is ok, it works in the first if(), however, when I try to prolong my "fading" , like someone can easily do with the delay(), somehow, the millis() is not working. Probably I have to code it differently...but I do not know where to tackle this problem. Please open my eyes and let me know what I am doing wrong here?

  // +++++++ FADING FUNCTION +++++++

  if (OS > HT && currentTime - startTime > 1000) {
    if (fadeUp) {
      if (brightness <= 254) {
        brightness++;
        analogWrite(ledPin, pgm_read_byte(&table[brightness]));
        startTime += currentTime;
        
        if ( currentTime1 - startTime >= 100 ) {
          //delay(5);
          Serial.println(" over with ++");
          startTime += currentTime1;
        }
      }
    }
    else if (!fadeUp) {
      if (brightness >= 1) {
        brightness--;
        analogWrite(ledPin, pgm_read_byte(&table[brightness]));
        startTime += currentTime;
        
        if ( currentTime1 - startTime >= 100 ) {
          //delay(5);
          Serial.println("over with --");
          startTime += currentTime1;
        }
      }
    }

Best.

somehow, the millis() is not working

Please, disavow yourself of that notion at the earliest possible opportunity.

If you want help, post your code.

Somehow I have deleted the previous post, internet was down, etc. Anyway, below is a working version, I am happy with it, but is there a way to write this better?

if (OS > HT && currentTime - startTime > 1000) {
if (fadeUp) {
if (brightness <= 254) {
currentTime1 = millis();
if (currentTime1 - startTime1 > 5) {
brightness++;
startTime1 = currentTime1;
analogWrite(ledPin, pgm_read_byte(&table[brightness]));
}
}
}
else if (!fadeUp) {
if (brightness >= 1) {
currentTime1 = millis();
if ( currentTime1 - startTime1 > 5) {
brightness--;
startTime1 = currentTime1;
analogWrite(ledPin, pgm_read_byte(&table[brightness]));
}
}
}

when I try to prolong my "fading" ... the millis() is not working.

The usual cause for this sort of problem is not using "long" for the "starttime" (or other variables that hold saved copies of millis())
(can't tell, in your case, since that part of the code wasn't included. Hint, hint.)

The one thing that is easier with the delay() is the fact that there is a function available, where you just put in a number eg. delay(x)... anyway, the above code is ok, at least for now, I have removed the currentTime1, because I can only use one variable, namely currentTime, which is the same :slight_smile:

Now I am wondering, if there is a way to reduce the number of variables even further?

Btw how come that no body has not yet already wrote a "NEW" delay with mills() or micros() as a function or am I just not looking good enough ?

Best.

Why are you checking time within another time-checking if block?

What are OS and HT?

What is the difference between currentTime and currentTime1? Why get the time twice? What is the difference between startTime and startTime1?

Anyway, below is a working version,

If it's working, it is only by accident. The snippet you've posted is a nonsensical mess.