SOLVED : Using Return function correctly

Hi Guys,

I am having issue returning values from a Distance sensor to my main loop() function. I have an error : Void value not ignored as it ought to be... I am following this tuto but cannot make it work thanks !

void loop() {

float measure = distance();

Serial.println(measure);

    ChangePalettePeriodically();
    
    FillLEDsFromPaletteColors();
    
    } 
 }



 void distance(){

  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);
  
  long measure = pulseIn(ECHO_PIN, HIGH, MEASURE_TIMEOUT);
   
  float distance_mm = measure / 2.0 * SOUND_SPEED;
Serial.println(distance_mm);
    return distance_mm;
   
//  Serial.print(F("Distance: "));
//  Serial.print(distance_mm);
//  Serial.print(F("mm ("));
//  Serial.print(distance_mm / 10.0, 2);
//  Serial.print(F("cm, "));
//  Serial.print(distance_mm / 1000.0, 2);
//  Serial.println(F("m)"));
   
  delay(500);


 }
      
float measure = distance();

This function call expects the distance function to return a float value

However

 void distance(){

The function definition has the return type set to void which means that no value will be returned

Change it to

 float distance(){

as a starting point, but there may be other problems

Please post your entire sketch, so it can be read, analyzed and tested.

Some or all of which might be revealed if you set the preferences in the IDE...

Turn on all compiler warnings! Then read and heed the advice which can be gleaned from the red ink that spills during verification or uploading.

HTH

a7

Thanks my friend.... it works !

You have an extra '}' at the end of loop().

Your 'delay(500)' at the end of distance() will never be executed because there is a 'return' before it.

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