0
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 语句。

4

1 回答 1

5
else if (ledlevel >= 192)

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

else if (ledLevel >= 192)
于 2012-12-10T01:57:43.080 回答