Menu
CoddyTech

ADD and SUB

Part of the Counters and Numbers section of Coddy's PLC Programming journey. Lesson 8 of 17.

Math boxes work on numbers. ADD adds its two inputs and writes the sum to a tag; SUB takes the second input away from the first. ADD(Count, 1, Count) means: Count plus 1, written back into Count.

Like MOVE, a math box works on every scan it has power. Hold a button for one second and, on this PLC's 10 ms scan, a plain contact would add 1 about a hundred times. So give it a rising edge contact (P): it passes power for one scan per press.

Order matters in SUB: SUB(Count, 1, Count) is Count minus 1, but SUB(1, Count, Count) is 1 minus Count.

challenge icon

Challenge

Easy

Each press of PB1 adds 1 to Count, and each press of PB2 takes 1 away. Use P contacts so a long press counts once. Rung 3 lights Green at 3 or more.

Try it yourself

{"v":1,"lang":"ld","tags":[{"name":"Count","type":"INT"}],"rungs":[{"els":[]},{"els":[]},{"els":[{"t":"cmp","op":"GE","a":"Count","b":3},{"t":"coil","tag":"Green"}]}]}

All lessons in Counters and Numbers

Practice on your own: PLC playground