This is Cool!!
I was able to take the smoothing concept and run it on two ping sensors. The result is much more fluid changes in values and even when error numbers roll across the screen, the data still only moves slowly.
The down side, given that I am pulling 100 number samples, the response time can be as much 5 seconds.
I will play with that and see if I can't get some better things going. Or maybe I don't need that level of smooth.
The other thing that I did was to call the smoothing process a function so I can thread it easily into my void loop for when I need decisions to be made.
The code is below.
-------------------------------
// Two Ping Sensors with Smoothing
//by Philip Leete
//Define where the Ping Sensors are
int trigPin_R = 2, trigPin_L = 8;
int echoPin_R = 3, echoPin_L = 9;
int duration_R, duration_L;
int distance_R, distance_L;
//This sections is required for the smoothing
const int numReadings = 10;
int readings_R[numReadings];
int readings_L[numReadings]; // the readings from the analog input
int index_R = 0, index_L = 0; // the index of the current reading
int total_R = 0, total_L = 0; // the running total
int average_R = 0, average_L = 0; // the average of the light values
void setup() {
Serial.begin (9600);
pinMode(trigPin_L, OUTPUT);
pinMode(trigPin_R, OUTPUT);
pinMode(echoPin_L, INPUT);
pinMode(echoPin_R, INPUT);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings_R[thisReading] = 0; // initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings_L[thisReading] = 0;
}
void smooth_sensors(){
//Activate the Left Ping Sensor
digitalWrite(trigPin_L, LOW);
delayMicroseconds(2);
digitalWrite(trigPin_L, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin_L, LOW);
duration_L = pulseIn(echoPin_L, HIGH);
distance_L = (duration_L)/140; //conversion to inches
//Activate the Right Ping Sensor
digitalWrite(trigPin_R, LOW);
delayMicroseconds(2);
digitalWrite(trigPin_R, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin_R, LOW);
duration_R = pulseIn(echoPin_R, HIGH);
distance_R = (duration_R)/140; //conversion to inches
//This section is for smoothing
total_R= total_R - readings_R[index_R]; // subtract the last reading:
total_L= total_L - readings_L[index_L];
readings_R[index_R] = distance_R; // read from the sensor:
readings_L[index_L] = distance_L;
total_R= total_R + readings_R[index_R]; // add the reading to the total:
total_L= total_L + readings_L[index_L];
index_R = index_R + 1, index_L = index_R + 1; // advance to the next position in the array:
if (index_R >= numReadings) // if we're at the end of the array...
index_R = 0; // ...wrap around to the beginning
if (index_L >= numReadings)
index_L = 0;
average_R = total_R / numReadings; // calculate the average:
average_L = total_L / numReadings; // calculate the average:
//this sections prints out info to the serial monitor
Serial.print(average_L);
Serial.println(" Left Sensor Avgerage");
// Serial.print(distance_L);
// Serial.println(" Left Sensor Data");
Serial.print(average_R);
Serial.println(" Right Sensor Avgerage");
// Serial.print(distance_R);
// Serial.println(" Right Sensor Data");
delay(1); // delay in between reads for stability
}
void loop() {
smooth_sensors();
}
No comments:
Post a Comment