IdigitalRead() reads the input several times consecutively and updates the state only when all values match. If HIGH and LOW are mixed, it retains the previous stable state.
bool IdigitalRead(uint8_t pin, uint8_t samples)
| Command | Arguments | Return Value | Operation |
|---|---|---|---|
IdigitalRead(pin, samples) | uint8_t pin — Digital input pin numberuint8_t samples — Consecutive read count. Values of 1 or less read once; supports up to 255 reads | bool | Reads the input several times consecutively and updates the state only when all values match. If HIGH and LOW are mixed, it retains the previous stable state. |
samples times consecutively.HIGH, update the stable state to HIGH.LOW, update the stable state to LOW.HIGH and LOW are mixed, treat this as brief noise and retain the previous stable state.LOW.
This example reads MPAINO-8A8R(T) input 0 and displays the result on output 64.
void setup() {} void loop() { bool input = IdigitalRead(0, 5); // Read the input repeatedly and update only if all samples match. If HIGH and LOW are mixed, retain the previous stable state. if (input) { digitalWrite(64, HIGH); } else { digitalWrite(64, LOW); } }
samples is 1 or less, the input is read only once.samples value increases the time taken by each call. 3 or 5 reads are recommended for typical input noise checks.Ibounce() to remove ms-scale chatter such as mechanical switch bounce.