Problem millis() 3 LEDs +dist sensor

Hi!
I’ve got a problem working with millis() instead of “delay”
:
Arduino UNO
Distance sensor hc-sr04
Protoboard 3 leds(red, yellow, green)

According to a range of distances an LED turns on, the problem is when I do with millis (), instead of turn off it sometimes stays on, (I think it depends on the state in which it is when readying distance) and sometimes my red led blinks every second
There is my code, I would be very glad if anyone can help me

Thank you!

This is the code without Delay

#define ECHO 11        
#define TRIGGER 12     
 
unsigned int time;
int distance=0;
int short=30; // Interruption period in milliseconds 
int medium=80;
int large=120; 
long before=0;//time before var
int red=7;
int pinR=LOW;
int yellow=6;
int pinY=LOW;
int green=5;
int pinG=LOW;
 
void setup() {
  
  pinMode(ECHO, INPUT);
  pinMode(TRIGGER, OUTPUT);
  pinMode(zumb,OUTPUT);
  pinMode(5,OUTPUT); // Led green 
  pinMode(6,OUTPUT); // led yellow
  pinMode(7,OUTPUT); // led red

} 
void loop(){
  digitalWrite(TRIGGER, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER, LOW);
  // Calculate the distance by timing the high state of pin ECHO
  time= pulseIn(ECHO, HIGH);
  //speed of sound 340m/s or 29 microsecond per cm
  distance= time/58
  unsigned long now=millis();//actual time
  if(distance<=10){
     if (now-before>short){
     before=now;
     if(pinR==LOW)
     pinR=HIGH;
     else
     pinR=LOW;
     digitalWrite(red,pinR);
     }
  }
  else if( distance>=10&&distance<=30){
   if (now-before>medium){
     before=now;
     if(pinA==LOW)
     pinA=HIGH;
     else
     pinA=LOW;
     digitalWrite(yellow,pinY);
     }
  }
  else if(distance>=30&&distance<=45){
   if (now-before>large){
     before=now;
     if(pinG==LOW)
     pinG=HIGH;
     else
     pinG=LOW;
     digitalWrite(green,pinG);
     }
  }
  
}

This is the original using Delay, it works perfect

#define ECHO 11       
#define TRIGGER 12    
 
unsigned int time;
int distance=0;
int short=30; 
int medium=80
int large=120; 

 
void setup() {
  
  pinMode(ECHO, INPUT);
  pinMode(TRIGGER, OUTPUT);
   pinMode(5,OUTPUT); // Led green 
  pinMode(6,OUTPUT); // led yellow
  pinMode(7,OUTPUT); // led red

} 
void loop(){
 digitalWrite(TRIGGER, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER, LOW);
  // Calculate the distance by timing the high state of pin ECHO
  time= pulseIn(ECHO, HIGH);
  //speed of sound 340m/s or 29 microsecond per cm
  distance= time/58
  
  if(distance<=10){
  
  digitalWrite(7,HIGH); //  led red
  delay(short);
  digitalWrite(7,LOW);
  delay(short);
  }
  else if(distance>=10&&distance<=30){
  digitalWrite(6,HIGH);//  led yellow
  delay(medium); 
  digitalWrite(6,LOW);
  delay(medium);
  }
  else if(distance>=30&&distance<=45){
  digitalWrite(5,HIGH);//  led green
  delay(large); 
  digitalWrite(5,LOW);
  delay(large);
  }
 }

The usual advice regarding the blink without delay example is to read, understand, and embrace the philosophy. You appear to have read, and at least partially understood the example, but you have not embraced the philosophy.

You can't just substitute a call to millis() for a delay(). You need to determine, based on distance, whether to turn an LED on.

Then, completely separately, you need to determine whether an LED has been on long enough to warrant turning off.

That is NOT what you are doing.

Thanks for your replay PaulS!
I am very new programming in arduino. But can't understand what you mean "determine wether an led has to be long on enough to warrant turning off"
Can you show me an expl. Please?

Thank yo again!

But can't understand what you mean "determine wether an led has to be long on enough to warrant turning off"

Suppose that you need to boil an egg for 3 minutes. You put the egg in the water, and bring the water to a boil. When it starts to boil, you note the time.

Periodically, you compare now to then, to see if enough time has passed, to warrant removing the egg from the water.

Note that you don't need to check now vs then if there is no egg in the water. Note, too, that the distance from the stove to the sink has nothing to do with whether you need to remove the egg from the water.

Your task is to determine whether to put the egg in the water, based on the distance from the stove to the sink, and remove it after 3 minutes.

Or, to put Paul's cooking class back into the led world 8), each time through loop() you need to see if it's time to turn the led off. You store the time from millis() when you turn the led on, and so each time through loop() you see if the new time from millis() minus that old stored time is equal to or greater than how long the led should be on for.

You might find this video to be useful.

BTW, 3 minutes is waaaaay to short for a boiled egg Paul....

Thank you very much PaulS and JimboZA. I will try

JimboZA:
BTW, 3 minutes is waaaaay to short for a boiled egg Paul…

If you want a soft-boiled egg, like a poached egg in the shell, it’s not.
If you want a hard-boiled egg then, yes, more time req’d.

Mind you, I live at 1800m 6000' ASL so water boils a bit colder up here, may need a bit more time to cook.