Underwater Arduino Lap-Velocity Counter

Hello everyone.
My item list: Arduino MEGA, LCD, HMC5883L/QMC5883 Sensor (The one without two big holes)
I wrote a code for counting the lap, whenever I change my direction by 180 degrees (like WEST to EAST) the sensor counts lap and LCD shows the data. I also assigned the pool as 24 meters and the sensor also calculates the velocity and shows it on LCD. The problem is that the code is not working (I think the sensor is not working properly) and LCD doesn’t show the data. I assigned 100 and 280 for West and East, but the sensor does not work. Can you please help me? Thank you.
Below you can see the code that I wrote.

#define forward 100
#define backward 280
#define range 10

#include <HMC5883L.h>
#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2); // rs=12,e=11,D4=5,D5=4,D6=3,D7=2

HMC5883L compass;

int lap=0;
unsigned long last_time=0;
unsigned long t=0;
float headingDegrees=0.0;
float heading=0;
int MilliGauss_OnThe_XAxis=0;

float distance=24.0;
float v=0.0;
float time_f=0.0;

void setup()
{
lcd.begin(16,2);
Wire.begin();
compass = HMC5883L();
}

void loop() 
{
read_();

f(headingDegrees<(forward+range) && headingDegrees>(forward-range))
{
lap=lap+1;
while(headingDegrees<(forward+range) && headingDegrees>(forward-range))
{
read_();
}
t=millis()-last_time;
last_time=millis();
time_f=(float)t*0.001;
v=distance/time_f;
lcd.clear();delay(10);
lcd.setCursor(0,0);lcd.print("Time:");lcd.print(time_f);
lcd.setCursor(11,0);lcd.print("Lap:");lcd.print(lap);
lcd.setCursor(0,1);lcd.print("Speed:");lcd.print(v);
}

if(headingDegrees<(backward+range) && headingDegrees>(backward-range))
{
lap=lap+1;
while(headingDegrees<(backward+range) && headingDegrees>(backward-range))
{
read_();
}
t=millis()-last_time;
last_time=millis();
time_f=(float)t*0.001;
v=distance/time_f;
lcd.clear();delay(10);
lcd.setCursor(0,0);lcd.print("Time:");lcd.print(time_f);
lcd.setCursor(11,0);lcd.print("Lap:");lcd.print(lap);
lcd.setCursor(0,1);lcd.print("Speed:");lcd.print(v);
}
}

void read_() //HMC5883L
{
MagnetometerScaled scaled = compass.ReadScaledAxis();
heading = atan2(scaled.YAxis, scaled.XAxis);
if(heading < 0) heading += 2PI;
if(heading > 2PI) heading -= 2*PI;
headingDegrees = heading * 180/M_PI;
delay(10);
}

How did You proceed in the project?
Putting everything together and experiencing this result?

If I was to tell you "it does not work", is that enough information for you to fix the issue or do you need more information other than it does not work?

Where is t declared

t=millis()-last_time;

?

If your pool is east to west then why do you care about degrees? If you swim one wan then direction is a + and if you swim the other way direction is a -, no?