Information has to be somewhere and I believe it is in the voltage. I have read something, that as the pulse travels further the level on echo gets bigger. And my test showed exactly the same, as long as I didn't started to test it while moving it gets some kind of interruptions.
Small introduction to my code: I`m making wall follower with PID. Ignore PID variables and other stuff. I just want to deal with the sensors.
MAIN:
#include <AFMotor.h>
#include <PID_v1.h>
#include "const.h"
#include "debug.h"
#include "drive.h"
#include <NewPing.h>
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
NewPing sonar1(TRIGGER1, ECHO1, MAX_DISTANCE);
NewPing sonar2(TRIGGER2, ECHO2, MAX_DISTANCE);
// void counter()
// {
//Update count
//Â pulses++;
// }
void setup()
{
 Serial.begin(9600);
 // pinMode(encoder_pin, INPUT);
 // attachInterrupt(0, counter, FALLING); //Interrupt 0 is digital pin 2 , Triggers on Falling Edge (change from HIGH to LOW)
 // pulses = 0;
 // rpm = 0;
 // timeold = 0;
 Setpoint = 160;        //cm
 myPID.SetMode(AUTOMATIC);
 myPID.SetSampleTime(150);   //ms
 myPID.SetControllerDirection(DIRECT);
 myPID.SetTunings(0.8, 0, 0);      //Kp , Kd, Ki
 myPID.SetOutputLimits(-30, 30);
}
void loop()
{
 sonar();Â
 myPID.Compute();
 drive();
 debugOutput(); // prints debugging messages to the serial console
 //countrpm();
}
void Compute()
{
 if (!inAuto) return;
 unsigned long now = millis();
  int timeChange = (now - lastTime);
 if (timeChange >= SampleTime)
 {
  /*Compute all the working error variables*/
  double error = Setpoint - Input;
  ITerm += (ki * error);
  if (ITerm > outMax) ITerm = outMax;
  else if (ITerm < outMin) ITerm = outMin;
  double dInput = (Input - lastInput);
  /*Compute PID Output*/
  Output = kp * error + ITerm - kd * dInput;
  if (Output > outMax) Output = outMax;
  else if (Output < outMin) Output = outMin;
  /*Remember some variables for next time*/
  lastInput = Input;
  lastTime = now;
 }
}
void sonar()
{
  Front = sonar1.ping_cm()*10;
  Serial.print("Front: ");
  Serial.print(Front);
  Serial.println("mm");
  delay(100);
  Input = sonar2.ping_cm()*10;
  Serial.print("Input: ");
  Serial.print(Input);
  Serial.println("mm");
  delay(100);
}
//void countrpm()
//{
// if (millis() - timeold >= 1000)
//Â {
//Don't process interrupts during calculations
//Â Â Â detachInterrupt(0);
//Â Â rpm = (60 * 1000 / pulsesperturn )/ (millis() - timeold)* pulses;
//Â Â timeold = millis();
//Â pulses = 0;
//Restart the interrupt processing
//Â attachInterrupt(0, counter, FALLING);
//Â }
// }
void SetTunings(double Kp, double Ki, double Kd)
{
 if (Kp < 0 || Ki < 0 || Kd < 0) return;
 double SampleTimeInSec = ((double)SampleTime) / 1000;
 kp = Kp;
 ki = Ki * SampleTimeInSec;
 kd = Kd / SampleTimeInSec;
 if (controllerDirection == REVERSE)
 {
  kp = (0 - kp);
  ki = (0 - ki);
  kd = (0 - kd);
 }
}
void SetSampleTime(int NewSampleTime)
{
 if (NewSampleTime > 0)
 {
  double ratio = (double)NewSampleTime / (double)SampleTime;
  ki *= ratio;
  kd /= ratio;
  SampleTime = (unsigned long)NewSampleTime;
 }
}
void SetOutputLimits(double Min, double Max)
{
 if (Min > Max) return;
 outMin = Min;
 outMax = Max;
 if (Output > outMax) Output = outMax;
 else if (Output < outMin) Output = outMin;
 if (ITerm > outMax) ITerm = outMax;
 else if (ITerm < outMin) ITerm = outMin;
}
void SetMode(int Mode)
{
 bool newAuto = (Mode == AUTOMATIC);
 if (newAuto == !inAuto)
 { /*we just went from manual to auto*/
  Initialize();
 }
 inAuto = newAuto;
}
void Initialize()
{
 lastInput = Input;
 ITerm = Output;
 if (ITerm > outMax) ITerm = outMax;
 else if (ITerm < outMin) ITerm = outMin;
}
void SetControllerDirection(int Direction)
{
 controllerDirection = Direction;
}
variables:
/*working variables*/
unsigned long lastTime;
double Input, Output, Setpoint, Front;
double ITerm, lastInput;
double Kp, Ki, Kd;
double kp, ki, kd;
double outMin, outMax;
int SampleTime;
bool inAuto = false;
#define MANUAL 0
#define AUTOMATIC 1
#define DIRECT 0
#define REVERSE 1
int controllerDirection = DIRECT;
//Motor set
AF_DCMotor motor1(1,MOTOR12_8KHZ); // create motor #1, 1KHz pwm
AF_DCMotor motor2(2,MOTOR12_8KHZ); // create motor #2, 1KHz pwm
int Lmotor;
int Rmotor;
const int motorNeutral = 60;
int speedmax = 255;
//sensor
#define TRIGGER1Â A0
#define ECHO1Â Â A1
#define TRIGGER2Â A2
#define ECHO2Â Â A3
#define MAX_DISTANCE 50000
///////encoder
//int encoder_pin = 2; // pulse output from the module
//unsigned int rpm; // rpm reading
//volatile byte pulses; // number of pulses
//unsigned long timeold;
// number of pulses per revolution
// based on your encoder disc
//unsigned int pulsesperturn = 20;
debugg:
void debugOutput()
{
 Serial.print("Lmotor: ");
 Serial.print(Lmotor);
 Serial.println();
 Serial.print("Rmotor: ");
 Serial.print(Rmotor);
 Serial.println();
 Serial.print("Output: ");
 Serial.print(Output);
 Serial.println();
//Â Serial.print("RPM = ");
//Serial.println(rpm,DEC);
//Â Serial.println();
Â
}
Drive:
void drive()
{
 if (Front > 30 && Front > 160) //160mm
 {
 Lmotor = motorNeutral - Output;
 Rmotor = motorNeutral + Output;
 }
 if (Front > 30 && Front < 160)
 {
  Lmotor = 0;
  Rmotor = speedmax;
 }
 // limit rightMotorSpeed
 if (Rmotor > 100)
  Rmotor = 100;
 else if (Rmotor < 0)
  Rmotor = 0;
 // limit rightMotorSpeed
 if (Lmotor > 100)
  Lmotor = 100;
 else if (Lmotor < 0)
  Lmotor = 0;
 motor1.run(FORWARD);
 motor2.run(FORWARD);
 motor1.setSpeed(Lmotor);
 motor2.setSpeed(Rmotor);
}