automatice alcohol spray

Hi, I am trying to write a code for automatic alcohol spray by using ultrasonic sensor . my goal is that after ultrasonic sensor detect hand as obstacle ,turn on and turn off relay then stop until the hand going aside.
but i don’t know why the code is not working. please lead me to correct code.

#define echoPin 3                // defining pins and variables
#define trigPin 4
int long duration;
int distance;
unsigned long timer=millis();

const int relay= 10;             // pin connected to In pin of relay
void setup()
{
  pinMode(echoPin,INPUT);        // defining pin modes
  pinMode(trigPin,OUTPUT);  
  pinMode(relay, OUTPUT); 
  digitalWrite(relay,HIGH);     // Keep relay pin High for turn off the output       
}

void loop()
{
  digitalWrite(trigPin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  duration=pulseIn(echoPin,HIGH);
  distance=(duration*0.034/2);   // now you got the distance
  if(distance<=20)
  {
  timer=millis(); 
  }

  if(distance<=20 && timer<=3000) {              
    digitalWrite(relay,LOW);    
    delay(2000); 
    digitalWrite(relay,HIGH);    
    delay(2000);              
  }
  
}

Hint: You've got the millis logic wrong...See this simple millis() tutorial...

I don't have the time now to explain in detail so I hope another forum member can do that...

timer<=3000will only be true up to three seconds after reset.

unsigned long timer=millis();

Will only assign the current millis passed to the variable...Surely not what you were expecting?