This should be easy...but I just don't know why my WHILE commands in the program don't work. The US sensor is sensing all the time and when I point it close to something (using test numbers) , it should say "near lane", and point it far, the LCD should display NO TRAFFIC. It displays nothing. I put a Serial print statement in the WHILE but it DOES NOT PRINT the statement.
Yet, if I replace WHILE with IF, it works. (I want WHILE so that the display doesn't change "while" the sensor is still reporting within the range specified.) I've used WHILE in other sketches where it works okay, so is there a problem in my MAP function or my InRange function? (Like I said, works with IF).
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LcdBarGraphRobojax.h>
//byte lcdNumCols=20;//need for BarGraph library
//byte lcdLine=4;//need for BarGraph library
bool OFF = HIGH; //leds are common anode, current sink (LOW) turns them on.
bool ON = LOW;
//int pulseW;
int scaleFactor = 147; //scale Factor of MB 7067 is 58 uS/cm or 147 uS/inch
//float duration;
//float voltage;
//const int pwPin = 2;
// int anPin =A0;
//int count=0;
int inches;
int outMax=301;//max output of Maxbotix sensor is 301 inches
int sensorMax=696;//derived from actual value 3.4. 3.4/5*1024
const byte ledBlue = 8; //motorcycle LED
//const byte ledCar = 9; //car
const byte ledGreen = 10; //SUV
//const byte ledPickup = 11; //pickup
const byte ledRed = 12; //bus
byte ledPins[] = {8, 9, 10, 11, 12};
int countSouth=0;
int countNorth=0;
byte threshold=5;//threshold before a detection changes.
LiquidCrystal_I2C lcd(0x27, 20, 4);
/****************** Setup ********************/
void setup()
{ for ( byte x = 0; x < 5; x++ )//make 5 LED pins all outputs
{ pinMode( ledPins[x], OUTPUT );
digitalWrite(ledPins[x], OFF);}
Serial.begin(115200);
Wire.begin(); // start I2C
lcd.begin(20, 4);
lcd.backlight();
// pinMode(pwPin, INPUT);//not used at this time.
pinMode(A0, INPUT);
}
/****************Function inRange****************/
byte inRange (int val, int minimum, int maximum)
{ return ((minimum <= val) && (val <= maximum));}
/****************** Loop ********************/
void loop()
{
//pulseW = pulseIn(pwPin, HIGH); //get pulse duration
//int inVal = pulseW;
int inVal=analogRead(A0);
inches= map(inVal, 0, sensorMax, 0, outMax);//sensorMax=value in 10 bits, OutMax: max of sensor,301 inches
/****************Near Lane******************/
// if (inRange(inches, 120, 216))//10 to 18 feet roughly center of near lane
while (inRange(inches, 12, 21))//test value
{ digitalWrite (ledBlue, ON); //BLUE
countSouth++;
Serial.print("doing the while for Near Lane");
}
lcd.setCursor(0, 1);
lcd.print("NEAR LANE ");
delay(200);
digitalWrite (ledBlue, OFF);
// lcd.setCursor (12,1);
//lcd.print(countSouth);
/***************Far Lane******************/
// else if(inRange(inches, 240, 276))//20-23 feet roughly center of far lane
while (inRange(inches, 24, 40))//test value
{ digitalWrite (ledGreen, ON); //GREEN
countNorth++;
Serial.print("doing the while for Far Lane");
}
lcd.setCursor(0, 2);
lcd.print("FAR LANE ");
delay(200);
digitalWrite (ledGreen, OFF);
//lcd.setCursor (12,2);
//lcd.print(countNorth);
/******************No Traffic*******************/
// if(inches>288)//real value ? 24 feet
while (inches >48)//test value
{ digitalWrite (ledRed, ON); //RED
lcd.setCursor(0, 3);
lcd.print("NO TRAFFIC ");
// delay(500);
digitalWrite (ledRed, OFF);
}}

