Magazine Counter
medium
Tracks parts in a feeder magazine: reload fills the count, then each dispense edge counts down to zero.
What it teaches
The mirror of Count Up: a feeder magazine holds a fixed number of parts, such as screws or labels. Reload fills the count, then each dispense edge counts one part down. The floor is the lesson — a plain subtraction happily goes negative, so you clamp at zero and report empty instead.
Inputs and outputs
| Tag | Type | Default | Description |
|---|---|---|---|
| In_Dispense | BOOL | — | Part dispensed; one count per rising edge |
| In_Reload | BOOL | — | Reload button; rising edge loads a full magazine |
| Cfg_MagazineSize | DINT | 5 | Parts in a full magazine |
| Val_Remaining | DINT | — | Parts left in the magazine |
| Sts_Empty | BOOL | — | 1 while the magazine is empty |
Required behavior
- A rising edge of In_Reload sets Val_Remaining to Cfg_MagazineSize.
- Each rising edge of In_Dispense decreases Val_Remaining by 1; a held signal counts once.
- If In_Reload and In_Dispense rise in the same scan, reload wins and Val_Remaining becomes Cfg_MagazineSize.
- Val_Remaining never goes below 0 — dispense edges while empty leave it at 0.
- Sts_Empty is 1 whenever Val_Remaining is 0, including at power-up before the first reload.
- A signal already high at power-up is not a rising edge; counting starts from the first fresh 0 to 1 transition.