if structure takes too much time

I also add a thermal sensor for security but the function is not achieved.

How does knowing the temperature relate to security? What does "the function is not achieved" mean?

  delay (200);

Called on each and every pass through loop(). Unconditionally.