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
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
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.
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;
}
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;
}