Passing variables from a cpp file to ino file

Hi,

so i am trying to send variable data from a cpp file to and ino file and i am getting an error. The purpose of the code is to measure the speed of a moving object. The object will travel 8cm and i am using two ultrasonic distance sensors to determine how long it takes for the object to cover 8cm. So the function MEASURE_BLOCK_DISTANCE will wait for the object to pass it by and will record the time with millis() in T1 and then as the block object continues to move it will pass by a second distance sensor with the function MEASURE_BLOCK_DISTANCE_AGAIN and then it will record the time in T2 once more. Finally the speed will be calculated with SPEED = 8/(T2-T1)

please see below:

Here is my cpp file for MEASURE_BLOCK_DISTANCE which is identical to MEASURE_BLOCK_DISTANCE_AGAIN except that variables have a 2 at the end

#include <MEASURE_BLOCK_DISTANCE_AGAIN.h>



void MEASURE_BLOCK_DISTANCE_AGAIN(float* T2) {
const int trigPin2 = 10;
const int echoPin2 = 11;


long duration;
int distance;
//float T2;
	 
	 
  // put your setup code here, to run once:
	// put your setup code here, to run once:
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  Serial.begin(9600);
int i = 1;

while(i){

  // put your main code here, to run repeatedly:
  digitalWrite(trigPin2, LOW); // make sure trig pin is initialy low
  delayMicroseconds(2);

  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(11);
  digitalWrite(trigPin2, LOW);

  duration = pulseIn(echoPin2, HIGH); // output proportional to range
  distance = duration*0.034/2;   //0.034cm/microsec

  Serial.print("Distance: ");
  Serial.println(distance);
 
  if (distance < 10){
    
     T2 = millis(); // T2 to be defined in final software
     delay(10);
     //Serial.println(T2);
     //Serial.println(5/T1,5);
     delay(10);
	 
     i =0;
      
    }
  
  } 
return(T2);			
		
}

Here is my code for the MEASURE_BLOCK_DISTANCE header file which is identical to MEASURE_BLOCK_DISTANCE_AGAIN except every variable has a 2 at the end

// Define guards for this header 
#ifndef MEASURE_BLOCK_DISTANCE_H
#define MEASURE_BLOCK_DISTANCE_H


extern float T1;
// Define guards to make sure .c functions/objects play nicely with .cpp functions/objects
// #ifdef __cplusplus
// extern "C" {
// #endif

 /******************************************************************************
 * Function Prototypes 
 *******************************************************************************/

void MEASURE_BLOCK_DISTANCE(float* T1);


// #ifdef __cplusplus
// }
// #endif // __cplusplus
 #endif // MEASURE_BLOCK_DISTANCE_H

Here is the source code, the ino file that i want to send T2 and T1 to.

#include <MEASURE_BLOCK_DISTANCE.h>
#include <MEASURE_BLOCK_DISTANCE_AGAIN.h>

void setup() {
  // put your setup code here, to run once:

Serial.begin(9600);
 MEASURE_BLOCK_DISTANCE(&T1); 
 
 MEASURE_BLOCK_DISTANCE_AGAIN(&T2);

  float SPEED = 8/(&T2-&T1);
  Serial.println(SPEED);

}

Finally here is the error message that i am receiving:

C:\Users\frank\AppData\Local\Temp\ccea9PKf.ltrans0.ltrans.o: In function `setup':

C:\Users\frank\Desktop\SORTING_ROBOT_SW\SIT_IntegrationTestofSomeComponents/SIT_IntegrationTestofSomeComponents.ino:46: undefined reference to `T2'

C:\Users\frank\Desktop\SORTING_ROBOT_SW\SIT_IntegrationTestofSomeComponents/SIT_IntegrationTestofSomeComponents.ino:46: undefined reference to `T2'

C:\Users\frank\Desktop\SORTING_ROBOT_SW\SIT_IntegrationTestofSomeComponents/SIT_IntegrationTestofSomeComponents.ino:46: undefined reference to `T1'

C:\Users\frank\Desktop\SORTING_ROBOT_SW\SIT_IntegrationTestofSomeComponents/SIT_IntegrationTestofSomeComponents.ino:46: undefined reference to `T1'

collect2.exe: error: ld returned 1 exit status

Any help will be greatly appreciated.

your function is of type void so it can NOT return a value. Since you are retuning the time, it should be delcared as unsigned long

Your variables are always declared "extern" so each module (.ino, .cpp) is saying "This is declared somewhere else." but nobody is declaring the variables.

Put the declarations near the top of your .ino file:

float T1;
float T2;

Yes, it is OK for the .ino to contain the "extern" declarations in the include files and the actual declarations.