Arduino Output after Time

I want to run my code after 10 seconds. Then after that it will detect if the sensor is low for 10 seconds. If yes, it will output something to the relay. I've put Serial Monitor to check. It seems fine. But the relay is not turning on.

This is my code. Hope you can help.

int relay = 7;
int sensor = 8;
unsigned long previousMillis = 0;
unsigned long interval = 10000;



void setup() {

pinMode(sensor ,INPUT);
pinMode(relay, OUTPUT);
Serial.begin(9600);
}

void loop() {
   
 unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval){
    if (digitalRead(sensor) == LOW){
        currentMillis = millis();
    if (currentMillis - previousMillis > 10000){
         digitalWrite(relay, LOW);
    previousMillis = currentMillis;
           Serial.println("Relay");
    }
    }
    else digitalWrite(relay, HIGH);
  }
    else digitalWrite(relay, HIGH);
   
  }

It seems fine.

Why do you say that? What did you see on serial monitor?

Could you explain what you want.

Run code 10 seconds after what ? Do you want to check every 10 seconds if the relay is on ?
Detect if the sensor is low for 10 seconds ? That means if during that time the sensor is high for 10ms, then the 10 seconds start to count from the begin ?
What is output something to the relay ? Turn the relay on or off ?

Anytime you have back to back else statements doing the exact same thing, that’s a clue your executing the wrong order.

Koepel:
Could you explain what you want.

Run code 10 seconds after what ? Do you want to check every 10 seconds if the relay is on ?
Detect if the sensor is low for 10 seconds ? That means if during that time the sensor is high for 10ms, then the 10 seconds start to count from the begin ?
What is output something to the relay ? Turn the relay on or off ?

I want to run the code 10 seconds after turning on. After that if the sensor outputs low for a good 10 seconds, i want to turn off the relay which will also turn off the arduino.

PaulRB:
Why do you say that? What did you see on serial monitor?

When I plugged in the sensors and relay, I waited 10 seconds after bootup. Then I tried to trigger the sensor, the serial does not print. After 10 seconds that the sensor is not triggered, the serial monitor prints relay, but the actual relay does not turn on.

I think we need to see your circuit. Please post a schematic.

Also please perform Auto Format on your code and post it again.

That would be a delay() of 10 minutes in setup() and a millis() timer in the loop(). Just a single simple millis() timer that updates previousMillis each time the sensor is high.

const unsigned long waitTime = 10UL * 60UL * 1000UL;  //  10 minutes, "UL" means "unsigned long"
unsigned long previousMillis;
const unsigned long interval = 10UL * 60UL * 1000UL;   // also 10 minutes


void setup()
{
  ...
  delay( waitTime);   // wait 10 minutes, doing nothing
  previousMillis = millis();     // set 'previousMillis' to this moment to be able to measure 10 minutes in the loop
}

void loop()
{
  unsigned long currentMillis = millis();
  byte sensorValue = digitalRead( sensorPin);

  if( sensorValue == HIGH)
  {
    previousMillis = currentMillis;  // reset the timer by updating 'previousMillis'
  }

  if( currentMillis - previousMillis >= interval)  // LOW for 10 minutes ?
  {
    // turn arduino off
    ...
  }
}

Proper code formatting always helps. This is the sketch from #0 after an autoformat and removing whitespace:

int relay = 7;
int sensor = 8;
unsigned long previousMillis = 0;
unsigned long interval = 10000;

void setup() {
  pinMode(sensor , INPUT);
  pinMode(relay, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval) {
    if (digitalRead(sensor) == LOW) {
      currentMillis = millis();
      if (currentMillis - previousMillis > 10000) {
        digitalWrite(relay, LOW);
        previousMillis = currentMillis;
        Serial.println("Relay");
      }
    }
    else digitalWrite(relay, HIGH);
  }
  else digitalWrite(relay, HIGH);
}

This code implies your relay is active LOW.
So the first 10 seconds the relay is set HIGH.
Then, the moment the sensor is LOW, currentMillis is set to millis() (again - this is redundant), the second if is true, the relay is set LOW, and previousMillis is set to the current millis() value.
For the next 10 seconds the first if statement is true and the relay is set HIGH. This goes so fast your relay doesn't have the time to even react and you won't hear a click.

roblestyle:
I want to run my code after 10 seconds.

That's one of the few reasons to use a delay(). In setup(), specifically.

if you wait for the time in setup.

then in loop

if (millis() > 20000) {
do that thing;
}

if you want to do it in loop()

if (millis() >= interval){
if (sensor == HIGH) {
if (millis() >= interval + 10000 ) {
do that thing;
}
}
}

no need to use blink without delay if we have 0 as the starting point.

the OP said the power is shut off, forcing a re-start at some point so it can all start over again.