I am glad that you got it working but would be interested to see the rest of the code block of the for loop and I hope that your program never needs to do anything during the 40 (or more) seconds
@PerryBebbington
Thank you for this... Well I understand my code is far from perfect but it works perfectly as it is now so why change it...?
Up to you of course but so you learn something. This is how your code works at the moment, based on a visit to a restaurant:
A waiter meets you at the door, takes you to a table, gives you a menu then waits by your table while you decide what to order. The waiter takes your order, goes to the kitchen and waits there while the chef cooks your food. When the food is ready the waiter brings it to your table then waits by your table while you eat it. When you’ve finished eating the waiter takes your plates away and returns to ask if you want anything else. This continues until you leave. No one else gets served. This is how your code is working at the moment.
I’m not going to describe what really happens in a restaurant as you already know. A waiter uses exactly the same system as a state machine to serve people when they need serving and check to see who needs serving next between dealing with customers.
One day you will need code that can do more than one thing at a time.
In short i've built a lift which goes up when LED light is on and goes down when LED light is off. (very simple right...)
OK as far as it goes but what if there are several floors, each with a call button and an emergency stop button in the lift. You would not want to be hanging about with the Arduino doing nothing with users not getting a response.
As others have suggested it would be a useful exercise for you to implement the code in a different way and you will certainly learn a useful technique for when you really need it