SEN 0352 problem
i am using arduino uno and trying to coonecting with this sensor , but it gives false reading , i have directly connected it with vcc to 5v , gnd to gnd and trig, echo to digital pins on arduino ., on the datasheet i saw it is compatible with arduino Leonardo , so isnt it be working with arduino uno ? , the code is : const int trigPin = 9;
const int echoPin = 10;
const int sampleSize = 5;
int readings[sampleSize];
int index = 0;
long total = 0;
int average = 0;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
for (int i = 0; i < sampleSize; i++) {
readings[i] = 0;
}
}
void loop() {
// Measure the distance
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.0344 / 2;
// Apply the moving average filter
total = total - readings[index];
readings[index] = distance;
total = total + readings[index];
index = (index + 1) % sampleSize;
average = total / sampleSize;
Serial.print("Measured Distance: ");
Serial.print(distance);
Serial.print(" cm. Average Distance: ");
Serial.println(average);
delay(1000);
}