I can't display what I want on my console

Hello everyone ,
I have a small problem with my program, I can't display what I want on my console.
If you could help me that would be nice.
just below, thanks again !

/*

  • Code d'exemple pour un capteur à ultrasons HC-SR04.
    */

/* Constantes pour les broches */
const byte TRIGGER_PIN = 6; // Broche TRIGGER
const byte ECHO_PIN = 5; // Broche ECHO

/* Constantes pour le timeout */
const unsigned long MEASURE_TIMEOUT = 25000UL; // 25ms = ~8m à 340m/s

/* Vitesse du son dans l'air en mm/us */
const float SOUND_SPEED = 340.0 / 1000;

float distance;

/** Fonction setup() */
void setup() {

/* Initialise le port série */
Serial.begin(9600);

/* Initialise les broches */
pinMode(TRIGGER_PIN, OUTPUT);
digitalWrite(TRIGGER_PIN, LOW); // La broche TRIGGER doit être à LOW au repos
pinMode(ECHO_PIN, INPUT);
}

/** Fonction loop() */
void loop()

{

if(distance >= 10)
{
Serial.println("pas de spray");
delay(500);
}
else
{
Serial.println("spray enclenché");
delay(500);

}

}

float mesure_dist()
{
/* 1. Lance une mesure de distance en envoyant une impulsion HIGH de 10µs sur la broche TRIGGER */
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);

/* 2. Mesure le temps entre l'envoi de l'impulsion ultrasonique et son écho (si il existe) /
long measure = pulseIn(ECHO_PIN, HIGH, MEASURE_TIMEOUT);
/
3. Calcul la distance à partir du temps mesuré */
float distance_mm = measure / 2.0 * SOUND_SPEED;

/Serial.print(F("Distance: "));/
distance = (distance_mm / 10.0, 2);
/*Serial.print(F("cm, ")); */
return distance;
delay(500);
}

You have hijacked someone elses thread. Please start a new topic for your question. Before you do, please go back and read the forum instructions that you were shown when you created your account. It will help you understand how the forum works, and how to post code correctly.

@hugo_24

Topic split from another topic. Please do not add your own questions to the end of other people's topics.

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

It will help you get the best out of the forum in the future.

Thank you.

You didn't call this function. You only implemented it.

This line of code doesn't make any sense. It will actually assign the value 2 to distance.
distance = (distance_mm / 10.0, 2);

Here the delay(500) will never be executed.

return distance;
delay(500);

In each case, you delay. That is redundant. If you always do it, take it out of the 'if else'.

if(distance >= 10)
{
Serial.println("pas de spray");
}
else
{
Serial.println("spray enclenché");
}
delay(500);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.