Triggering output after set ammount of time

I'm rather new to Arduino coding and was having a bit of trouble getting my code to have an LED trigger after having an IR Transceiver receive constant input for 10 seconds. The LED will trigger and stay on, but will also flash if the IR received an input, then 10 seconds has passed and it received another input. i have attached my code for any possible help.

#define IR_SENSOR 13
#define LED 11
#define LED2 12
 
int ir_reading;

unsigned long trigger = 10000;
unsigned long previousMillis;
unsigned long currentMillis;

void setup()
{
  pinMode(IR_SENSOR, INPUT);
  pinMode(LED,OUTPUT);
  pinMode(LED2,OUTPUT);
}  
void loop()
{
  ir_reading = digitalRead(IR_SENSOR);
  digitalWrite(LED2,HIGH);
  if(ir_reading == 0)
  {
    previousMillis = millis();
    if(previousMillis - currentMillis >= trigger)
    {
      currentMillis = millis();
      digitalWrite(LED,HIGH);
    }
  }
  else
  {
    digitalWrite(LED,LOW);
  }
}

I did't exactly follow your description, but this code will turn an LED on after 10 seconds

#define IR_SENSOR 13
#define LED 11
#define LED2 12

int ir_reading;

unsigned long trigger = 10000;
unsigned long previousMillis;
unsigned long currentMillis;

void setup()
{
  pinMode(IR_SENSOR, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
}
void loop()
{
  ir_reading = digitalRead(IR_SENSOR);
  if (ir_reading == LOW)  {
    // not detected, reset timer
    startMillis = millis();
    digitalWrite(LED, LOW);
  }

  if (millis() - startMillis >= trigger) {
    digitalWrite(LED, HIGH);
  }
}

I see 2 issues:

  1. You should initialize currentMillis to millis() in setup so it has a known value.
  2. You should reset currentMillis to millis() when IR_SENSOR is HIGH (I assume LOW is triggered). This way the LED will only come on if the sensor has been triggered for more than 10 seconds and will stay on until it is not triggered anymore.

Here it is:

#define IR_SENSOR 13
#define LED 11
#define LED2 12
 
int ir_reading;

unsigned long trigger = 10000;
unsigned long previousMillis;
unsigned long currentMillis;

void setup()
{
  pinMode(IR_SENSOR, INPUT);
  pinMode(LED,OUTPUT);
  pinMode(LED2,OUTPUT);
  currentMillis = millis();
}  
void loop()
{
  ir_reading = digitalRead(IR_SENSOR);
  digitalWrite(LED2,HIGH);
  if(ir_reading == 0)
  {
    previousMillis = millis();
    if(previousMillis - currentMillis >= trigger)
    {
      digitalWrite(LED,HIGH);
    }
  }
  else
  {
    currentMillis = millis();
    digitalWrite(LED,LOW);
  }
}

blh64:
I did't exactly follow your description, but this code will turn an LED on after 10 seconds

#define IR_SENSOR 13

#define LED 11
#define LED2 12

int ir_reading;

unsigned long trigger = 10000;
unsigned long previousMillis;
unsigned long currentMillis;

void setup()
{
  pinMode(IR_SENSOR, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
}
void loop()
{
  ir_reading = digitalRead(IR_SENSOR);
  if (ir_reading == LOW)  {
    // not detected, reset timer
    startMillis = millis();
    digitalWrite(LED, LOW);
  }

if (millis() - startMillis >= trigger) {
    digitalWrite(LED, HIGH);
  }
}

blh64 you're version is cleaner except you assume HIGH is triggered. Not sure which it is. I assumed from the OP's code that LOW is triggered.

ToddL1962:
I see 2 issues:

  1. You should initialize currentMillis to millis() in setup so it has a known value.

Global variables are required to be initialized to 0 by the C/C++ standard so currentMillis is guaranteed to be 0 without explicitly setting it.

OTOH, doing so doesn't hurt anything and makes the intention clearer to the reader.

Thank you all very much for your help, not only was I able to get it to work because of your help. I also have a little better understanding of how to apply some of the commands

blh64:
Global variables are required to be initialized to 0 by the C/C++ standard so currentMillis is guaranteed to be 0 without explicitly setting it.

OTOH, doing so doesn't hurt anything and makes the intention clearer to the reader.

Good point blh64 and I knew that. Since I do the millis() timer thing quite often I always initialize it to millis() in setup(). I should have stated more precisely that currentMillis should be initialized to, in fact, the current millis() value. I appreciate you pointing that out so that the OP doesn't think it would be some random value.