# Pulse counter
Counts rising-edge pulses up and/or down within configurable limits. Supports clamp and wrap overflow modes.
Pulse counter
I+
I-
R
V
UL
LL
# Inputs
| ID | Abbrev | Name | Type | Default | Description |
|---|---|---|---|---|---|
increment | I+ | Increment | BOOLEAN | false | Pulse input. Each rising edge increments the counter value by 1. |
decrement | I- | Decrement | BOOLEAN | false | Pulse input. Each rising edge decrements the counter value by 1. |
reset | R | Reset | BOOLEAN | false | Pulse input. Rising edge resets the counter to the configured start value. Dominant input. |
# Outputs
| ID | Abbrev | Name | Type | Default | Description |
|---|---|---|---|---|---|
value | V | Value | NUMBER | 0 | Current counter value. |
at_upper_limit | UL | At upper limit | BOOLEAN | false | True when the counter value equals the configured upper limit. |
at_lower_limit | LL | At lower limit | BOOLEAN | false | True when the counter value equals the configured lower limit. |
# Configuration
| ID | Name | Type | Default | Unit | Description |
|---|---|---|---|---|---|
start_value | Start value | NUMBER | 0 | Initial counter value and value restored on reset. Must be within the configured limits. Details: ≥ lower_limit≤ upper_limit | |
lower_limit | Lower limit | NUMBER | 0 | Minimum counter value. Must be less than the upper limit. Details: < upper_limit | |
upper_limit | Upper limit | NUMBER | 1000 | Maximum counter value. Must be greater than the lower limit. Details: > lower_limit | |
overflow_mode | Overflow mode | ENUM | 0 | Behavior when the counter reaches a limit. Clamp stops at the limit. Wrap continues from the opposite limit. Details: Values: Clamp, Wrap |
# State
| ID | Name | Type | Default | Unit | Description |
|---|---|---|---|---|---|
prev_increment | Previous increment state | BOOLEAN | false | Stores the previous state of the increment input to detect rising edges. | |
prev_decrement | Previous decrement state | BOOLEAN | false | Stores the previous state of the decrement input to detect rising edges. | |
prev_reset | Previous reset state | BOOLEAN | false | Stores the previous state of the reset input to detect rising edges. |
# Source Code
View Volang source
channel = input::channel()
value = input::value()
// Handle "reset" input first (dominant input)
if (channel == "reset") {
prev_reset = state::get("prev_reset")
if (value and !prev_reset) {
// Rising edge on reset - restore start value
start_value = config::get("start_value")
output::set("value", start_value)
output::set("at_upper_limit", start_value == config::get("upper_limit"))
output::set("at_lower_limit", start_value == config::get("lower_limit"))
}
state::set("prev_reset", value)
return
}
// Handle "increment" input
if (channel == "increment") {
prev_increment = state::get("prev_increment")
if (value and !prev_increment) {
// Rising edge on increment
current = output::get("value")
upper_limit = config::get("upper_limit")
lower_limit = config::get("lower_limit")
overflow_mode = config::get("overflow_mode") // 0=Clamp, 1=Wrap
if (current < upper_limit) {
current = current + 1
} else if (overflow_mode == 1) {
// Wrap around to lower limit
current = lower_limit
}
// else: Clamp - no change
output::set("value", current)
output::set("at_upper_limit", current == upper_limit)
output::set("at_lower_limit", current == lower_limit)
}
state::set("prev_increment", value)
return
}
// Handle "decrement" input
if (channel == "decrement") {
prev_decrement = state::get("prev_decrement")
if (value and !prev_decrement) {
// Rising edge on decrement
current = output::get("value")
upper_limit = config::get("upper_limit")
lower_limit = config::get("lower_limit")
overflow_mode = config::get("overflow_mode") // 0=Clamp, 1=Wrap
if (current > lower_limit) {
current = current - 1
} else if (overflow_mode == 1) {
// Wrap around to upper limit
current = upper_limit
}
// else: Clamp - no change
output::set("value", current)
output::set("at_upper_limit", current == upper_limit)
output::set("at_lower_limit", current == lower_limit)
}
state::set("prev_decrement", value)
return
}
