Simple delay sketch with intermitent follow through

I am a new-be to the programing/sketch writing. I have a simple trigger/motion sensor to start the a series of air cylinders in a looping series. I am using a 5volt input to command a 12volt relay board to 4 separate air solenoid's(12volt). I can run this sketch and runs fine for a few times( sometime with no issues) then if I let the sketch sit for a min or longer it has a hard time completing 3rd command and the Arduino sends a command to energize all relays at one time(locking up the flow and direction of all air cylinder. I have tried to reset the Arduino and reprogram it using the same sketch, but still have the same issue. I have read up on using the Millis instead of delay but not sure if this will help me out or not. Here is my sketch below that I am currently running.

Proto-1-can.ino (975 Bytes)

Also will a overloaded solenoid( air valve) back feed power to the Arduino causing it to command all the relays on at one time?

Welcome to the forums. Please read the sticky post at the top of the forum about how to post your code using code tags rather than attach it. It makes it easier for people to help you.

You did not provide a circuit diagram showing how everything is wired up which would be very helpful with your problem. Here's a couple of problems that are common.

How is your input wired up? You have it as an INPUT which means it needs an external pullup or pulldown resistor. Do you have that installed?

The arduino can not provide enough current to power a solenoid. Is the 12V supply that is powering the solenoid the same as what is powering the Arduino? Do you have a common ground for the Arduino and the relays and the solenoids?

Confirm your solenoids have kickback diodes and your switch is wired properly.

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:

Using delay()s can make your sketch blocking, but in simple sketches you can get away using it.