while loop, help please

Hello
we are trying to make a while loop working…without success.

we have 1 ultrasonic sensor and we have 2 conditions:

if distance is < 150
and
if distance is < 50

so we inserted the II condition into the 1° loop using “While” but seems that do not read the distance value… we tried all the afternoon… some help please?

here is the code:

#written by Alessandro Migliorini
int buzzer          = 12;
int led1             = 11;
int led2             = 2;
const int trigger = A1;
const int sensor = A2;

void setup() {
  Serial.begin(9600);           
  Serial.print( "Sensor value: ");
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(attivatore,OUTPUT);
  pinMode(sensor,INPUT);
  pinMode(buzzer,OUTPUT);

  
}

void loop() {
  digitalWrite( led1, LOW);
  digitalWrite( led2, LOW);
  digitalWrite( attivatore, LOW );        //attivatore o grilletto spento
  digitalWrite( attivatore, HIGH );       //invia un impulso di 10microsec su trigger
  delayMicroseconds( 10 );
  digitalWrite( trigger, LOW );
  long durata = pulseIn( sensor, HIGH );
  long distanza = 0.034 * durata / 2;

  Serial.print("distance: ");
  if( durata > 38000 ){
  Serial.println("Fuori portata   ");
    }
    
  else{ 
    Serial.print(distanza); 
    Serial.println(" cm     ");
    }
 
  if( distanza < 150 ){
       
      digitalWrite( led1, HIGH);
      digitalWrite( led2, HIGH);
      digitalWrite(buzzer,HIGH);
      delay(100); 
      digitalWrite(buzzer,LOW);
      delay(100); 
      digitalWrite(buzzer,HIGH);
      delay(100); 
      digitalWrite(buzzer,LOW);

    while (distanza <50)    {
      digitalWrite( trigger, LOW );        //attivatore o grilletto spento
      digitalWrite( trigger, HIGH );       //invia un impulso di 10microsec su trigger
      delayMicroseconds( 10 );
      digitalWrite( trigger, LOW );
      long durata = pulseIn( sensor, HIGH );
      long distanza = 0.034 * durata / 2;

      Serial.print("distance: ");
      if( durata > 38000 ){
      Serial.println("Fuori portata   ");
      }
    
      else{ 
       Serial.print(distanza); 
       Serial.println(" cm     ");
       }
        
     digitalWrite( led1, HIGH);
      digitalWrite( led2, HIGH);
      digitalWrite(buzzer,HIGH);
      delay(30); 
      digitalWrite(buzzer,LOW);
      delay(30); 
      digitalWrite(buzzer,HIGH);
      delay(30); 
      digitalWrite(buzzer,LOW);
      digitalWrite(buzzer,HIGH);
      delay(30); 
      digitalWrite(buzzer,LOW);
      delay(30); 
      digitalWrite(buzzer,HIGH);
      delay(30); 
      digitalWrite(buzzer,LOW);
    }
     delay(1000);
     
    }
  }

Try making this a float instead of long:

long distanza = 0.034 * durata / 2;

Hi
thank you

is not that the problem, distance is correct in serial monitor, but even distance is >50 the code execute while condition and don't go out the loop.
(anyway I checked, but not solved)

distanza: 265 cm
distanza: 265 cm
distanza: 313 cm
distanza: 265 cm
distanza: 266 cm
distanza: 265 cm
distanza: 209 cm
distanza: 265 cm
distanza: 266 cm
distanza: 264 cm
distanza: 265 cm
distanza: 264 cm
distanza: 265 cm
distanza: 264 cm
distanza: 265 cm

You have this code twice:

  Serial.print("distance: ");
  if( durata > 38000 ){
  Serial.println("Fuori portata   ");
    }
   
  else{ 
    Serial.print(distanza); 
    Serial.println(" cm     ");
    }

and

      Serial.print("distance: ");
      if( durata > 38000 ){
      Serial.println("Fuori portata   ");
      }
      else{ 
       Serial.print(distanza); 
       Serial.println(" cm     ");
       }

Make them the "distance" part unique so you can see which one it's printing from.

without the duplication of code,
the distance is “printed 0”
when the condition “<50” enable the WHILE loop

