Ultrasonic sensor with LED, how can I do this?

I think a large part of the problem with the code in #10 is that you have two variables for the door state. If a door is open, it's not closed. If it's closed, it's not open. So to maintain two of them is bound to create confusion and errors.

Also if you want to do this with a flag 'isDoorOpen' you should make it a bool variable and assign it values of 'true' and 'false' instead of '0' and '1'. The readability itself will help avoid errors.

So you would have something like:

if (distance < 10) {
  isDoorOpen = true;
}
else {
  isDoorOpen = false;
}

if (isDoorOpen == true) {
  analogWrite(red_LED, 255);
  analogWrite(green_LED, 0);
  delay (5000);
}
else {
  analogWrite(red_LED, 0);
  analogWrite(green_LED, 255);
}