Doppio sensore ultrasuoni con lcd

Buonasera a tutti.
Come già ho scritto nella presentazione mi sono da poco avvicinato ad Arduino e quindi inizio a trovare i primi nodi :).

Il mio progetto è piazzare arduino con due sensori ad ultrasuoni che mi rilevano l'altezza di liquido in due vasche e me lo riportano su un lcd 16x2.

Sembra funzionare tutto bene ma sul lcd nonmi arrivano i due risultati.
Sono partito con questo schema:

void setup (per lcd e 2 sensori)

void loop (vasca 1 risultato su lcd quindi vasca 2 risultato su lcd)

Ottengo: H Vasca 1 misura corretta cm
H Vasca 2 nessun risultato senza neanche cm

A questo punto ho copiato il loop vasca 1 dopo il loop della vaasca 2
con questo schema:

void setup (per lcd e 2 sensori)

void loop (vasca 2 risultato su lcd quindi vasca 1 risultato su lcd)

Ottengo: H Vasca 1 nessun risultato senza neanche cm
H Vasca 2 misura corretta cm.

Quindi mi viene il dubbio che il mio errore sia nella gestione del lcd.
Naturalmente nel modificare lo sketch non ho toccato alcun filo.
Ringrazio anticipatamente per la mano che potrete darmi e vi posto il mio sketch:

#include <NewPing.h>
#include   <LiquidCrystal.h> 
 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
int pingPin = 9;
int inPin = 8;
int pingPin1 =10;
int inPin1 = 7;
 
void setup(){
  //imposto il numero di colonne ed il numero di righe di lcd
  //usando il metodo lcd
  lcd.begin(16, 2);
  // Scrive il messaggio sul display.
  lcd.setCursor(0, 0);
  lcd.print("H Vasca 1");
  lcd.setCursor(0, 1);
  lcd.print("H Vasca 2");
}
 
void loop()
{
 //parte il 2
 
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration1, cm;
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin1, OUTPUT);
  digitalWrite(pingPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin1, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin1, LOW);
 
  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(inPin1, INPUT);
  duration1 = pulseIn(inPin1, HIGH);
 
  // convert the time into a distance
   cm = microsecondsToCentimeters1(duration1);
    lcd.setCursor(10, 1);
  lcd.print(cm);
  lcd.print(" cm");
  delay(100);
}
 
long microsecondsToCentimeters1(long microseconds1)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return (500 -(microseconds1 / 29 / 2));
  }
 
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, cm;
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
 
  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(inPin, INPUT);
  duration = pulseIn(inPin, HIGH);
 
  // convert the time into a distance
   cm = microsecondsToCentimeters(duration);
  lcd.setCursor(10, 0);
  lcd.print(cm);
  lcd.print(" cm");
  delay(0);
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return (500 -(microseconds / 29 / 2));  

}

vedo, vedo, vedo un Sensore ultrasuoni SRF04. :wink: :wink: :wink:

Visto che usi dei pin separati per il trigger e per la risposta non é necessario che definisci i Pin nel Loop. Metti i pinMode nel setup().

La lettura del primo sensore (vasca1) non é nel loop ma fuori percui non viene esguita.

Attenzione visto che immetti dei ultrasuoni in un vano chiuso puó essere che in una misura successiva ricevi l' eco di quella precedente e percui delle misure errate. Deve passare un certo tempo tra una misura e l'altra.

Ciao Uwe

Grazie per la risposta.
Per il fatto dei problemi dei due ultrasuoni che si disturbano non c'è alcun problema in quanto sono completamente separati.
Non capisco come la vasca 1 sia fuori del loop.
Come devo fare per far si che faccia anche la n.1?

nessuno che sappia aiutarmi?

giatora:
Non capisco come la vasca 1 sia fuori del loop.

Controlla dove é chiusa la parentesi graffa della funzione loop.
Ciao Uwe

ho provato e riprovato ma non riesco a capire l'errore.

giatora:
ho provato e riprovato ma non riesco a capire l'errore.

Leggi i miei commenti.

#include <NewPing.h>
#include   <LiquidCrystal.h> 
 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
int pingPin = 9;
int inPin = 8;
int pingPin1 =10;
int inPin1 = 7;
 
void setup(){
  //imposto il numero di colonne ed il numero di righe di lcd
  //usando il metodo lcd
  lcd.begin(16, 2);
  // Scrive il messaggio sul display.
  lcd.setCursor(0, 0);
  lcd.print("H Vasca 1");
  lcd.setCursor(0, 1);
  lcd.print("H Vasca 2");
}
 
void loop()
{
 //parte il 2
 
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration1, cm;
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin1, OUTPUT);
  digitalWrite(pingPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin1, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin1, LOW);
 
  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(inPin1, INPUT);
  duration1 = pulseIn(inPin1, HIGH);
 
  // convert the time into a distance
   cm = microsecondsToCentimeters1(duration1);
    lcd.setCursor(10, 1);
  lcd.print(cm);
  lcd.print(" cm");
  delay(100);
}
//------------------------------------------------------------------------------
// la funzione loop si chiude qua!!!!!!!!!!!!!!
//------------------------------------------------------------------------------

 
long microsecondsToCentimeters1(long microseconds1)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return (500 -(microseconds1 / 29 / 2));
  }

//------------------------------------------------------------------------------
// Il codice sucessivo non viene chiamato o eseguito perché fuori dalla funzione loop()!!!!!!!!!!!!!!
//------------------------------------------------------------------------------
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, cm;
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
 
  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(inPin, INPUT);
  duration = pulseIn(inPin, HIGH);
 
  // convert the time into a distance
   cm = microsecondsToCentimeters(duration);
  lcd.setCursor(10, 0);
  lcd.print(cm);
  lcd.print(" cm");
  delay(0);
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return (500 -(microseconds / 29 / 2));  

}