Michael
Michael

Reputation: 905

arduino else if statement error

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);
}

This is my code for my arduino, it turns on one led when someone starts walking towards the ultrasonic sensor and when they get 3/4 of the way it turns on the second led. When I try to compile it this comes up "'ledLevel' was not declared in this scope," and highlights the else if statement.

Upvotes: 0

Views: 2476

Answers (1)

Lazy Ninja
Lazy Ninja

Reputation: 22537

else if (ledlevel >= 192)

ledLevel instead of ledlevel( L is upper case )
it should be

else if (ledLevel >= 192)

Upvotes: 5

Related Questions