You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino中for循环调用函数报错及超声波检测功能优化咨询

问题解决:Arduino函数未声明错误

错误原因

在C++(Arduino基于C++)中,编译器从上到下解析代码,当loop()函数里调用checkDist()和blip()时,编译器还没读取到这两个函数的定义,因此报错'checkDist' was not declared in this scope。global关键字用于修饰变量,对函数无效。另外你的代码里loop()函数末尾缺少闭合的},这也是语法错误,必须补上。

两种解决方案

方案1:添加函数原型声明

在全局变量声明之后、setup()函数之前,添加要调用的函数的原型,告诉编译器这些函数的存在:

const int red1 = 4;
const int red2 = 5;
const int yellow1 = 6;
const int yellow2 = 7;
const int echo = 10;
const int trig = 9;
const int buzzer = 11;
int maxCmAway = 50;

float duration_us, distance_cm;

// 添加函数原型声明
int checkDist();
int blip();

void setup() {
  Serial.begin (9600);
  pinMode(red1, OUTPUT);
  pinMode(red2, OUTPUT); 
  pinMode(yellow1, OUTPUT);
  pinMode(yellow2, OUTPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
}

void loop() {
  digitalWrite(yellow1, LOW);
  digitalWrite(yellow2, LOW);
  digitalWrite(red1, HIGH);
  digitalWrite(red2, HIGH);
  for(int i=0; i<6; i++) {
    if (checkDist() > maxCmAway) {
      blip();
    } else {
      noTone(buzzer);
      delay(250);
    }
  }
  digitalWrite(red1, LOW);
  digitalWrite(red2, LOW);
  digitalWrite(yellow1, HIGH);
  digitalWrite(yellow2, HIGH);
  for(int i=0; i<6; i++) {
    if (checkDist() > maxCmAway) {
      blip();
    } else {
      noTone(buzzer);
      delay(250);
    }
  }
} // 补上loop()函数的闭合括号

int checkDist() {
    // generate 10-microsecond pulse to TRIG pin
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(echo, HIGH);

  // calculate the distance
  distance_cm = 0.017 * duration_us;

  // print the value to Serial Monitor
  if (distance_cm < maxCmAway) {
    Serial.print("distance: ");
    Serial.print(distance_cm);
    Serial.println(" cm");
    Serial.print("real: ");
    Serial.print(distance_cm);
    Serial.println(" cma");
  } else {
    Serial.println("distance: 0 cm");
    Serial.print("real: ");
    Serial.print(distance_cm);
    Serial.println(" cma");
  }

  return distance_cm;
}

int blip() {
    tone(buzzer, 800);
    delay(50);
    tone(buzzer, 600);
    delay(100);
    tone(buzzer, 800);
    delay(100);
    noTone(buzzer);
    return 1;
}

方案2:调整函数定义顺序

把checkDist()和blip()的定义移到loop()函数之前,让编译器先读取到函数定义:

const int red1 = 4;
const int red2 = 5;
const int yellow1 = 6;
const int yellow2 = 7;
const int echo = 10;
const int trig = 9;
const int buzzer = 11;
int maxCmAway = 50;

float duration_us, distance_cm;

int checkDist() {
    // generate 10-microsecond pulse to TRIG pin
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(echo, HIGH);

  // calculate the distance
  distance_cm = 0.017 * duration_us;

  // print the value to Serial Monitor
  if (distance_cm < maxCmAway) {
    Serial.print("distance: ");
    Serial.print(distance_cm);
    Serial.println(" cm");
    Serial.print("real: ");
    Serial.print(distance_cm);
    Serial.println(" cma");
  } else {
    Serial.println("distance: 0 cm");
    Serial.print("real: ");
    Serial.print(distance_cm);
    Serial.println(" cma");
  }

  return distance_cm;
}

int blip() {
    tone(buzzer, 800);
    delay(50);
    tone(buzzer, 600);
    delay(100);
    tone(buzzer, 800);
    delay(100);
    noTone(buzzer);
    return 1;
}

void setup() {
  Serial.begin (9600);
  pinMode(red1, OUTPUT);
  pinMode(red2, OUTPUT); 
  pinMode(yellow1, OUTPUT);
  pinMode(yellow2, OUTPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
}

void loop() {
  digitalWrite(yellow1, LOW);
  digitalWrite(yellow2, LOW);
  digitalWrite(red1, HIGH);
  digitalWrite(red2, HIGH);
  for(int i=0; i<6; i++) {
    if (checkDist() > maxCmAway) {
      blip();
    } else {
      noTone(buzzer);
      delay(250);
    }
  }
  digitalWrite(red1, LOW);
  digitalWrite(red2, LOW);
  digitalWrite(yellow1, HIGH);
  digitalWrite(yellow2, HIGH);
  for(int i=0; i<6; i++) {
    if (checkDist() > maxCmAway) {
      blip();
    } else {
      noTone(buzzer);
      delay(250);
    }
  }
} // 补上loop()函数的闭合括号

额外说明

针对你“LED闪烁一次后,超声波传感器能进行多次检测”的需求,当前代码中每个LED状态(红亮/黄亮)下的for循环会执行6次距离检测,已经符合多次检测的逻辑。如果需要调整检测次数,修改for循环的i<6中的数字即可。

内容的提问来源于stack exchange,提问作者helloworld123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 09:55:31