What is wrong with the following code? I have a US sensor attached and read the pulsewidth which I convert to inches (using the given 147 uS/inch conversion). If the sensor detects an object within a range, eg 10-24 inches for test 1, and 24-50 inches for test 2, it prints to monitor and LCD and lights up an LED. All this is still under development so there is still much to do.
However...when it gets to the 3rd condition where inches is between 200 and 350, it does not work. Nothing gets printed. I checked, with a scope the pulsewidth and it is right on when the sensor is in "free space", the max 301 inches or 44 mS pulsewidth. So I know the sensor is picking up the value, but my "if" statement is not making the decision. I've tried moving the "if" around and even put the long range test (the one that doesn't work) up higher...still doesn't work. I've tried Case statements and comparison statements (eg if ((12<= inches) && (inches <=30))) and that works for the first TWO, but not the THIRD. I cannot get this code to respond to a value >200 inches
pulseW = pulseIn(pwPin, HIGH); //get pulse duration
int inches = pulseW / scaleFactor;
if (inRange(inches, 10, 24)) //lab test value
{ digitalWrite (ledCycle, ON); //BLUE
lcd.setCursor(0, 2);
lcd.print("NEAR LANE ");
delay(500);
digitalWrite (ledCycle, OFF);
Serial.print ("Near Lane - inches= ");//for testing
Serial.println (inches);
}
else if(inRange(inches, 24, 50)); //lab test value
{ digitalWrite (ledSUV, ON); //GREEN
lcd.setCursor(0, 2);
lcd.print("FAR LANE ");
delay(500);//for test only
digitalWrite (ledSUV, OFF);
Serial.print ("Far Lane - inches= ");//for testing
Serial.println (inches);
}
if(inRange(inches, 200, 350))//max is 301 inches
{ digitalWrite (ledBus, ON); //RED
lcd.setCursor(0, 2);
lcd.print("NO TRAFFIC ");
delay(500);
digitalWrite (ledBus, OFF);
Serial.print ("No Traffic - reading= ");//for testing
Serial.println (inches);}