Problem with edge detection

I've written a simple sketch to detect an object passing in front of a light sensor (LDR in the code). When the sensor detects an object it moves from the "scanning" to "object detected" function, and when the object clears the sensor the program runs the "warning" function, pauses, then returns to the "scanning" function.

int LDR=0;
int LDRValue = 0;
int previousLDRValue=255;
int previousLDRValue2=0;
int light_sensitivity = 200;

void setup() {
  {
    Serial.begin(9600);
  }
}

void loop() {
  scanning();
}

void scanning()  {   
   LDRValue = map(analogRead(LDR), 0, 1023, 0, 255);  //reads the ldr's value through LDR which we have set to Analog inut 0 "A0"
   Serial.println(LDRValue); //prints the LDR values to serial monitor
   Serial.println("reversing");
   delay(100);                  //This is the speed at which LDR sends value to Arduino
   
   if (LDRValue < light_sensitivity) {
      if (previousLDRValue > light_sensitivity) {
        object_detected();
      }
   }  
   previousLDRValue = LDRValue;   //save light sensor state 
  }

void object_detected() {
   LDRValue = map(analogRead(LDR), 0, 1023, 0, 255);  //reads the ldr's value through LDR which we have set to Analog inut 0 "A0"
   Serial.println(LDRValue); //prints the LDR values to serial monitor
   Serial.println("object detected");
   delay(100);                  //This is the speed at which LDR sends value to Arduino
   
   if (LDRValue > light_sensitivity) {
      if (previousLDRValue2 < light_sensitivity) {
        warning();
      }
   }
   else {
        object_detected();
   }    
   previousLDRValue2 = LDRValue;   //save light sensor state 
}

void warning()   {
      Serial.println("warning");
      delay(3000);   //pause
      scanning();  //go back to scanning
     }

This all works fine.... once. After the "warning" function, the arduino goes back to the scanning function, like it should. Then it detects the object again, like it should. But once the sensor is cleared, it does not display the warning again - it goes straight back to the scanning function.

I'm really scratching my head here. I think I've missed something really obvious. Any ideas?

Try this to aid in finding out where the problem is.
I used LDRValue = 100 and the code got into infinite loop due to recursive code - see NOTE.
That will cause problems.
I need to add some more "break points " to emulate problem as you described it. Stand by.

void scanning()  {
  LDRValue = map(analogRead(LDR), 0, 1023, 0, 255);  //reads the ldr's value through LDR which we have set to Analog inut 0 "A0"

force to known level 

  LDRValue = 205; 


  Serial.println(LDRValue); //prints the LDR values to serial monitor
  Serial.println("reversing");
  delay(100);                  //This is the speed at which LDR sends value to Arduino

so why the delay here? you already read the sensor 

  if (LDRValue < light_sensitivity) {
    if (previousLDRValue > light_sensitivity) {
      object_detected();
    }
  }
  previousLDRValue = LDRValue;   //save light sensor state
  for(;;); 
}

Recursive when LDRValue = 100

void object_detected() {
  LDRValue = map(analogRead(LDR), 0, 1023, 0, 255);  //reads the ldr's value through LDR which we have set to Analog inut 0 "A0"
  Serial.println(LDRValue); //prints the LDR values to serial monitor
  Serial.println("object detected");
  delay(100);                  //This is the speed at which LDR sends value to Arduino
  if (LDRValue > light_sensitivity) {
    if (previousLDRValue2 < light_sensitivity) {
      warning();
    }
  }
  else {
    object_detected();

Recursive when LDRValue = 100 



  }
  previousLDRValue2 = LDRValue;   //save light sensor state
  for(;;); 
}

I see this project screaming for a simple state machine layout.

Why, may I ask?

There are only two states - leading edge and trailing edge or no light / light.

But I agree any time you combine / cascade "if", especially without comments and taking care of "false" conditions the code gets messy.

State machine is much more readable and it helps in troubleshooting.

Yes, I do normally use state machines for things like this, but I thought something so "simple" (two edge detections) wouldn't need one.

So ignoring the state machine solution, what should I do to make sure that...

Delta_G:
each of those functions should do its job and return and let loop have the logic to figure out which one to call.

Even without the endless recursion, why isn't warning() being triggered the second time?

Yep, moving the code to a state machine showed no problems. Surely the same result could have achieved with cascading "if"s though.

OT / hijacked

I'll need a positive pulse detector and its length in near future and after this discussion I came up with this.
Any comments would be appreciated.

Just out of curiosity - is there a simple way to add negative pulse detection length into this code?
I do not see it.

void setup() {
  {
    Serial.begin(115200);
  }
  int NewInput, OldInput = 0;
  long PulseStart, PulseEnd, Pulse;
  delay(2000);
  while (1)
  {
    NewInput = random(0, 2);
    Serial.print("New Input  = " );
    Serial.println(NewInput);
    if (NewInput == OldInput)
    {
      Serial.println("No change " );
      // continue;                    only "if" without "else"
    }
    else
    {
      if (NewInput)
      {
        Serial.println("Rising edge  " );
        PulseStart = millis();
      }
      else
      {
        Serial.println("Traiing     edge  " );
        PulseEnd = millis();
      }
      if (PulseEnd - PulseStart > 0 )
      {
        Serial.print( "Pulse positive  = ");
        Serial.println(PulseEnd - PulseStart);
      }
      OldInput = NewInput;
      Serial.print("Set old input =  " );
      Serial.println(OldInput);
    }
    delay(5000);
  }
}
void loop() {
  scanning();
}

is there a simple way to add negative pulse detection length into this code?

What do you mean by "negative pulse detection length"? The Arduino can not handle negative voltage, so, by definition, negative pulses are not possible. Measuring the length of something that doesn't exist won't take long.