I am trying to make an LED blink when someone goes past an ultrasonic range finder. My code constantly blinks seemingly whether the if statement is true or not. Below is my void loop. Why would this be?
void loop() {
// establish variables for duration of the ping, and the distance result
// in inches and centimeters:
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH pulse
// whose duration is the time (in microseconds) from the sending of the ping
// to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
Serial.println(oldcm);
Serial.println(oldcm-cm);
if (oldcm-cm > 50);{
digitalWrite(9, HIGH);
Serial.println("BLINK");
delay(600);
digitalWrite(9, LOW);
}
oldcm=cm;
delay(100);
}