'Millis' timing help

I am using millis to tell leds to come on and off at specifically the right time (the leds are synced with music). I think I might be miss using millis but i got it to do what i want.

I am using codes like:

if(currentMillis - prevm == (interval + 9500)){
      digitalWrite(ledYR,HIGH);

so that the led will turn on after 9.5 seconds.
This all works but now i want to fade leds, use arrays, and use for loops at the right time, so i need the if statement to be true for longer than just that one second.

How could i go about this?
is there a way to set a range of time for the if statement to be true.

Like:

 if(currentMillis - prevm == (interval + 7000 to 9500)){
digitalWrite(ledYR,HIGH);

or maybe something that says its true if greater than 8000 but less than 9500

Here is my whole code:

const int ledBR = 13;
const int ledGR = 12;
const int ledRR = 11;
const int ledYR = 10;
const int ledBL = 9;
const int ledGL = 8;
const int ledRL = 7;
const int ledYL = 6;
const int ledW = 5;
const int ledUV = 4;

long prevm = 0;
long interval = 0;

void setup(){

pinMode(ledBR, OUTPUT);
pinMode(ledGR, OUTPUT);
pinMode(ledRR, OUTPUT);
pinMode(ledYR, OUTPUT);
pinMode(ledBL, OUTPUT);
pinMode(ledGL, OUTPUT);
pinMode(ledRL, OUTPUT);
pinMode(ledYL, OUTPUT);
pinMode(ledW, OUTPUT);
pinMode(ledUV, OUTPUT);

analogWrite(ledW, 50);
}
void loop(){
    unsigned long currentMillis = millis();

  if(currentMillis - prevm == (interval + 1000)){
    digitalWrite(ledRL, HIGH);}
   if(currentMillis - prevm == (interval + 1500)){
    digitalWrite(ledRL, LOW);}
   if(currentMillis - prevm == (interval + 2000)){
    digitalWrite(ledRL, HIGH);}
   if(currentMillis - prevm == (interval + 2500)){
    digitalWrite(ledRL, LOW);}

  if(currentMillis - prevm == (interval + 4500)){
    digitalWrite(ledW, LOW);}
    
    if(currentMillis - prevm == (interval + 9500)){
      digitalWrite(ledYR,HIGH);
      digitalWrite(ledYL,HIGH);}
      
      if(currentMillis - prevm == (interval + 21500)){
        digitalWrite(ledBR, HIGH);
        digitalWrite(ledGR, HIGH);
        digitalWrite(ledRR, HIGH);
        digitalWrite(ledBL, HIGH);
        digitalWrite(ledGL, HIGH);
        digitalWrite(ledRL, HIGH);}
        
        if(currentMillis - prevm == (interval + 24000)){
          digitalWrite(ledUV, HIGH);}
          
          if(currentMillis - prevm == (interval + 27000)){
            digitalWrite(ledW,HIGH);}
            
            if(currentMillis - prevm == (interval + 30000)){
        digitalWrite(ledBR, LOW);
        digitalWrite(ledGR, LOW);
        digitalWrite(ledRR, LOW);
        digitalWrite(ledBL, LOW);
        digitalWrite(ledGL, LOW);
        digitalWrite(ledRL, LOW);
        digitalWrite(ledUV, LOW);
        digitalWrite(ledW, LOW);
        digitalWrite(ledYL, LOW);
        digitalWrite(ledYR, LOW);}
}

Thank you so much if you can help!! :slight_smile:

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

If I had to use millis() I'd store the currentMillis variable like so
unsigned int currentMillis = millis() / 1000;

This will give you a whole second rather than a fraction of a second, the timer would say 1...2...3...4... rather than showing milliseconds.

Alternatively, you could write a nearly function that returns true if the given time is within a second or two of the current time.

I am surprised that your code does what you want.

  if(currentMillis - prevm == (interval + 1000))

prevm is initially set to zero and does not change
interval is initially set to zero and does not change
There is a chance that you will miss millis() being exactly equal to 1000
It probably won't matter in your code but it is better to use unsigned longs for timing variables as then you don't run into negative values

If you want an ouput to come on after a defined time then
if (millis() - startTime >= period)is the way to do it.
Once the output is on you can set startTime to millis() again and set a different period if required . The next time the period ends you can turn off the output and so on. Keep the state of the output in a boolean variable, switch its value at the end of the period and take the appropriate actions for its state.