help with understanding millis

woodharry:
Hello.
Let me first explain what I am attempting to do with this sketch.
The sensor is there to detect metal when it does, it triggers the servo to open a door that lets the metal out and then closes it.
What I would like to add now is a timer of sorts so that if the sensor does not detect any metal after say 5 secs, the sketch turns the relay off. until the reset button is hit.
I am looking at using millis for this as from what I understand it will count while the rest of the sketch is running, however the examples I have looked at thus far are not 'clicking' in my head for me to understand how to use it correctly. or where to put it.

my code so far

#include <Servo.h>

Servo door;  //calling servo the door
int sensor = 7;  //sensor is on pin7
int relay = 6;  //relay on pin6

int detect = 0; //setting sensor to open

void setup()
{
  pinMode(sensor, INPUT);
  pinMode(relay, OUTPUT);
  door.attach(5); // servo on pin5
 
}

void loop()
{
  detect = digitalRead(sensor);
  digitalWrite(relay, HIGH);
  if (detect == HIGH) {
     door.write(90);
     delay(2000);
     door.write(45);
    }
}




If you need anymore information to be able to help, I will do my best to provide.
I'm more looking for an example using millis for my use, not someone to write my code for me.

Thanks

Here is an approach to doing more than one thing at the same time, each on a timed basis. It is very clear.

That is the start, not all of it but once you learn the above it will be far easier to take the next step.