Help ultra sonic sensor

Hello everyone
I would like to ask you for a hand in solving my problem with this code which I will attach at the end.
In practice there is a problem with the sound sensor, the classic model HC-SR04 3.5-5v.
The code seems to me well written, in practice he should measure the distance from a surface and when the height of the surface is lowered below a certain value, the sensor must make the input and turn off a controlled pump and motor by relay.
The problem lies in the fact that the sensor turns off everything as soon as it notices even a slight change in height.
We don't know how to do it ..
p.s. The sensor is placed face down, so it should do this ("if the distance is greater than 19 or less than 5 it blocks everything, otherwise it continues to work .. so the sensor must always work)
The part about the sensor is after setup if I'm not mistaken
Thank you in advance
<
`#include <IRremote.h>
#include <NewPing.h>
#include<LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

float value = 0;
float rev = 0;
int rpm;
int oldtime = 0;
int time;

void isr() //interrupt service routine
{
rev++;
}

int pinIRremote = 5;
int pinMotore = 4;
int pinPompa = 3;
int pinTrigger = 13;
int pinEcho = 6;

IRrecv receiver(pinIRremote);
decode_results results;
int stateMotore = LOW;
int statePompa = LOW;

// Sensore ultrasuoni
NewPing sonar (pinTrigger, pinEcho, 250);

void setup()
{
pinMode(pinMotore, OUTPUT);
pinMode(pinPompa, OUTPUT);
pinMode(pinTrigger, OUTPUT);
pinMode(pinEcho, INPUT);
receiver.enableIRIn();

lcd.begin(16, 2);
attachInterrupt(0, isr, RISING);
Serial.begin(9600);
Serial.print(rpm);

}

bool ultrasuoni() //bool è "si o no"
{
// calcolo del tempo attraverso il pin di echo
lcd.setCursor (0, 0);
int sonarDistance = sonar.ping_cm();

// dopo 38ms è fuori dalla portata del sensore
if ( sonarDistance > 23 ) {
lcd.println("PERICOLO POMPA ");
return false;
}

if ( sonarDistance <= 4 ) {
lcd.println("PERICOLO SENSORE ");
return true;
}

else {
lcd.print("distanza: ");
lcd.print(sonarDistance);
lcd.println(" cm ");
return true;
}
}

void loop() {

if (receiver.decode(&results))
{
Serial.println(results.value);
receiver.resume();
if (results.value == 16724175) //tasto1
{
stateMotore = !stateMotore;
digitalWrite(pinMotore, stateMotore);

  if (digitalRead(pinMotore) == HIGH)
  {
    lcd.begin (16, 2);
    lcd.setCursor (0, 0);
    lcd.print ("MOTORE ACCESO");
    delay (2000);
    lcd.clear ();

    tachimetroMotore();
    delay (500);
    lcd.clear ();

  }

  else if (digitalRead(pinMotore) == LOW)
  {
    lcd.begin (16, 2);
    lcd.setCursor (0, 0);
    lcd.print ("MOTORE SPENTO");
    delay (2000);
    lcd.clear ();
  }
}

Serial.println(results.value);
receiver.resume();
if (results.value == 16718055) //tasto2
{
  statePompa = !statePompa;
  digitalWrite(pinPompa, statePompa);

  if (digitalRead(pinPompa) == HIGH)
  {
    lcd.begin (16, 2);
    lcd.setCursor (0, 0);
    lcd.print ("POMPA ACCESA");
    delay (1000);
    lcd.clear ();

    tachimetroPompa();
    delay (500);
    lcd.clear ();

  }
  else if (digitalRead(pinPompa) == LOW)
  {
    lcd.begin (16, 2);
    lcd.setCursor (0, 0);
    lcd.print ("POMPA SPENTA");
    delay (2000);
    lcd.clear ();
  }
}

}

if (receiver.decode(&results))
{
Serial.println(results.value);
receiver.resume();
if (results.value == 16716015) //tasto4
{

  delay(1000);                                          //codice tachimetro
  detachInterrupt(0); //detaches the interrupt
  time = millis() - oldtime; //finds the time
  rpm = (rev / time) * 60000 / 1; //calculates rpm for blades
  oldtime = millis(); //saves the current time
  rev = 0;
  lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("TACHIMETRO");
  lcd.setCursor(4, 1);
  lcd.print( rpm);
  lcd.print(" RPM");
  lcd.print(" ");
  attachInterrupt(0, isr, RISING);


  while (rpm != 0)
  {
    delay(1000);                                          //codice tachimetro
    detachInterrupt(0); //detaches the interrupt
    time = millis() - oldtime; //finds the time
    rpm = (rev / time) * 60000 / 1; //calculates rpm for blades
    oldtime = millis(); //saves the current time
    rev = 0;
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("TACHIMETRO");
    lcd.setCursor(4, 1);
    lcd.print( rpm);
    lcd.print(" RPM");
    lcd.print(" ");
    attachInterrupt(0, isr, RISING);
  }
}

}

if (receiver.decode(&results))
{
Serial.println(results.value);
receiver.resume();
if (results.value == 16726215) //tasto5
{
ultrasuoni();
}
}

bool canContinue = ultrasuoni(); // La funzione ultrasuoni() mi avverte in caso di pericolo ritornandomi false o true (no o sì)
if (!canContinue) { // Se il sensore mi avverte che non c'è acqua, spengo pompa e motore
digitalWrite(pinMotore, LOW);
digitalWrite(pinPompa, LOW);
lcd.begin (16, 2);
lcd.setCursor (0, 0);
lcd.println("PERICOLO POMPA ");
delay (3000);
lcd.clear ();
lcd.print ("MOTORE E POMPA");
lcd.setCursor (4, 1);
lcd.print ("SPENTI");
delay (2000);
lcd.clear ();

}
}

void tachimetroMotore()
{
delay(1000); //codice tachimetro
detachInterrupt(0); //detaches the interrupt
time = millis() - oldtime; //finds the time
rpm = (rev / time) * 60000 / 1; //calculates rpm for blades
oldtime = millis(); //saves the current time
rev = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("TACHIMETRO");
lcd.setCursor(4, 1);
lcd.print( rpm);
lcd.print(" RPM");
lcd.print(" ");
attachInterrupt(0, isr, RISING);

while (rpm != 0)
{
delay(1000); //codice tachimetro
detachInterrupt(0); //detaches the interrupt
time = millis() - oldtime; //finds the time
rpm = (rev / time) * 60000 / 1; //calculates rpm for blades
oldtime = millis(); //saves the current time
rev = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("TACHIMETRO");
lcd.setCursor(4, 1);
lcd.print( rpm);
lcd.print(" RPM");
lcd.print(" ");
attachInterrupt(0, isr, RISING);

if (receiver.decode(&results))
{
  Serial.println(results.value);
  receiver.resume();
  if (results.value == 16718055) //tasto2
  {
    break;
  }
  tachimetroMotore();
}

delay(1000);
lcd.clear ();

}
}

void tachimetroPompa()
{
delay(1000); //codice tachimetro
detachInterrupt(0); //detaches the interrupt
time = millis() - oldtime; //finds the time
rpm = (rev / time) * 60000 / 1; //calculates rpm for blades
oldtime = millis(); //saves the current time
rev = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("TACHIMETRO");
lcd.setCursor(4, 1);
lcd.print( rpm);
lcd.print(" RPM");
lcd.print(" ");
attachInterrupt(0, isr, RISING);

while (rpm != 0)
{
delay(1000); //codice tachimetro
detachInterrupt(0); //detaches the interrupt
time = millis() - oldtime; //finds the time
rpm = (rev / time) * 60000 / 1; //calculates rpm for blades
oldtime = millis(); //saves the current time
rev = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("TACHIMETRO");
lcd.setCursor(4, 1);
lcd.print( rpm);
lcd.print(" RPM");
lcd.print(" ");
attachInterrupt(0, isr, RISING);

if (receiver.decode(&results))
{
  Serial.println(results.value);
  receiver.resume();
  if (results.value == 16743045) //tasto3
  {
    break;
  }
  tachimetroPompa();
}
delay(1000);
lcd.clear ();

}
} `>

#sensor #using-arduino #projects

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

volatile uint32_t rev;

(I feel I've already posted this).

Please remember to use code tags when posting code.

(I feel I've already posted this too)

it doesn't work man , i rewrote all in english (with google sry for error)

Ciao a tutt
Vorrei chiedervi una mano per risolvere il mio problema con questo codice che allegherò alla fine.
In pratica c'è un problema con il sensore ultrasuoni , il classico modello HC-SR04 3.5-5v .
Il codice mi sembra scritto bene, in pratica lui dovrebbe misurare la distanza da una superficie e nel momento in cui si abbassa l'altezza della superficie sotto un certo valore , il sensore deve madare l'imput e far spegnere una pompa e un motore controllati da relè.
Il problema sta nel fatto che il sensore spegne tutto non appena nota una anche minima variazione di altezza .
Non sappiamo come fare..
P.s. Il sensore si ritrova poisionato a faccia in giù , quindi dovrebbe fare questo (" se la distanza è maggiore di 19 o minore di 5 blocca tutto, altrimenti continua a lavorare.. quindi il sensore deve lavoreare sempre )
La parte riguardante il sensore sta dopo il setup se non sbaglio
Vi ringrazio in anticipo

`#include <IRremote.h>
#include <NewPing.h>
#include<LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

float value = 0;
float rev = 0;
int rpm;
int oldtime = 0;
int time;

void isr() //interrupt service routine
{
rev++;
}

int pinIRremote = 5;
int pinMotore = 4;
int pinPompa = 3;
int pinTrigger = 13;
int pinEcho = 6;

IRrecv receiver(pinIRremote);
decode_results results;
int stateMotore = LOW;
int statePompa = LOW;

// Sensore ultrasuoni
NewPing sonar (pinTrigger, pinEcho, 250);

void setup()
{
pinMode(pinMotore, OUTPUT);
pinMode(pinPompa, OUTPUT);
pinMode(pinTrigger, OUTPUT);
pinMode(pinEcho, INPUT);
receiver.enableIRIn();

lcd.begin(16, 2);
attachInterrupt(0, isr, RISING);
Serial.begin(9600);
Serial.print(rpm);

}

bool ultrasuoni() //bool è "si o no"
{
// calcolo del tempo attraverso il pin di echo
lcd.setCursor (0, 0);
int sonarDistance = sonar.ping_cm();

// dopo 38ms è fuori dalla portata del sensore
if ( sonarDistance > 23 ) {
lcd.println("PERICOLO POMPA ");
return false;
}

if ( sonarDistance <= 4 ) {
lcd.println("PERICOLO SENSORE ");
return true;
}

else {
lcd.print("distanza: ");
lcd.print(sonarDistance);
lcd.println(" cm ");
return true;
}
}

void loop() {

if (receiver.decode(&results))
{
Serial.println(results.value);
receiver.resume();
if (results.value == 16724175) //tasto1
{
stateMotore = !stateMotore;
digitalWrite(pinMotore, stateMotore);

  if (digitalRead(pinMotore) == HIGH)
  {
    lcd.begin (16, 2);
    lcd.setCursor (0, 0);
    lcd.print ("MOTORE ACCESO");
    delay (2000);
    lcd.clear ();

    tachimetroMotore();
    delay (500);
    lcd.clear ();

  }

  else if (digitalRead(pinMotore) == LOW)
  {
    lcd.begin (16, 2);
    lcd.setCursor (0, 0);
    lcd.print ("MOTORE SPENTO");
    delay (2000);
    lcd.clear ();
  }
}

Serial.println(results.value);
receiver.resume();
if (results.value == 16718055) //tasto2
{
  statePompa = !statePompa;
  digitalWrite(pinPompa, statePompa);

  if (digitalRead(pinPompa) == HIGH)
  {
    lcd.begin (16, 2);
    lcd.setCursor (0, 0);
    lcd.print ("POMPA ACCESA");
    delay (1000);
    lcd.clear ();

    tachimetroPompa();
    delay (500);
    lcd.clear ();

  }
  else if (digitalRead(pinPompa) == LOW)
  {
    lcd.begin (16, 2);
    lcd.setCursor (0, 0);
    lcd.print ("POMPA SPENTA");
    delay (2000);
    lcd.clear ();
  }
}

}

if (receiver.decode(&results))
{
Serial.println(results.value);
receiver.resume();
if (results.value == 16716015) //tasto4
{

  delay(1000);                                          //codice tachimetro
  detachInterrupt(0); //detaches the interrupt
  time = millis() - oldtime; //finds the time
  rpm = (rev / time) * 60000 / 1; //calculates rpm for blades
  oldtime = millis(); //saves the current time
  rev = 0;
  lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("TACHIMETRO");
  lcd.setCursor(4, 1);
  lcd.print( rpm);
  lcd.print(" RPM");
  lcd.print(" ");
  attachInterrupt(0, isr, RISING);


  while (rpm != 0)
  {
    delay(1000);                                          //codice tachimetro
    detachInterrupt(0); //detaches the interrupt
    time = millis() - oldtime; //finds the time
    rpm = (rev / time) * 60000 / 1; //calculates rpm for blades
    oldtime = millis(); //saves the current time
    rev = 0;
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("TACHIMETRO");
    lcd.setCursor(4, 1);
    lcd.print( rpm);
    lcd.print(" RPM");
    lcd.print(" ");
    attachInterrupt(0, isr, RISING);
  }
}

}

if (receiver.decode(&results))
{
Serial.println(results.value);
receiver.resume();
if (results.value == 16726215) //tasto5
{
ultrasuoni();
}
}

bool canContinue = ultrasuoni(); // La funzione ultrasuoni() mi avverte in caso di pericolo ritornandomi false o true (no o sì)
if (!canContinue) { // Se il sensore mi avverte che non c'è acqua, spengo pompa e motore
digitalWrite(pinMotore, LOW);
digitalWrite(pinPompa, LOW);
lcd.begin (16, 2);
lcd.setCursor (0, 0);
lcd.println("PERICOLO POMPA ");
delay (3000);
lcd.clear ();
lcd.print ("MOTORE E POMPA");
lcd.setCursor (4, 1);
lcd.print ("SPENTI");
delay (2000);
lcd.clear ();

}
}

void tachimetroMotore()
{
delay(1000); //codice tachimetro
detachInterrupt(0); //detaches the interrupt
time = millis() - oldtime; //finds the time
rpm = (rev / time) * 60000 / 1; //calculates rpm for blades
oldtime = millis(); //saves the current time
rev = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("TACHIMETRO");
lcd.setCursor(4, 1);
lcd.print( rpm);
lcd.print(" RPM");
lcd.print(" ");
attachInterrupt(0, isr, RISING);

while (rpm != 0)
{
delay(1000); //codice tachimetro
detachInterrupt(0); //detaches the interrupt
time = millis() - oldtime; //finds the time
rpm = (rev / time) * 60000 / 1; //calculates rpm for blades
oldtime = millis(); //saves the current time
rev = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("TACHIMETRO");
lcd.setCursor(4, 1);
lcd.print( rpm);
lcd.print(" RPM");
lcd.print(" ");
attachInterrupt(0, isr, RISING);

if (receiver.decode(&results))
{
  Serial.println(results.value);
  receiver.resume();
  if (results.value == 16718055) //tasto2
  {
    break;
  }
  tachimetroMotore();
}

delay(1000);
lcd.clear ();

}
}

void tachimetroPompa()
{
delay(1000); //codice tachimetro
detachInterrupt(0); //detaches the interrupt
time = millis() - oldtime; //finds the time
rpm = (rev / time) * 60000 / 1; //calculates rpm for blades
oldtime = millis(); //saves the current time
rev = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("TACHIMETRO");
lcd.setCursor(4, 1);
lcd.print( rpm);
lcd.print(" RPM");
lcd.print(" ");
attachInterrupt(0, isr, RISING);

while (rpm != 0)
{
delay(1000); //codice tachimetro
detachInterrupt(0); //detaches the interrupt
time = millis() - oldtime; //finds the time
rpm = (rev / time) * 60000 / 1; //calculates rpm for blades
oldtime = millis(); //saves the current time
rev = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("TACHIMETRO");
lcd.setCursor(4, 1);
lcd.print( rpm);
lcd.print(" RPM");
lcd.print(" ");
attachInterrupt(0, isr, RISING);

if (receiver.decode(&results))
{
  Serial.println(results.value);
  receiver.resume();
  if (results.value == 16743045) //tasto3
  {
    break;
  }
  tachimetroPompa();
}
delay(1000);
lcd.clear ();

}
} `

volatile uint32_t rev;

I'm sorry, I don't speak Italian

Please remember to use code tags when posting code

thx man
What do you mean with you're message ? i have to delete float rev = 0 and write volatile uint32_t rev

sorry i'm new and don t know all rules

@gabroooooo

TOPIC MERGED.

Could you take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

If you prefer to get help in Italian I can move this to the Italian section for you.