A cyclic timer program with input dependent condition

This code solved my problem. Thank you.