the code is made from my son 10 years old,
but have some logic…

into the while the “distance” was not read any more…
for that reason he tried to read again the distance and print that again into the while loop

You are defining “distanza” twice, once outside the while loop and once inside. I suspect that the line

    while (distanza <50)    {

is using the old distanza, which does not change inside the loop because there is a new distanza declared.

Try using the same variable as below and see what happens.

    while (distanza <50)    {
      digitalWrite( trigger, LOW );        //attivatore o grilletto spento
      digitalWrite( trigger, HIGH );       //invia un impulso di 10microsec su trigger
      delayMicroseconds( 10 );
      digitalWrite( trigger, LOW );
      long durata = pulseIn( sensor, HIGH );
      distanza = 0.034 * durata / 2;          // did not redeclare distanza

The code you posted does not compile due to “attivatore” not being declared and an illegal comment character at the beginning, so the version that you are running is different.

the code is this one (I transalted some part as it id written in “italian”)

int buzzer           = 12;
int led1             = 11;
int led2             = 2;
const int attivatore = A1;
const int sensore    = A2;

void setup() {
  Serial.begin(9600);           
  Serial.print( "Sensore Ultrasuoni: ");
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(attivatore,OUTPUT);
  pinMode(sensore,INPUT);
  pinMode(buzzer,OUTPUT);

  
}

void loop() {
  digitalWrite( led1, LOW);
  digitalWrite( led2, LOW);
  digitalWrite( attivatore, LOW );        //attivatore o grilletto spento
  digitalWrite( attivatore, HIGH );       //invia un impulso di 10microsec su trigger
  delayMicroseconds( 10 );
  digitalWrite( attivatore, LOW );
  long durata = pulseIn( sensore, HIGH );
  long distanza = 0.034 * durata / 2;

  Serial.print("distanza: ");
  if( durata > 38000 ){
  Serial.println("Fuori portata   ");
    }
    
  else{ 
    Serial.print(distanza); 
    Serial.println(" cm     ");
    }
 
  if( distanza < 150 ){
       
      digitalWrite( led1, HIGH);
      digitalWrite( led2, HIGH);
      digitalWrite(buzzer,HIGH);
      delay(100); 
      digitalWrite(buzzer,LOW);
      delay(100); 
      digitalWrite(buzzer,HIGH);
      delay(100); 
      digitalWrite(buzzer,LOW);

    while (distanza <50)    {
      digitalWrite( attivatore, LOW );        //attivatore o grilletto spento
      digitalWrite( attivatore, HIGH );       //invia un impulso di 10microsec su trigger
      delayMicroseconds( 10 );
      digitalWrite( attivatore, LOW );
      long durata = pulseIn( sensore, HIGH );
      long distanza = 0.034 * durata / 2;

      Serial.print("distanza: ");
      if( durata > 38000 ){
      Serial.println("Fuori portata   ");
      }
    
      else{ 
       Serial.print(distanza); 
       Serial.println(" cm     ");
       }
        
     digitalWrite( led1, HIGH);
      digitalWrite( led2, HIGH);
      digitalWrite(buzzer,HIGH);
      delay(30); 
      digitalWrite(buzzer,LOW);
      delay(30); 
      digitalWrite(buzzer,HIGH);
      delay(30); 
      digitalWrite(buzzer,LOW);
      digitalWrite(buzzer,HIGH);
      delay(30); 
      digitalWrite(buzzer,LOW);
      delay(30); 
      digitalWrite(buzzer,HIGH);
      delay(30); 
      digitalWrite(buzzer,LOW);
    }
     delay(1000);
     
    }
  }

YESSSS

worked!!!!

Thank you!

CrossRoads:
Try making this a float instead of long:

long distanza = 0.034 * durata / 2;

[/quote]

fedale:
(that) is not the problem, distance is correct in serial monitor

Even if it works, that's still a terrible way to do that calculation. I had to check the behavior of that code on my PC, and yes it did give the correct answer (though with a compiler warning of "conversion from double"). The Arduino wont use double as the intermediate in that calculation, but it will use a float.

Why not just use:
long distanza = 17 * durata / 1000;