Ping sensor and LED lightbox

Hi Paul

Here is all the code

What I am trying to do is control when a led lightbox comes on and goes off. I have constrained it to a window of between 20cm to 100cm.

Inside the window the Led will fade up to it's max value once stepped outside the window the led lightbox will fade back to 0.

The leds where flickering which is the reason I thought it would help if I averaged the readings.

Hope the this information helps

/*

Smoothing

*/

// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 60;

int readings[numReadings]; // Number of readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int cm = 0; // the average value in cm
long brightness = 0;

const int pingPin = 7; // PING input connected to pin 7
int LED = 3; // LED is connected to pin 3

void setup()
{

Serial.begin(9600); // initialize serial communication with computer:

for (int thisReading = 0; thisReading < numReadings; thisReading++) // initialize all the readings to 0:
readings[thisReading] = 0;
}

void loop() {

// establish variables for duration of the ping,
// and the distance result in centimeters:
long duration,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
pinMode(pingPin, INPUT); // pulse whose duration is the time (in microseconds) from the sending
duration = pulseIn(pingPin, HIGH); // of the ping to the reception of its echo off of an object.

cm = microsecondsToCentimeters(duration); // convert the time into a distance

total= total - readings[index]; // subtract the last reading:

readings[index] = cm; // read from the sensor:

total= total + readings[index]; // add the reading to the total:

index = index + 1; // advance to the next position in the array:

if (index >= numReadings) // if we're at the end of the array...
// ...wrap around to the beginning:
index = 0;

cm = total / numReadings; // calculate the average:

cm = total / numReadings;
rightness = 0;
//cm = constrain(cm,20,100);

if(cm <= 100)
{

brightness = brightness+1;
analogWrite(LED,brightness);
delay(10);

}

else

{

brightness = brightness-1;
analogWrite(LED,brightness);
delay(10);
}

Serial.print(cm);
Serial.print("cm");
Serial.println();

}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}