arduino else if语句错误

时间:2012-12-10 01:54:20

标签: 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。当我尝试编译它时,出现“'ledLevel'未在此范围内声明,”并突出显示else if语句。

1 个答案:

答案 0 :(得分:5)

else if (ledlevel >= 192)

ledLevel而不是ledlevel(L是大写)
它应该是

else if (ledLevel >= 192)