Smoothing Sonic Finder Project

Hello all!
I was wondering if I could get some help with smoothing out my readings. I was wondering how I could do that with the code I'm going to give. It works great, it sometimes just gets a little janky when reading inputs.

I'm using two sonic range finders to light a LED with the ranges they take in. I'm going to use 4 later, but at the moment I'm trying to get the smoothness to work with two of them.

If anybody could help that would be awesome!

//this is a modified version of testsonar
  //LED
const int LED = 9;

//leftsensor
const int trigpin1 = 3;
const int echopin1 = 2;

//rightsensor
const int trigpin2 = 5;
const int echopin2 = 4;

//defines variablles
long duration, distance;
int fadeValue,cm2, inputvalue;
long sensor1, sensor2;

void setup() {
  pinMode(trigpin1, OUTPUT);
  pinMode(echopin1, INPUT);
  pinMode(trigpin2, OUTPUT);
  pinMode(echopin2, INPUT);
  Serial.begin(9600);
}

void loop() {
  //alternating and setting the cm reading to each sensor
  sensor1 = SonarSensor(trigpin1, echopin1); 
  sensor2 = SonarSensor(trigpin2, echopin2); 

  if(sensor1 < 40 && sensor2 < 40)
  {
    inputvalue = (sensor1 + sensor2)/2;  
  }
  else if(sensor1 < 40)
  {
    inputvalue = sensor1; 
   }
  else if(sensor2 < 40)
  {
    inputvalue = sensor2; 
   }


  //this is where the cut off distance is determined on it's brightness. I.E. : The working rang
  //and the range at which it gets brighter
  if(sensor1 < 40 || sensor2 < 40)
  {
    //finding the LED brightness inversely
    cm2 = 40 - inputvalue;
    fadeValue = map(cm2,0,40,0,254);
    analogWrite(LED,fadeValue);
  }
  else
  {
   //if no sensors are picking up anything, it should shut off the light. Dim it to 1 actually.
   analogWrite(LED,1); 
  }

  //testing area to print values
  Serial.print("Sensor1: ");
  Serial.println(sensor1);
  Serial.print("Sensor2: ");
  Serial.println(sensor2);
//  Serial.print("Average: ");
//  Serial.println(inputvalue);
//  Serial.println(map(cm2,0,40,0,254));
//  Serial.println(fadeValue);
//  Serial.println("");

}

//functions
long SonarSensor(int trigPin,int echoPin)
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
//  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  //centimeters
  distance = (duration/2) / 29.1;
  return distance;  
}

I'd put a longer pause between each reading - you may be seeing returns from the first outgoing pulse being received by the second sensor.
I'd suggest no more than 20 readings per second total, ie no more than ten per second per sensor.
Also check your supply and decoupling

The standard way of averaging a jittery datastream is to take a running average..

eg

    ave2 += altitude;
    ave2 *= (1-1/aveLen);
    ave3 = ave2/(aveLen-1);

as you may guess this was for a altimeter .. aveLen is the number of samples over which you want to average. aveLen is an integer, everything else floats

regards

Allan

Thank you everyone!
I have done as some of you have said, but still working out on how to implement it. Question is what would be the supply and coupling? Not to sure on what that means. I'm a beginner if you must know. For the averaging that was given, I'm still trying to figure out a way to implement that into my code.

The simplest thing for you to implement would be the slower sampling rate, then if that doesn't work, you can start to worry about the other stuff.

Okay I have changed the delay in the bottom function. Is there anything else I should change? Do ultrasonic readers pick up one another?

//this is a modified version of testsonar
  //LED
const int LED = 9;

//leftsensor
const int trigpin1 = 3;
const int echopin1 = 2;

//rightsensor
const int trigpin2 = 5;
const int echopin2 = 4;

//defines variablles
long duration, distance;
int fadeValue,cm2;
int inputvalue = 0; 
long sensor1, sensor2;

void setup() {
  pinMode(trigpin1, OUTPUT);
  pinMode(echopin1, INPUT);
  pinMode(trigpin2, OUTPUT);
  pinMode(echopin2, INPUT);
  Serial.begin(9600);
}

void loop() {
  //alternating and setting the cm reading to each sensor
  sensor1 = SonarSensor(trigpin1, echopin1); 
  sensor2 = SonarSensor(trigpin2, echopin2); 

  if(sensor1 < 40 && sensor2 < 40)
  {
    inputvalue = (sensor1 + sensor2)/2;  
  }
  else if(sensor1 < 40)
  {
    inputvalue = sensor1; 
   }
  else if(sensor2 < 40)
  {
    inputvalue = sensor2; 
   }


  //this is where the cut off distance is determined on it's brightness. I.E. : The working rang
  //and the range at which it gets brighter
  if(sensor1 < 40 || sensor2 < 40)
  {
    //finding the LED brightness inversely
    cm2 = 40 - inputvalue;
    fadeValue = map(cm2,0,40,0,254);
    analogWrite(LED,fadeValue);
  }
  else
  {
   //if no sensors are picking up anything, it should shut off the light. Dim it to 1 actually.
   analogWrite(LED,1); 
  }

  //testing area to print values
  Serial.print("Sensor1: ");
  Serial.println(sensor1);
  Serial.print("Sensor2: ");
  Serial.println(sensor2);
  Serial.print("Average: ");
  Serial.println(inputvalue);
//  Serial.println(map(cm2,0,40,0,254));
//  Serial.println(fadeValue);
  Serial.println("");

}

//functions
long SonarSensor(int trigPin,int echoPin)
{
  digitalWrite(trigPin, LOW);
  //stay at 50 or 35?
  delay(50);
  digitalWrite(trigPin, HIGH);
  delay(50);
  digitalWrite(trigPin, LOW);
//  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  //centimeters
  distance = (duration/2) / 29.1;
  return distance;  
}

Okay I have changed the delay in the bottom function. I

Why?
All you've done is extend the trigger pulse, and possibly made your sensor blind to close returns.

Do ultrasonic readers pick up one another?

There's no coding to say "This ping is for sensor 1 only" - they're simply tuned to detect anything with a 40 kHz component.

If you want a pause, put a delay after each call to "SonarSensor"

Ahhh. I'm still learning, but I changed everything back. Yet, I put the delays after the function calls.
Here is the code:

//this is a modified version of testsonar
  //LED
const int LED = 9;

//leftsensor
const int trigpin1 = 3;
const int echopin1 = 2;

//rightsensor
const int trigpin2 = 5;
const int echopin2 = 4;

//defines variablles
long duration, distance;
int fadeValue,cm2, inputvalue;
long sensor1, sensor2;

void setup() {
  pinMode(trigpin1, OUTPUT);
  pinMode(echopin1, INPUT);
  pinMode(trigpin2, OUTPUT);
  pinMode(echopin2, INPUT);
  Serial.begin(9600);
}

void loop() {
  //alternating and setting the cm reading to each sensor
  sensor1 = SonarSensor(trigpin1, echopin1); 
  delay(50);
  sensor2 = SonarSensor(trigpin2, echopin2);
  delay(50); 

  if(sensor1 < 40 && sensor2 < 40)
  {
    inputvalue = (sensor1 + sensor2)/2;  
  }
  else if(sensor1 < 40)
  {
    inputvalue = sensor1; 
   }
  else if(sensor2 < 40)
  {
    inputvalue = sensor2; 
   }


  //this is where the cut off distance is determined on it's brightness. I.E. : The working rang
  //and the range at which it gets brighter
  if(sensor1 < 40 || sensor2 < 40)
  {
    //finding the LED brightness inversely
    cm2 = 40 - inputvalue;
    fadeValue = map(cm2,0,40,0,254);
    analogWrite(LED,fadeValue);
  }
  else
  {
   //if no sensors are picking up anything, it should shut off the light. Dim it to 1 actually.
   analogWrite(LED,1); 
  }

  //testing area to print values
  Serial.print("Sensor1: ");
  Serial.println(sensor1);
  Serial.print("Sensor2: ");
  Serial.println(sensor2);
  Serial.print("Average: ");
  Serial.println(inputvalue);
//  Serial.println(map(cm2,0,40,0,254));
//  Serial.println(fadeValue);
  Serial.println("");

}

//functions
long SonarSensor(int trigPin,int echoPin)
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(10);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
//  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  //centimeters
  distance = (duration/2) / 29.1;
  return distance;  
}

Also, here is a picture of my setup.