User Tools

Site Tools


en-us_commands:reference:idigitalread

This is an old revision of the document!


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.

IdigitalRead command operation flow

bool IdigitalRead(uint8_t pin, uint8_t samples)

Commands

Command Arguments Return Value Operation
IdigitalRead(pin, samples) uint8_t pin — Digital input pin number
uint8_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 samples times consecutively.
  • If all readings are HIGH, update the stable state to HIGH.
  • If all readings are LOW, update the stable state to LOW.
  • If HIGH and LOW are 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 samples is 1 or less, the input is read only once.
  • A larger samples value increases the time taken by each call. 3 or 5 reads 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 Command List

en-us_commands/reference/idigitalread.1788791734.txt.gz · Last modified: by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki