Your problems are NOT in the program, since the same program is running here without any problems.

What is that part I pointed to above?

Where are the Flyback diodes for the relay coils?

Where are the capacitors ... decoupling, and on the regulator?
<br>