CodeWriter
I have been doing some other work but am back for a bit. I was having difficulty getting the Uno to reading again so I reloaded the program from the instructable by Jsvestor. It runs again and the float is not so bad now as it is only moving about 4 cm as it sets there running. It is saying it is 178 cm to the ceiling from my desk. If I move my hand in front of the sensor it reads the difference ok. earlier it was reading any where from 170 to 186 on its whim. That was why I wanted to know how to do a smoothing on the answer.
I still want to know how to wright one simply. I think it should not matter if you needed to do a smoothing from 10 or a 1000 readings if the code was the same. Just change the number of readings to smooth out. Maybe I am just thinking too simplistic for the answer to make sense. If you average 10 readings it should make the changes less and smooth out a curve. I kinda expected to find out it was a standard function even. I will endever to wright one.
Booradley60
I do not understand what your code is doing. It looks to me like your only using 2 readings so I must not be able to follow what you wrote. I will study your code more later so I can try to understand it better.
This is the code of jsvestor that I am using. I hope it is not wrong to post it here. It is from his instructable.
code
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at:
http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by
http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
/code
you can change the readout to be in inches if you change the distance calculation to use 72 or 76 I forget which and change the last print line to (in) instead of (cm)