My map values are going over their highs and lows!

I’m mapping two ultrasonic sensor readings to PWM (255). The raw sensor reading is between 0 and approximately 15, which are in centimeters. After the readings are taken, they are subtracted to get a PWM value. My problem is that they are reading over the highest limit of my map! Code is below…

#include <NewPing.h>

#define TRIGGER_PINL  3
#define ECHO_PINL     2

#define TRIGGER_PINC  5
#define ECHO_PINC     4

#define TRIGGER_PINR  8
#define ECHO_PINR     7

#define MAX_DISTANCE 500        // centimeters

NewPing sensorL(TRIGGER_PINL, ECHO_PINL, MAX_DISTANCE);
NewPing sensorC(TRIGGER_PINC, ECHO_PINC, MAX_DISTANCE);
NewPing sensorR(TRIGGER_PINR, ECHO_PINR, MAX_DISTANCE);

const int numReadings = 2;

long readingsL[numReadings];      // the readings from the ping sensors
long readingsR[numReadings]; 
long readingsC[numReadings]; 
int index = 0;                    // the index of the current reading
long totalL = 0;                  // the running total for each sensor
long totalR = 0;
long totalC = 0;
long averageL = 0;                // the average from all three sensors
long averageR = 0; 
long averageC = 0;
int LavCm;                        // average from ping sensors in centimeters
int RavCm;
int CavCm;
int right = 9; //this line will be changed to digital pin 12
int left = 10;//this line will be changed to digital pin 13


void setup()
{
  // initialize serial communication with computer:
  Serial.begin(9600);   
  // motor negative wires get a low
  analogWrite(6, 0);
  analogWrite(11, 0);
  
  // initialize all the readings to 0: 
    for (int thisReadingL = 0; thisReadingL < numReadings; thisReadingL++)
      readingsL[thisReadingL] = 0;          
    for (int thisReadingR = 0; thisReadingR < numReadings; thisReadingR++)
      readingsR[thisReadingR] = 0;  
    for (int thisReadingC = 0; thisReadingC < numReadings; thisReadingC++)
      readingsC[thisReadingC] = 0;
  
    
}

void loop() {
  index = 0;
  for(index=0; index < numReadings; index++){
    // subtract the last reading:
    totalL = totalL - readingsL[index]; 
    totalR = totalR - readingsR[index];
    totalC = totalC - readingsC[index];
    
    
    // read from the sensor:  
    readingsL[index] = sensorL.ping(); 
    readingsR[index] = sensorR.ping(); 
    readingsC[index] = sensorC.ping(); 
    
      
    // add the reading to the total:
    totalL = totalL + readingsL[index];
    totalR = totalR + readingsR[index];
    totalC = totalC + readingsC[index];
  

    // advance to the next position in the array:
  }

  // if we're at the end of the array...                         


  // calculate the average:
  averageL = totalL / numReadings;
  averageR = totalR / numReadings;
  averageC = totalC / numReadings;


  // average in centimeters
  LavCm = averageL / 148;  //converts microseconds to centimeters
  RavCm = averageR / 148;  //converts microseconds to centimeters
  CavCm = averageC / 148;  //converts microseconds to centimeters
  LavCm = constrain(LavCm, 1, 200); //will not read more than 122 centimeters ( 1/3 of the max constraint)  
  RavCm = constrain(RavCm, 1, 200); //will not read more than 122 centimeters
  CavCm = constrain(CavCm, 1, 255); //will not read more than 366 centimeters (max constraint)
  int RavCmBigger = averageR / 100;
  int LavCmBigger = averageL / 100;
  int CavCmBigger = averageC / 100;
  
  // use to test sensor readings
  Serial.print("left reading: ");  //debugging information that displays left sensor average in centimeters
  Serial.println(LavCm);
  Serial.print("center reading: ");  //debugging information that displays center sensor average in centimeters
  Serial.println(CavCm);
  Serial.print("right reading: ");  //debugging information that displays right sensor average in centimeters
  Serial.println( RavCm);
  Serial.println(" ");
//  
  
  // delay between reads for stability . . 8ms == 0 .008 seconds
  //delay(1);
  delay(500);
 int Lx = LavCm - RavCm;
 int Rx = RavCm - LavCm;
 Lx = map((Lx), 0, 50, 255, 0);
 Rx = map((Rx), 0, 50, 255, 0);
 Serial.println(Rx);
 Serial.println(Lx);
 Serial.println(" ");
 analogWrite(right, Lx);
 analogWrite(left, Rx);
 
}

If the output is outside the range then the input is as well. Map does not constrain its output. Use constrain function if you want to clamp the output.

The constrain function was my first thought and I used this:

if(Lx < 0){
    Lx=0;
  }
  if(Rx < 0){
    Rx=0;
  }
  if(Lx > 10){
    Lx=10;
  }
  if(Rx > 10){
    Rx=10;
  }

as I was getting replies. Now my limits are not going over at all!

  LavCm = constrain(LavCm, 1, 200); //will not read more than 122 centimeters ( 1/3 of the max constraint)  
  RavCm = constrain(RavCm, 1, 200); //will not read more than 122 centimeters
  CavCm = constrain(CavCm, 1, 255); //will not read more than 366 centimeters (max constraint)

Since the constrain values have no relationship to the comments, the comments look pretty stupid.

Your code for averaging the sensor readings is pretty much useless. There is no reason to be using arrays, since you iterate through the loop, replacing, and then ignoring, each value in the array.

there exist constrain

lx = constrain(lx, 0, 10);
rx = constrain(rx, 0, 10);

constrain() is slightly more efficient as it skips the second compare if the first is true.

if (lx < 0) lx = 0;
else if (lx > 10) lx = 10;