if this sensor detects a faulty product (denoted by press a push button 4 times with[in] 2 seconds). the reject cylinder comes out and the conveyor stops while this happens. if the sensor detects 2 blips within 2 seconds it is assumed the component is good and the conveyor starts back up.
[...]
Can some one help me how to add the timer for the within 2 seconds?
The program (model) has two modes (actually three, but let's focus on the main two):
- Parts are good, no push button presses detected, God is in His heaven and all is right with the world.
- An initial push button press is/has been detected, we need to detect if we get three more (four total) before two seconds pass since the initial button press
Once that initial press occurs, it's a race:
- Will the counter reach four presses ...
- ... before the timer reaches 2s?
The key word there is
before, because PLC programming is primarily about
time, so
when something happens is more important than
what happens.
The solution is to have the completion output of each "counter" (a timer is a counter, but of time rather than of rising edges) interrupt the "count" (action) of the other "counter;" so the first one to reach its finish line "wins" the race.
Timer
The preset for the timer is 2s.
The input to the timer is the most complex; it must be held high
- (A) from the first button press,
- (B) AND stay high until the counter completes (four button presses total).
(A) Consider what changes after the first button press: the count increases from 0 to 1; subsequent button presses will increase the count. So one way to detect "from the first button press" is to check if the counter accumulator is greater than 0.
(B) The timer should keep running as long as
- BOTH (A) is true (counter accumulator > 0).
- AND counter has not reached its preset of 4 - which is the same as saying the counter output is 0
- -]/[- NC Contact of C0.Q or counter_object.DN or C5:0/DN or etc., depending on the model of PLC
That is the total logic for the timer: if the counter accumulator reaches its preset, it will reset the timer; no additional rungs are needed to reset the timer (although it could be done that way). Also note that, if the the timer expires, that resets the counter accumulator to 0 (see below), which also resets the timer because the (A) comparison (counter accumulator > 0) becomes false.
How we configure those two conditions, (A) and (B) to feed the timer depends on the PLC model and the language chosen (ST, LAD, FBD, etc.).
Counter
The input to the button press counter is simple: it is an
-] [- NO Contact on the button input; the counter has built-in edge detection, and will always, and only, increment on the rising edge each button press. Note that we could AND that button press with the timer not being expired, to prevent a simultaneous final (fourth) button press and timer expiry from being detected and muddling the logic, but I don't think that will be necessary (see Caveat below also).
All that remains is to reset the counter if the timer expires before the counter reaches its preset of 4. The simplest way to do that is to clear the counter accumulator to a value of 0 when timer expiry (.Q, .DN, /DN, etc.) is detected. It is also possible to reset the counter, but in some PLC models that can generate a false count of 0 to 1 if the button happens to be held pressed when the reset occurs.
Also note that, if the counter accumulator reaches its preset, the timer is reset (cf. the previous section), so the counter completion bit (.Q, .DN, /DN, etc.) is that flag that four presses have occurred within 2s, so putting that bit into an
-] [- NO Contact can be used to trigger the reject logic, and the completion of the reject logic can eventually reset the counter.
Caveat
What
does happen if the timer expires on the same cycle that the fourth button press occurs? That is up to the coder choosing in what order to execute the three rungs above : timer rung; counter rung; counter reset rung on timer expiry. Because PLC programming is primarily about
time, so
when something happens (e.g. the order of evaluation of each rung's logic) is more important than
what happens.