On delay timer help or On milli timer help!

Hi guy's, long time reader first time poster. I have a problem that I just can't understand. I am making a timer relay that I need to activate a few seconds AFTER the switch has been triggered. Also known as ON DELAY. But after days of google searches I finally stopped in here to ask. So like I said a relay to be triggered a few seconds after the switch has been tripped. Also, I have the timer to be adjusted by a pot, I think I figured that one out , but not sure. How can I accomplish this daunting task. Every time I look for an arduino controlled on delay relay it's a rash of "don't use delay use milli" yeah well it is a delay but not the code delay. If that made since. Ok code time.

/////////////////////////////////////////////////// 
//Project 3.04 
//use potentiometer output to set time rate
int potentiometer = A0;
int potRead = 0;
int buttonPin = 2;     // the number of the pushbutton pin
int relay = 12;
int isOn = 1;
long now = 0;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup(){
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
  pinMode(relay,OUTPUT);
  pinMode(potentiometer,INPUT);
  digitalWrite(relay,LOW);
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  
   // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
      if (buttonState == HIGH) { 
        
    potRead = analogRead(potentiometer);
    
  if(millis() > now + potRead){
    
    digitalWrite(relay,isOn);
    isOn = 1 - isOn;
    now = millis();
  }
  } 
  else {
    
    digitalWrite(relay, LOW); 
    
  }
  
}
///////////////////////////////////////////////////

If you guy's have any IO on this matter it would be greatly appreciated, thank you. Sorry I forgot to add, I have looked on this forum but I could only find things close but, I couldn't figure out how to reorder the code.

Try this.
How does it work?
Why am I doing this? potRead * 5

/////////////////////////////////////////////////// 
//Project 3.04 
//use potentiometer output to set time rate
int potentiometer = A0;
int potRead = 0;
int buttonPin = 2;     // the number of the pushbutton pin
int relay = 12;
int isOn = 1;
unsigned long now = 0; //<<<<<<<<<<<<<<added unsigned, use this when using with millis()

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup(){
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP); //<<<<<< ADDED _PULLUP    
  pinMode(relay,OUTPUT);
  //  pinMode(potentiometer,INPUT); //<<<<<< Not needed
  digitalWrite(relay,LOW);
  isOn = 0;
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH)  //so your button if not pushed is at a low then ????? 
  { 
    isOn = 1;
    digitalWrite(relay,isOn);
    now = millis();
  } 
  
  potRead = analogRead(potentiometer);
  
  if( isOn == 1 && millis() > now + potRead * 5 )
  {
    digitalWrite(relay, LOW); 
    isOn = 0;
  }
}
///////////////////////////////////////////////////
if( isOn == 1 && millis() > now + potRead * 5 )

The proper way to use millis() is with subtraction

if( isOn == 1 && millis() - now >=  (potRead * 5) )

And the variables associated with millis() such as now and potRead should be defined as unsigned long

While the compiler won't care, those are not very meaningful names for the variables - especially "now" because if anything refers to the present moment it is millis() itself - not some value that was recorded in the past.

...R

Robin2 and LarryD thank you for your input. It works better that I had it. Just one slight problem, as it is now as soon as I press the switch the relay does stay on for the amount of time the pot is set for. But, how can I press the switch and then a wait time (the Pot) have the relay activate?

I figured it out. I changed the isOn status and added a new line.

/////////////////////////////////////////////////// 
//Project 3.04 
//use potentiometer output to set time rate
int potentiometer = A0;
int potRead = 0;
int buttonPin = 2;     // the number of the pushbutton pin
int relay = 13;
int isOn = 1;
unsigned long now = 0; //<<<<<<<<<<<<<<added unsigned, use this when using with millis()

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup(){
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP); //<<<<<< ADDED _PULLUP    
  pinMode(relay,OUTPUT);
  digitalWrite(relay,LOW);
  isOn = 0;
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH)  //so your button if not pushed is at a low then ????? 
  { 
    isOn = 0;////  Changed 1-0
    digitalWrite(relay,isOn);
    now = millis();
  } 
  
  potRead = analogRead(potentiometer);
  
  if( isOn == 0 && millis() - now >=  (potRead * 5) )//// Changed 1-0
  {
    digitalWrite(relay, HIGH); //// Added
    delay(50);                    //// Added
    digitalWrite(relay, LOW); ////
    isOn = 1;//// Changed 0-1
  }
}
///////////////////////////////////////////////////

But, how can I press the switch and then a wait time (the Pot) have the relay activate?

A bit more explaination please. So the relay is activated for how long?

dhaggy1980: Robin2 and LarryD thank you for your input. It works better that I had it. Just one slight problem, as it is now as soon as I press the switch the relay does stay on for the amount of time the pot is set for. But, how can I press the switch and then a wait time (the Pot) have the relay activate?

When do you want to start the wait time for the relay...when the switch is pressed or when the switch is released?

Sorry LarryD I was posting when you posted. The relay I wanted to activate half a second. It was the time frame getting to activation is what I was having problems with. But now I think I have a good working system now. Now, I have to add 3 more relays and 3 more switches and I'll bee golden.

Henry_Best: When do you want to start the wait time for the relay...when the switch is pressed or when the switch is released?

On release.

But now I think I have a good working system now.

It looks like you made some head way in programming, good for you.

LarryD: It looks like you made some head way in programming, good for you.

Thank you and thank everyone else too. I'm sure I'll run into a snag and come running back.