Mixing manual and automatic logic in one flow is the first step toward an unmaintainable program. The right structure: the main program only decides the mode and handles common concerns, while manual and automatic logics live in their own subroutines, selected by the mode bits. The two never cross, never share output drives — and mode switching becomes predictable. The sections below cover the architecture, interfaces, switching and pitfalls.
| Stage | Content | Key point |
|---|---|---|
| Common processing | Input debounce, E-stop/safety collection, alarm handling | Mode-independent, runs always |
| Mode decision | Read the mode switch, derive the active mode | The only place the mode state is written |
| Call branch | Manual → CALL manual subroutine; Auto → CALL auto subroutine | Exactly one call per scan |
| Output summary | Subroutine intermediates + interlocks → final outputs | One shared exit for both modes |
Two core rules: ① exactly one mode's subroutine executes per scan (CALL one of two); ② both subroutines drive only their own intermediates — the outputs Y are driven once, in the summary stage. That is the root of "mode switching never fights".
Manual logic = button-driven + simple interlocks, no self-holding sequences: hold to move, release to stop (jog), or SET with basic conditions RST. Points: ① every action carries "manual mode AND its button", broken when either fails; ② mechanism interlocks still apply (two cylinders that would collide never extend together); ③ home/position conditions join the permissions (no fast motion before homing). The manual program should answer every press with exactly one predictable response.
Automatic logic = a stepping flow: step flags + transition conditions, or SFC. Points: ① the auto subroutine's entrance checks "auto allowed" (E-stop clear, no alarms, home conditions); if not allowed, clear the step number and hold at step 0; ② each step's outputs write to auto-specific intermediates; ③ the flow ends back at step 0. The auto subroutine never reads manual buttons — mode isolation means the two sides do not look at each other's signals.
| Action | Why |
|---|---|
| Clear the auto flow (step number to 0, auto intermediates cleared) | Residual steps would fire the mechanism mid-way at the next auto start |
| Manual outputs to safe positions, or wait for the operator | Nothing may move by itself at the switch instant |
| Trigger on the mode switch's edge, not the level | A level trigger re-clears every scan; an edge clears once, cleanly |
Put the switching logic in its own small "mode-switch handling" stage, triggered by the mode switch's rising/falling edge, performing "clear auto + confirm manual initial state". The first response after any mode change must be stillness — never spontaneous motion.
The two subroutines share only three classes: safety state (E-stop, alarms), mechanism feedback (in position, home), and output intermediates (each writes its own, merged in the summary). Rules: ① dedicated intermediate zones per mode (a manual M block / an auto M block), never reused; ② shared read-only data lives in a common area neither writes; ③ the output summary states every Y's source: manual intermediate OR auto intermediate, ANDed with the global interlocks.
| Symptom | Cause | Fix |
|---|---|---|
| Auto flow keeps running after switching to manual | Auto subroutine lacks the mode gate | AND the auto mode bit at the auto entrance |
| Mechanism lurches when switching back to auto | Step number not cleared, resumes mid-flow | Clear the step number to 0 on the switch edge |
| Manual-held motion persists after switching | Manual intermediates not cleared | Clear the manual zone at the switch |
| Two mode switches fight | Both switches write the mode bit directly | The mode bit follows one combining logic, commented |
| Double coils | Manual/auto each drive Y directly | All outputs unified in the summary |
Mode separation means "one will commands the mechanism at a time". Subroutines give it a physical boundary (CALL one of two), intermediate zones give it a data boundary (each writes its own), and the output summary is the last line of defence (unified interlocks). With the three boundaries up, switching is always clean.
Operating modes (manual/automatic), stop and reset design follow GB 5226.1 / IEC 60204-1 (control functions and operating modes); subroutine organisation follows IEC 61131-3 convention; CALL instruction details follow the PLC's programming manual.
Delivery check: search the program for every Y coil — all of them live in the output summary; and run the "zero motion at switch instant" test with the mode selector.