I had similar circuit for a battery charger setup. I used 2 main loops, 1 for off and 1 for on. you can use for-next or while loops. set the counts based on how often you want to check the a/d values and then pause in loop....... for the 1 minute loop, pause 100, check the a/d (10 times a second) and the loop count would be 600, then either exit loop because of a/d or at end of loop, turn off relay and jump into 5 minute loop. The instructions in the looping take little time but you can adjust the pause or loop count to make the timing more correct.