Table of Contents
Ictu()
Ictu() counts input rising edges and returns true when the preset value is reached.
bool Ictu(uint8_t counter_id, bool CU, bool RESET, int32_t PV)
Commands
| Command | Arguments | Return Value | Operation |
|---|---|---|---|
Ictu(counter_id, CU, RESET, PV) | uint8_t counter_id — Counter identifier. Use the same number for the same counting taskbool CU — Count input. Increments by 1 when it changes from LOW to HIGHbool RESET — When true, reset the current count to 0int32_t PV — Target count. Returns true when the current count reaches or exceeds this value | bool | Count rising input edges and return true when the preset is reached. |
Beginner-friendly explanation
Ictu() does not measure how long a sensor stays on. It counts how many times the sensor changes from LOW to HIGH. For example, when one item passes the sensor, the input changes LOW → HIGH → LOW and the counter increases by 1. A sensor that remains HIGH is counted only once.
Read the call Ictu(counter_id, CU, RESET, PV) as follows.
counter_id: Identifies one counter among several counters. Always use the same number for the same counting task.CU: The count input. The current count increases by 1 when this input changes fromLOW → HIGH.RESET: Whentrue, clears the current count to 0. While RESET is active, CU is not counted even if it turns on.PV: The target count. The return value becomestruewhen the current count reaches or exceeds this value.
Call it continuously from loop() so it can detect input changes. The example below counts five items with input 0 and turns on the board's status LED when the target is reached. Pressing the reset button on input 1 starts the count again from zero.
Example
const uint8_t COUNTER_ID = 0; const uint8_t ITEM_SENSOR_PIN = 0; const uint8_t RESET_BUTTON_PIN = 1; const uint8_t FULL_LAMP_PIN = LED_BUILTIN; const int32_t BOX_CAPACITY = 5; void setup() { Serial.begin(115200); } void loop() { const bool itemSensor = digitalRead(ITEM_SENSOR_PIN) == HIGH; const bool resetButton = digitalRead(RESET_BUTTON_PIN) == HIGH; const bool boxFull = Ictu(COUNTER_ID, itemSensor, resetButton, BOX_CAPACITY); // Count rising input edges and return true when the preset is reached. digitalWrite(FULL_LAMP_PIN, boxFull ? HIGH : LOW); static bool previousBoxFull = false; if (boxFull && !previousBoxFull) { Serial.println(F("5 items counted: box is full")); } previousBoxFull = boxFull; }
Precautions
CUcounts onlyLOW → HIGHtransitions. If the signal remainsHIGH, the count increases only once.- When
RESETistrue, the current count is cleared to 0 andCUis not counted during that call. - Set
PVto a target count of 1 or greater. IfPVis 0 or less, the initial count of 0 is already considered to have reached the target. - Input changes can be missed when sensor pulses are faster than
loop(). Use the product's high-speed counter for fast pulses. - Use a different
counter_idfor each counting task. Do not share one number among CTU, CTD, and CTUD. - Use
IgetCount()with the samecounter_idonly when you need the current count.
