have readings from functions availabel in other functions and the loop

Hi guys, I'm sure that it isn't a great deal but I just couldn't figure it out and couldn't find a suitable post.

I’ve been told to outsource code into functions and I must say, for me coding is easier and better to read when blocks are “outsourced” into functions. But the problem I’m having is, that I don’t have the readings available anymore in the loop or other functions. My attached code, has 3 main functions. 2 sensor readings and a serial_print.

The line serial.println in the function “long sensor1/2()” work fine and print values. But the serial.println in the function “Serial_print()” do NOT.

How do I get it to work? How do I make the value (Distance_mm1) available to the loop or other functions? The goal would be to add more functions such as “print to an oled” or “turn on the pump” both referring to readings from Sensor1 and Sensor2

thanks a lot,

const int TriggerPin1 = 2;      //Trig pin = D2
const int EchoPin1    = 3;      //Echo pin = D3
const int TriggerPin2 = 4;      //Trig pin = D4
const int EchoPin2    = 5;      //Echo pin = D5
long Duration1;
long Duration2;
long Distance_mm1;
long Distance_mm2;


void setup(){
  pinMode(TriggerPin1,OUTPUT);  
  pinMode(EchoPin1,INPUT);      
  pinMode(TriggerPin2,OUTPUT);  
  pinMode(EchoPin2,INPUT);      
  
  Serial.begin(9600);          // Serial Output
}

void loop(){ 

  Sensor1();
  Sensor2();

  Serial_print ();

  delay(1000);                             // Wait to do next measurement
}



long Sensor1 () {
  digitalWrite(TriggerPin1, LOW);                   
  delayMicroseconds(2);
  digitalWrite(TriggerPin1, HIGH);         
  delayMicroseconds(10);                   
  digitalWrite(TriggerPin1, LOW);           

  Duration1 = pulseIn(EchoPin1,HIGH);        
  long Distance_mm1 = Distance(Duration1); 
  Serial.println(Distance_mm1);
  
}

long Sensor2() {
  digitalWrite(TriggerPin2, LOW);                   
  delayMicroseconds(2);
  digitalWrite(TriggerPin2, HIGH);          
  delayMicroseconds(10);                   
  digitalWrite(TriggerPin2, LOW);           

  Duration2 = pulseIn(EchoPin2,HIGH);        
  long Distance_mm2 = Distance(Duration2);
  Serial.println(Distance_mm2);    
}

void Serial_print (){
  Serial.print("Distance1 = ");             
  Serial.print(Distance_mm1);
  Serial.println(" mm");

  Serial.print("Distance2 = ");             
  Serial.print(Distance_mm2);
  Serial.println(" mm");
  
}


long Distance(long time)
{
    // ((time)*(Speed of sound))/ toward and backward of object) * 10
   
    long DistanceCalc;                      // Calculation variable
    DistanceCalc = ((time /2.9) / 2);     // Actual calculation in mm
    return DistanceCalc;                    // return calculated value
}

You declare Distance_mm1 and Distance_mm2 twice. First before the loop and then into a function. So, when you use Distance_mm1 in your sensor function, you use a local variable that is destroyed when the function ends. Just use your global variable Distance_mm1. Basically just remove the "long" before Distance_mm1 and Distance_mm2 into your functions Sensor1 and Sensor2 :)

In Sensor1() you are creating a new variable called

long Distance_mm1 = Distance(Duration1);

you are not using the global Distance_mm1 variable.

thanks guys!

you use a local variable that is destroyed when the function ends.

valuable information too!

there is good documentation to read on Arduino Variables and their scope and then if you want to go deeper read C++ documentation (here is just an example, there are plenty)