2012-12-10 174 views
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語句。arduino else if語句錯誤

回答

5
else if (ledlevel >= 192) 

ledLevel而不是ledlevel(L是大寫)
應該

else if (ledLevel >= 192) 
+0

哦,我的天哪!我覺得很愚蠢!非常感謝。 – Michaelslec

+0

適合所有人,大聲笑。請註冊並標記爲答案。 –