사용자 도구

사이트 도구


commands:reference:analogread2avg

문서의 이전 판입니다!


analogRead2Avg()

analogRead2Avg()는 4~20mA 입력값의 정수 평균을 반환합니다.

analogRead2Avg 명령어 동작 흐름

int32_t analogRead2Avg(uint8_t ch, uint8_t samples)

명령어

명령어 인수 반환값 동작
analogRead2Avg(ch, samples) uint8_t ch — MPINO는 A0 같은 아날로그 핀, MPAINO는 0부터 시작하는 전체 모듈 채널 번호
uint8_t samples — 읽기 이동 평균 샘플 개수. 최대 20개까지 적용
int32_t 4~20mA 입력값의 정수 평균을 반환합니다.

예제

void setup() {
  Serial.begin(115200);
}
 
void loop() {
  uint16_t raw2 = analogRead2(0);
  int32_t filtered2 = analogRead2Avg(0, 5);
  float filtered2f = analogRead2Avgf(0, 5);
  float currentmA = IanalogRead2f(0, 4.0F, 20.0F);
 
  if (raw2 == ANALOG_READ2_ERROR || filtered2 == ANALOG_READ2_AVG_ERROR ||
      filtered2f == (float)ANALOG_READ2_AVG_ERROR ||
      currentmA == (float)ANALOG_READ2_ERROR) {
    Serial.println("4-20mA input error");
    delay(500);
    return;
  }
 
  int32_t percent = Iscale((int32_t)raw2, 0, 32767, 0, 100);
 
  IanalogWrite2(0, 0, 100, 50);       // 50% → 약 12mA
  IanalogWrite2f(1, 0.0F, 10.0F, 2.5F); // 25% → 약 8mA
 
  Serial.print("4-20mA raw: ");
  Serial.println(raw2);
  Serial.print("4-20mA filtered: ");
  Serial.println(filtered2);
  Serial.print("4-20mA filtered float: ");
  Serial.println(filtered2f, 2);
  Serial.print("percent: ");
  Serial.println(percent);
  Serial.print("current: ");
  Serial.println(currentmA, 2);
 
  delay(500);
}

주의사항

채널, 입력 모듈의 전압·전류 선택과 딥스위치 설정을 먼저 확인하십시오. 평균 횟수를 늘리면 값은 안정되지만 응답이 느려집니다.

← 내장 명령어 리스트

commands/reference/analogread2avg.1788260661.txt.gz · 마지막으로 수정됨: 저자 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki