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