====== Digital inputs: debounce, counter and direct switch ====== The digital inputs on Unipi controllers have interesting "add-ons", which help accuracy and responsiveness of the regulation. These add-ons are configured in the CPU of the input/output backplane board in Unipi, so they are SW independent and therefore very fast. ;;# Patron \\ Neuron \\ Axon \\ Extension ;;# ===== Debounce ===== The **debounce** functionality is for catching glitches which typically occur when changing states on the digital input. Those happen mostly when the digital input is operated by some mechanical element, e.g. momentary switch. The signal generated upon pressing and releasing a momentary switch can look like this: {{ :en:sw:01-mervis:digital-inputs-01-debounce.jpg?direct&400 |}} To "debounce" these glitches, you have to slow down the reaction on changing states of the input. On HW level, you could achieve this by employing an RC element for example. To make your life easier, you can set this "slowing down" on SW level. The functionality looks for changes on a digital input. If the change is less then defined debounce time, it will be ignored. Once the change exceeds the debounce time, then the event is passed to the Mervis. By default, all digital inputs are equipped with 5 ms debounce time Now let's see how debounce can be configured in the Mervis IDE. The settings are made via the device inputs, so double-click on the name of your Unipi controller under the Modbus TCP channel. A tab with all controller registers will appear on the **Main window**. Under the **Name** field, you will find a search bar in which enter "DI_". A list of all available digital inputs, or more precisely their data points, is displayed. {{ :en:sw:01-mervis:digital-inputs-02-list-of-inputs.png?direct |}} Select the desired digital input, for which you want to set the debouncing. In the **Properties panel** you can see all the options you can set on this register. The option we are interested in right now is the **Debounce**. The value of this option is represented in milliseconds of deboucing period. {{ :en:sw:01-mervis:digital-inputs-03-debounce-time.png?direct |}} We just changed the configuration of the PLC modules, which is not applicable by simply uploading the solution. To save the configuration, follow the [[en:sw:01-mervis:unipi-configure-module-hidden|UniPi - Configure Module]] tutorial. If you are about to set options on multiple registers, you can do it at once and **Configure module** afterwards ===== Counter ====== Another interesting functionality of **digital inputs** is the **counter**. It is a software-independent, high-speed counter of rising edges of changing signal on the digital input. In simple words, it can count short pulses very precisely. The speed of Mervis programs is not enough for detecting quick changes of digital inputs traditionally (with the "Pulse counter (BD30)" FUPLA box for example). If you have a [[https://en.wikipedia.org/wiki/Slot_car|slot car track]] and you want to count the number of passes of slot cars, you wouldn't be able to detect it, since the length of pulses from the "pass" sensors will be in units of milliseconds. Speed of the slot car is up to 80 kmph and with the length of the car about 10 cm is the pass of the whole car above the sensor about 4.5 ms. The HW counter depends on the **Debounce** settings. Therefore the **Debounce** has to be set to lower value than is the expected length of the pulses to count. Each **digital input** is equipped with **counter** and you don't need to set anything to use it. The counter is available through a global variable. For the input named ''Neuron_L503_DI_1.01'' we have counter variable named ''Neuron_L503_CNT_1.01''. To demonstrate how slow is the software counting, look at this program in debugging mode: {{ :en:sw:01-mervis:digital-inputs-08-sw-counter-vs-hw-counter.png?direct |}} As you can see, we are counting the rising edge events on the ''Neuron_L503_DI_1.01'' through ''Pulse counter (BD30)'' function block, and reading the HW counter of the same input via the ''Neuron_L503_CNT_1.01'' variable. Afterwards, we compare those two variables in ''eq'' function block, and if they are equal, we will turn the ''X1'' LED on the controller ''ON''. Once the two counters are not equal, the LED will be ''OFF''. The debugging started with both counter values at 0. We connected a momentary switch to the ''DI1.1'' input and start pressing the switch very quickly. As you can see, the software counter detected only 10 presses, but the hardware counter detected 54. The counter variables are read-only, therefore you cannot set or reset them directly from the program or the variable browser in debugging mode. You can set or reset the variables via the "set" variable. For the ''Neuron_L503_CNT_1.01'' the set variable is called ''Neuron_L503_CNT_1.01_set''. ===== Direct switch ===== Last extra functionality (but not least) is the direct switch. As we discovered, the software detection of changes is far from being instantaneous. And there are applications, where even 20 ms delay can cause problems, or represent some discomfort at least. A typical example is a switch controlling a light. What we expect from the behaviour is the instant change of the lighting upon pressing the switch. That, as demonstrated above, is not possible to achieve in the software by simple wiring the DI to DO. The direct switch is the configuration of digital input in a way it will directly control digital or relay output of the same name. The wiring of the input and output is fixed a cannot be changed, only turned on or off. On Group 1, the DIs are wired to DOs. On other Groups, the DIs are wired to ROs. On Neuron L503, the direct switch (DS) on DI1.1 is wired to DO1.1. The DS on DI3.14 is wired to RO3.14 Direct Switch configuration is performed in the same way as for **Debounce**. In the **left panel**, double-click on the name of your Unipi controller under the Modbus TCP channel, in the **main panel** find the appropriate DI, click on it and in the **Properties** panel find the **Direct Switch** field. {{ :en:sw:01-mervis:digital-inputs-09-direct-switch.png?direct |}} As you can see, the option has a dropdown menu with different modes. * **Disabled**: the direct switch is disabled * **Copy**: The status of the DI is copied to the DO/RO. So when the DI is ''TRUE'', the DO is ''TRUE'' and when the DI is ''FALSE'' the DO is also ''FALSE'' * **Copy (Invert)**: Same as above but inverted. When the DI is ''TRUE'' the DO is ''FALSE'' and vice versa. * **Toggle**: This mode toggles the DO on the rising edge of the DI. Handy for controlling light with a momentary switch for example After setting the configuration, you need to download it to the module. See the [[en:sw:01-mervis:unipi-configure-module-hidden|UniPi - Configure Module]] tutorial. The **Direct switch** depends on the **Debounce** settings. To have an instant change, you need to set the **Debounce** to value which doesn't deny the purpose of the **Direct switch** and yet to value, which doesn't deny the purpose of the **Debouncing**. 5-10 ms should be OK.