en-us_commands:reference:idigitalread
This is an old revision of the document!
Table of Contents
IdigitalRead()
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)
Commands
| 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. |
How It Works
- Read the input
samplestimes consecutively. - If all readings are
HIGH, update the stable state toHIGH. - If all readings are
LOW, update the stable state toLOW. - If
HIGHandLOWare mixed, treat this as brief noise and retain the previous stable state. - If no previous stable state exists and the readings are mixed, return
LOW.
Example
void setup() { pinMode(0, INPUT); } 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); } }
Precautions
- If
samplesis1or less, the input is read only once. - A larger
samplesvalue increases the time taken by each call.3or5reads are recommended for typical input noise checks. - This command reads several times immediately. Use
Ibounce()to remove ms-scale chatter such as mechanical switch bounce.
Built-in Commands in the Same Category
en-us_commands/reference/idigitalread.1788796399.txt.gz · Last modified: by 127.0.0.1
