====== analogRead2Avgf() ====== ''analogRead2Avgf()'' returns a floating-point average of 4~20mA input values. {{:en-us_commands:reference:analogread2avgf.svg?900|analogRead2Avgf command operation flow}} ''float analogRead2Avgf(uint8_t ch, uint8_t samples)'' ===== Commands ===== ^ Command ^ Arguments ^ Return Value ^ Operation ^ | ''analogRead2Avgf(ch, samples)'' | ''uint8_t ch'' — MPINO: analog pin such as ''A0''; MPAINO: overall module channel number starting at ''0''\\ ''uint8_t samples'' — Number of moving-average read samples; up to ''20'' | ''float'' | Returns a floating-point average of 4~20mA input values. | ===== Example ===== Connect an X input module to MPAINO-8A8R(T) and select **Analog Input Module → X**. Set CH0 DIP switches 20mA and GND to ON, then connect the 4–20mA signal. void setup() { Serial.begin(115200); } void loop() { const float value = analogRead2Avgf(0, 5); // Read the floating-point moving average of up to five recent valid values. if (value == (float)ANALOG_READ2_AVG_ERROR) { Serial.println(F("4-20mA input error")); } else { Serial.println(value, 2); } delay(500); } ===== Precautions ===== * Set the MPAINO X module CH0 DIP switches 20mA and GND to ON, and wire the current input with the correct polarity. * An input of approximately 3.6~4.0mA returns the normal value ''0''; inputs below 3.6mA return an error value. * Error values help detect open circuits. The user must implement final alarms and safety actions in the program. * ''IanalogRead2()'' and ''IanalogRead2f()'' also return ''65535'' on error, rather than a user-range value. Check for errors before using the result. * The full ''int32_t'' range is available for ''min~max'' in ''IanalogRead2()'', but if the normal range includes ''65535'', it cannot be distinguished from an error value. * ''analogRead2Avgf()'' uses state and a buffer separate from ''analogRead2Avg()''. Using both increases SRAM usage. * If ''samples'' is ''0'' or memory allocation fails in ''analogRead2Avgf()'', it returns the current valid corrected value as a ''float''. * On MPINO, do not use ''analogRead2()'' on voltage/NTC channels that are not 4~20mA inputs. * For MPINO products requiring ''analogReference(EXTERNAL)'', set it first in ''setup()''. * ''IanalogWrite2()'' and ''IanalogWrite2f()'' are convenience outputs dedicated to 4~20mA. Avoid alternating them with ''IanalogWrite()'' on the same channel. * The analog output circuit requires an external 12~24V supply. USB power alone may not provide normal output. * Verify actual current input/output with measuring instruments after preparing a test product. ===== Built-in Commands in the Same Category ===== * [[:en-us_commands:reference:analogread2|analogRead2() View detailed description]] * [[:en-us_commands:reference:analogread2avg|analogRead2Avg() View detailed description]] [[:en-us_commands:builtin|← Built-in Command List]]