Code for turning on relays with limit switches

I am having trouble getting my head around the code for this.

And I would like code written for this could anybody help

1.Starting the program with a momentary switch (switching on Relay 1&2)
2.Activates Limit switch 1(Turning on Relay no.3)
3.Activates Limit switch 2 (Turning off Relay no.2)
4.Activates Limit switch 3 (Turning on Relay no.4)
5.Activates Limit switch 4 (Turning on Relay no.2)
6.Activates Limit switch 1 (Turning off Relay no.3)
7.Activates Limit switch 5 (Turning off Relay no.1&2)

The relays turn on air solenoids when cylinders reach there limit contact limit switch and so on till program is finished
I have a Arduino Uno and a relay board and no/nc limit switches and a momentary switch (no)

Would appreciate any help or direction on how I could get started.


Each relay 1 2 3 and 4 operate air valves on separate cylinders with pushrods. Each cylinder has its own limit switch to say when it is fully extended.
R1 has LS1
R2 has LS2 etc.
When the valve is turned off, what happens to the cylinder rods. Do they stay there or return to initial position.

When does R1 get turned off, so it can be turned back on to activate LS1 for the second time to turn R3 off.

What is your project application?
Tom..... :slight_smile:

Instead of numbering the relays it would be good to name them:

1: Cylinder1Extend
2: Cylinder1Retract
3: Cylinder2Extend
4: Cylinder2Retract

Similarly, name the limit switches.
If the cylinders have a specific purpose, name them as well:

1: LandingGearExtend
2: LandingGeartRetract
3: CanopyOpen
4: CanopyClose

Using meaningful names will make the code much easier to maintain.

Thank you for your replies I am here DownUnder and maybe asleep or building something so I maybe slow in replying.

I have built a turntable with 12 racks for spraying on its all quite large each rack is 800 mm square I am presently setting up a CNC sprayer for it to spray one rack then turn onto the next and so on,

I have also built a machine for turning the product ( which I have presently been using with air toggle valves to operate) it has a rack already loaded into it, a structure extends onto a rack at a given point on the outer edge of the turntable picks up a rack and clamping product retracts then turns or spins racks 180 then extends back into position then dropping back into place then returns home.

Hope that is all clear quite a mouth full.

I am using 5 port 12 volt spring return pneumatic air valves

Relay 1 double acting cylinder locking turntable into exact position
Relay 2 double acting cylinder extends and retracts pickup structure
Relay 3 double acting cylinder pickup and clamping cylinders
Relay 4 rotary cylinder for turning or spinning. (double acting)

I have built all my CNC machines but I find this language difficult and having trouble moving forward.

Would appreciate any Help.

Regards Paul

I think your first step should be to write down the requirement using the sort of nomenclature that @johnwasser suggested.

It may be useful to have some variables to record the state of the system - such as cylinder1Extend == true

This pseudo code is the same idea using arrays (because I was too lazy to write the example the long way). By the way this is a few pieces of code rather than a continuous lump.

cExtend[0] = LOW
cExtend[1] = LOW
if (btnStartPin == HIGH) {
  cExtend0] = HIGH;
if (cExtend[0] == HIGH && cLimitSwitch[1] == LOW) {
  cExtend[0] = LOW;
  cExtend[1] = HIGH;
for (byte n = 0; n < numCyls) {
  cylRelayPin[prevN] = LOW;
  prevN = n;
  cylRelayPin[n] = cExtend[n];