Try to average data from HC-SR04

Hi, i'm new here and i need some help to find out my problem.

I try to average data from my ultrasonic sensor, but i don't understand how to do it.
Please help me to finish it, thank you and this is my code for my ultrasonic sensor.

Project_ultrasonik_relay_otomatis_kernell_silo.ino (907 Bytes)

Same way you average anything - add up a number of samples, and divide by the number of samples.

Please remember to use code tags when posting code

but how to add the results from the sensor and average it?

AWOL:
Same way you average anything - add up a number of samples, and divide by the number of samples.

Please remember to use code tags when posting code

   // deklarasi pin sensor ultrasonic
  const int trigPin = 9;
  const int echoPin = 10;

// deklarasi variabel
  float waktu,jarak;

//deklarasi relay
  int relay=4;
  
void setup() {

  //mengatur fungsi sensor ultrasonik
  pinMode( trigPin,OUTPUT); 
  pinMode( echoPin,INPUT);
  Serial.begin(9600);

  //mengatur fungsi relay 
  pinMode(relay,OUTPUT);
}

void loop() {
   digitalWrite(trigPin, LOW);
   delayMicroseconds (2);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPin,LOW);

   waktu = pulseIn(echoPin,HIGH);
   jarak = waktu*0.034/2;

   Serial.print("jarak= ");
   Serial.println(jarak);
   delay(5000);

   //code untuk on/off relay
   if(jarak<=100){
    digitalWrite(relay,LOW);
   }
   if(jarak>=200){
    digitalWrite(relay,HIGH);
   }
   if(relay==LOW && jarak<200){
    digitalWrite(relay,LOW);
   }
}

The same way you add anything - with the '+' operator.

AWOL:
The same way you add anything - with the '+' operator.

sorry i don't get it, can you help me with the sample code?

jarak keep adding up a number of these.

AWOL:

jarak

keep adding up a number of these.

like this?

float jarak=100;

My usual minimum requirement for an addition requires the presence of the '+' operator, so no.

AWOL:
My usual minimum requirement for an addition requires the presence of the '+' operator, so no.

what do you mean about the presence of the '+' operator?
so how can i finish it?

Let me try another approach.

I read out a short list of numbers.

You write them down, and then tell me the average.

Could you do that?

Here are the numbers:. 5, 6, 5,7,5,3.

AWOL:
Let me try another approach.

I read out a short list of numbers.

You write them down, and then tell me the average.

Could you do that?

Here are the numbers:. 5, 6, 5,7,5,3.

this is the average 5,166666666666667

OK, so now apply that algorithm to your code.

AWOL:
OK, so now apply that algorithm to your code.

OK, i know about the algorithm how to count the average.
But, i don't know how to setting, assume every 300 data it will automatic average it.

Do you want a rolling average, so an updated average for every sample period, or do you want to wait for 300 samples, and just get one average every 300 sample periods?

AWOL:
Do you want a rolling average, so an updated average for every sample period, or do you want to wait for 300 samples, and just get one average every 300 sample periods?

That's it, i just want one average every 300 sample periods.

Simplest way is to set up a for loop.
Before the for loop, set a sum variable (a long, probably) to zero, then iterate through the for loop 300 times, summing the echo pulse durations into the long variable.
At the end, divide the sum by 300, and convert to the units of your choice.

Don't take readings too quickly - not more than about twenty or thirty a second.

Previous reading + new reading divided by 2.

Doing the loop thing:

SonicReading += UltraSonicSensorReading
SonicReading = SoncicReading / 2, does a continuous average

AWOL:
Before the for loop, set a sum variable (a long, probably) to zero, then iterate through the for loop 300 times,

like this?

long sample[100];

void loop(){
long i;
for(i=0;i<=99;i++){
sample[i]=digitalRead(10);
average=sample[i]/100;
delay (1000);
}
}




please help me if i'm wrong

AWOL:
Before the for loop, set a sum variable (a long, probably) to zero, then iterate through the for loop 300 times,
like this?

long sample[100];

void loop(){
long i;
for(i=0;i<=99;i++){
sample[i]=digitalRead(10);
average=sample[i]/100;
delay (1000);
}
}




please help me if i'm wrong

a) long sample[100]; I would not use a long, I'd use an int instead.

b) does this statement represent the math you want?

for(i=0;i<=99;i++)
{
sample[i]=digitalRead(10); <<<<----
average=sample[i]/100; <<<----
}

Take the numbers 5,6, and 7 and apply that math to those numbers. I use just one box to represent numbers going into the same cell as well as represent what you have coded.

box1 = 5
box1 = box1/100

box1 = 6
box1 = box1/100

box1 = 7
box1= box1/100

Does that look like averaging to you?

[quote author=Idahowalker link=msg=4151515 date=1556289923]

Take the numbers 5,6, and 7 and apply that math to those numbers. I use just one box to represent numbers going into the same cell as well as represent what you have coded.

box1 = 5
box1 = box1/100

box1 = 6
box1 = box1/100

box1 = 7
box1= box1/100

Does that look like averaging to you?
[/quote]

i'm try to compile data from HC-SR04 until 50/100 data and average it, and i don't know how to compile the data and average it. Not spesific numbering like that you give to me.