Latching Logic and Syntax

New to Arduino and having a heck of a time making the transition from programming PLCs using ladder logic. I'd like a recommendation how to approach programming the following setup.

  • 3 lights, each attached to a relay output (L1=off, L2=on, L3=complete)
  • 2x digital inputs...1 for cycle status (on/off) and 1 for door status (open/closed).

I was able to successfully monitor the cycle status input to activate lights 1 & 2, but I need to have light 3 come on after a completed cycle then turn off/reset when the door is opened. Closing the door should not turn light 3 back on again. Am I on the right track here or is there any easier way to do this? Doing this logic in ladder I would use a latch to fire light 3 when complete and release when the door is opened. After the door is closed again, light 3 would not turn on again until another cycle has completed.

Here's the current code in my loop

  // Read digital pin D35 = Cycle in progress. L2 On during cycle.
  int cycleOn = digitalRead(35);
  // Set RELAY6 state according to D35 "ACTUAL: ON=ON, OFF=OFF"
  digitalWrite(PLDuino::RELAY6, cycleOn);

  // Read digital pin DXX = Cycle completed. L3 On after cycle until door switch is opened.
  // Set RELAY5 state according to D35 change while D30 stays the same
  int doorClosed = digitalRead(30);
  digitalWrite(PLDuino::RELAY5, doorClosed);  

  // Read digital pin D35 = Cycle in progress. L1 Off during cycle.
  int cycleOff = !cycleOn;
  // Set RELAY4 state according to D35 "OPPOSITE: ON=OFF, OFF=ON"
  digitalWrite (PLDuino::RELAY4, cycleOff);

Here's the current code in my loop

Given that you only posted some of your code, I'll assume that you are only looking for some of an answer.

So, what you need to do is...

Well, that's some of the answer. Hope that helps.

I really don't understand why you need an int to store the value, even temporarily of not cycleOn. You CAN use !cycleOn in the call to digitalWrite().

Skibber:
I was able to successfully monitor the cycle status input to activate lights 1 & 2, but I need to have light 3 come on after a completed cycle then turn off/reset when the door is opened. Closing the door should not turn light 3 back on again.

You need to write down the complete sequence of what you want to happen as a series of steps with one step on each line. Make each step as simple as possible so there is no ambiguity.

I suspect when you have done that you will have 80% figured out how to solve your problem.

The piece of code you have posted is meaningless to me in the absence of the details of the project.

...R
Planning and Implementing a Program

Robin2:
You need to write down the complete sequence of what you want to happen as a series of steps with one step on each line. Make each step as simple as possible so there is no ambiguity.

Components

  1. machine cycle digital output: low=off, high=on
  2. arduino cycle digital input: low=off, high=on
  3. arduino machine door status: low=closed, high=open
  4. 3 lamps that signal statuses

Sequence

  1. apply power to machine to be monitored
  2. monitor machine cycle output with digital input 35 on the arduino PLC
  3. machine is idle = lamp 1 is on, lamp 2 is off
  4. monitor machine door switch status
  5. door is closed = lamp 3 is off
  6. door is open = lamp 3 is on
  7. machine cycle starts and is active = lamp 1 is off, lamp 2 is on, lamp 3 is off
  8. machine cycle ends = lamp 1 is on, lamp 2 is off, lamp 3 is on (door is closed though)
  9. open door to remove part = lamp 1 is on, lamp 2 is off, lamp 3 is on
  10. close door and go back to step 2

How much experience do you have writing C or C++.

I think the most relevant tool you're missing here is a static variable to record the inputs. Each loop then involves comparing the new digitalRead values to the previously recorded value. This allows you to change your outputs only when your input changes. Then you have the power to light the completed indicator when the cycle finishes and then kill it only when the door closes.

You need some variables in the Arduino program that represent the state of the machine - for example
machineWorking = true (or false) (for steps 1, 7 and 8 )
doorOpen = true (for steps 5, 6 and 9)

and it might be useful also to have a variable (cycleComplete ?) to represent the change from machineWorking = true to machineWorking = false as that seems to be the signal for step 8

i wonder is there a door state prior to step 5 which you have not mentioned?

...R

How did you describe such a logic before, for PLC?

What you want is a state machine (DFA) most probably. Did you ever use state diagrams?

As a first step, use names instead of pin numbers.

#define pinDoor 2
bool doorOpen; //door state
...
 doorOpen = digitalRead(pinDoor);
...
 if (doorOpen && machineWorking) ...
//or
 digitalWrite(lampOn, powerApplied && !doorOpen); //AND gate equvalent
...

Perhaps it helps if you design your code like a synchronous circuit, with a clock signal and latches for all input signals.
Then every iteration of loop() is equivalent to a clock cycle. First the input signals are read and stored in boolean variables (latches), including debouncing of mechanical switches. Then the outputs are set to their logic conditions (gates), based on the latched values.

Boardburner2:
How much experience do you have writing C or C++.

Zero...this is my first go. Been studying the language reference page on the arduino site as much as I can

This may be of interest.

Not tried it yet though.

http://cq.cx/ladder.pl

It says it outputs C code for use by other compilers so could give a few clues.