# Rangefinder how spikes

Hello all!
I’m trying out two sorts of code to light an LED according to how close they are to the sensors. It will eventually be using four sensors when I get testing with the two sensors. It starts to freak out around the point in which it triggers actually lighting up the LED. What I mean is the closest point to were it will fade the LED or turn it on. I was wondering if anybody could help me figure out how to get these spikes out of the code. Here are the two sets of code I’m using in separate testing cases.

Test code 1:

``````//this is a modified version of testsonar
//need to convert the inches he has given me to cm.
//48inches is the farthest he wants it to catch people which is 121 cm
//so change all the 40 to 121 cm

//LED
const int LED = 9;

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

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

//defines variablles
long duration, distance;
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;
}
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("");

}

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

Test code 2:

``````//this is a modified version of testsonar
//need to convert the inches he has given me to cm.
//48inches is the farthest he wants it to catch people which is 121 cm
//so change all the 40 to 121 cm
//having trouble with one of the sensors, check code

//LED
const int LED = 9;

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

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

//range variables
const int twentypercent = 40;
const int fortypercent = 20;
const int sixtypercent = 10;
const int eightypercent = 6;
const int onehundredpercent = 3;

//defines variablles
long duration, distance;
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;
}
else
{
inputvalue = 0;
}

//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
//switching from reading input value to sensors
if( sensor1 > twentypercent && sensor2 > twentypercent)
{
analogWrite(LED,1);
}
else if ( fortypercent < sensor1 <= twentypercent ||fortypercent < sensor2 <= twentypercent  )
{
cm2 = twentypercent - inputvalue;
}
else if ( sixtypercent < sensor1 <= fortypercent || sixtypercent < sensor2 <= fortypercent )
{
cm2 = fortypercent - inputvalue;

}
else if ( eightypercent < sensor1 <= sixtypercent ||eightypercent < sensor2 <= sixtypercent )
{
cm2 = sixtypercent - inputvalue;
}
else if ( onehundredpercent  < sensor1 <= eightypercent || onehundredpercent  < sensor2 <= eightypercent )
{
cm2 = eightypercent - inputvalue;
}
else if ( sensor1 <= onehundredpercent || sensor2 <= onehundredpercent)
{
cm2 = onehundredpercent - inputvalue;
}
//  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("");

}

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

If you could help out in anyway that would be awesome!
Thanks!
Also, below is an attachment to my setup.

Note: If the sonar doesn’t receive an echo it doesn’t produce an echo pulse. That cause pulseIn() to return zero after the default 1000 millisecond timeout period (you may want to make that shorter). Your code looks for a distance < 40 as being a close object but a distance of zero means nothing is in range. You may want to add checks for a zero distance and treat that specially.

``````sixtypercent < sensor1 <= fortypercent
``````

This is NOT how comparison operators work in the C++ programming language. You want:`(sixtypercent < sensor1 && sensor1 <= fortypercent)`

It looks like you are trying to do piecewise interpolation. There is a function called multiMap() that will do that using tables of input and output values. You can map distances directly to PWM values for the LED brightness. http://playground.arduino.cc/Main/MultiMap

Thank you so much! I'll be getting on to this as soon as possible. I'll try to update.