compiler-errors - arduino else if语句错误

标签 compiler-errors arduino scope if-statement

const int pingPin = 7;
const int ledPin = 11; 
const int ledPin2 = 10;
int ledLevel = 0; 
int ledLevel2 = 255;
int constraint = 0;

void setup() 
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{ 
  long duration, inches, cm;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  constraint = constrain(duration, 500, 8000);

  ledLevel = map(constraint, 500, 8000, 255, 0);

  if (ledLevel < 192) {
  analogWrite(ledPin, ledLevel);
  ledLevel2 = ledLevel2 - 255;
  } else if (ledlevel >= 192) {
    analogWrite(ledPin, ledLevel);
    analogWrite(ledPin2, ledLevel2);
  }

  Serial.println(duration);
  delay(100);
}

这是我为arduino设计的代码,当有人开始朝超声波传感器行走时,它打开了一个LED,而当他们获得3/4时,它打开了第二个LED。当我尝试对其进行编译时,出现“在此范围内未声明'ledLevel'”,并突出显示了else if语句。

最佳答案

else if (ledlevel >= 192)

ledLevel而不是ledlevel(L为大写)
它应该是
else if (ledLevel >= 192)

关于compiler-errors - arduino else if语句错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13793964/

相关文章:

visual-studio - 为什么我在 unit3d 上而不是在 Visual Studio 上出现此错误

python - 我收到此错误 --> TypeError : string indices must be integers

c++ - 数组模板和内置数组类型之间有区别吗?

c++ - 类构造函数声明了两次

c++ - 如何处理模板中的成员变量

Arduino 波形发生器

Arduino - 用 for 循环编写 int 数组不起作用

perl - 从 CGI 迁移到 mod_perl。了解我的、我们的、本地的

actionscript-3 - ActionScript 3.0 : Scope

Javascript - 全局变量和全局范围内的变量是否不同(在 jsfiddle 中)?