how to stabilize the value??

the value not stable and sometimes got too high value...anybody help :frowning:

void loop()
{
digitalWrite(trigPin1,LOW);//ultrasonic
delayMicroseconds(1);
digitalWrite(trigPin1,HIGH);
delayMicroseconds(1);
digitalWrite(trigPin1,LOW);
digitalRead(trigPin1);
duration = pulseIn(8,HIGH);
cm = (duration/58.2); //change to cm

Serial.print(reading);
Serial.print(" ");
Serial.println(cm);
lcd.setCursor(0,0); //set the cursor to column 0 and line 1
lcd.print("Set(cm)=");
lcd.print(reading);
lcd.setCursor(0,1);
lcd.print("Changes(cm)=");
lcd.print(cm);
delay(100);

val = digitalRead(button);
readingSto = cm;
rst();

anybody help

Help us to help you by posting your whole program, using code tags when you do, and describing what hardware you are using.

Follow the advice given in read this before posting a programming question

Which one is too high? You have “val” and “cm” as the two main variables you are reading.

How much is too high? Please use a specific number.

My sensor which is ultrasonic HC-SR04…
this coding will function through set value.(Button). thus, the set value is ok but the changes value sometimes reach to 100++ and 3000++… is it need a kalman?

#define button 11
#define led 10
#define led2 12

#include<LiquidCrystal.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
const int trigPin1=9;
const int echoPin1=8;

double distance,cm,inches,duration;

int reading =10;
int val, readingSto;

void setup()
{
lcd.begin(16,2);
lcd.setCursor(0,0);
Serial.begin(9600);
pinMode(trigPin1,OUTPUT);
pinMode(echoPin1,INPUT);
pinMode(button,INPUT);
pinMode(led,OUTPUT);
pinMode(led2,OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
//lcd.print(“Ref. Value”);
}

void loop()
{
digitalWrite(trigPin1,LOW);//ultrasonic
delayMicroseconds(1);
digitalWrite(trigPin1,HIGH);
delayMicroseconds(1);
digitalWrite(trigPin1,LOW);
digitalRead(trigPin1);
duration = pulseIn(8,HIGH);
cm = (duration/58.2); //change to cm
Serial.print(reading);
Serial.print(" ");
Serial.println(cm);
lcd.setCursor(0,0); //set the cursor to column 0 and line 1
//lcd.print(reading);
lcd.print(“Set(cm)=”);
lcd.print(reading);
//lcd.print(“cm”);
lcd.setCursor(0,1);
lcd.print(“Changes(cm)=”);
lcd.print(cm);
//lcd.print(“cm”);
delay(100);

val = digitalRead(button);
readingSto = cm;
rst();
cm = cm-5;
if(cm >reading)
{
digitalWrite(led, HIGH);

}
else
{

digitalWrite(led, LOW);
}
/*
if(cm >=100)
{
digitalWrite(led, LOW);
}*/

//}

if(reading >cm )
{
digitalWrite(led2, HIGH);
}
else
{
digitalWrite(led2, LOW);
}

delay(500);
}
void rst()
{
if(val == 0 )
{
digitalWrite(LED_BUILTIN, HIGH);
reading = readingSto;
}
else
{
digitalWrite(LED_BUILTIN, LOW);
reading = reading;
}
}

    reading = reading;

How useful. I wonder why more people don't do that.

:slight_smile: :frowning: .....but i don't know to stabilize

Your code looked wrong at first glance for the delay on trigger.

Then I found these notes for a working example.
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse: (They used 5 microseconds)

Your code just uses 1 microsecond delays, where did you come up with that?

Trying to stabilize the value starts with making sure your using the sensor correctly first.