Script with conditions

When I said to create a block diagram, I should have said create a flowchart that shows what will happen. Something like this


You need to build a complete flow chart of what you need to do because I don't understand your writeup.

You can draw it on paper or use the site I used to produce this above flowchart

Doing this will help you determine how your system will work.