making a Fuelmeter

I want to make a fuel meter. It can be used in cars, boats, motorcycles, or anywhere to measure the water flow.
It should show the current amount, hour and daily amount of liquid passed through the sensor. It is based on YF-S401 flow meter and ST7789 display.
I'm bad at coding, so i found two sketches and trying to combine them. One sketch is for fuel sensor and the second one is for stopwatch. I want the stopwatch to start when there is a signal from the sensor and to stop when there is no signal, but it's not starting. Originally the stopwatch was triggered by the button AnalogRead, but I need it to be digitalRead coz my sensor sends digital signal.

#include <Adafruit_GFX.h>    // Core graphics library by Adafruit
#include <Arduino_ST7789.h> // Hardware-specific library for ST7789 (with or without CS pin)
#include <SPI.h>
//#include "FontsRus/TimesNRCyr16.h"

#define TFT_DC    8 
#define TFT_RST   9 //RES
#define TFT_CS    10 // only for displays with CS pin NIL
#define TFT_MOSI  11   // for hardware SPI data pin (all of available pins) SDA
#define TFT_SCLK  13   // for hardware SPI sclk pin (all of available pins)SCK
Arduino_ST7789 tft = Arduino_ST7789(TFT_DC, TFT_RST); //for display without CS pin


volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
void flow () // Interrupt function
{
   flow_frequency++;
}


unsigned long start, finished, elapsed;
boolean r = false;

long lastButtonPressTime = 0; 
long debounceDelay = 50; 




void setup () {
 

  tft.init(80, 160);   // initialize a ST7789 chip, 240x240 pixels

  uint16_t time = millis();
  tft.fillScreen(BLACK);
tft.setTextColor(WHITE, BLACK);



pinMode(flowsensor, INPUT);
   digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
   
   attachInterrupt(0, flow, RISING); // Setup Interrupt
   sei(); // Enable interrupts
   currentTime = millis();
   cloopTime = currentTime;





}

void loop()
{  
  CheckFlow();
  CheckStartStop();
DisplayResult();
  
 //tft.setFont(&TimesNRCyr16pt8b); 

}
   

 

void CheckFlow()
{
tft.setCursor(5, 30);

  tft.setTextSize(2);
  tft.println("Fuel Meter");
 

 currentTime = millis();
   // Every second, calculate and print litres/hour
  if(currentTime >= (cloopTime + 1000))
   {
     cloopTime = currentTime; // Updates cloopTime
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
    l_hour = (flow_frequency * 60 / 5880); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
     flow_frequency = 0; // Reset Counter
      tft.setCursor(5, 60);

  tft.setTextSize(2);
 
      tft.print(l_hour, DEC); // Print litres/hour
    tft.println(" L/h");

   }

   }



void CheckStartStop()
{
  
int x = digitalRead(2); // button input
if (x < 800 && x > 600 ) 
{
if ((millis() - lastButtonPressTime) > debounceDelay)
 {

if (r == false)
  {
tft.fillScreen(BLACK);
tft.setCursor(3, 90); 
tft.setTextSize(2);
tft.print("Time Past");
start = millis(); 
}
else if (r == true)
{
tft.setCursor(2, 120); 
tft.print(" Final Time ");
}
r = !r;
    }
lastButtonPressTime = millis();
   
   
   }
 }




void DisplayResult()
{
if (r == true)
{
finished = millis(); 
float h, m, s, ms;
unsigned long over;


elapsed = finished - start;

h = int(elapsed / 3600000);
over = elapsed % 3600000;
m = int(over / 60000);
over = over % 60000;
s = int(over / 1000);
ms = over % 1000;

tft.setCursor(0, 160);

tft.print(h, 0); 
tft.print("h "); 
tft.print(m, 0);
tft.print("m ");
tft.print(s, 0);
tft.print("s ");

if (h < 10)
  {
tft.print(ms, 0);
tft.print("ms ");

  }
    }
  
}

I'm bad at coding, so i found two sketches and trying to combine them.

I'm bad at car mechanics. If I cut two different cars in half, an Audi and a Honda, then try to make a new car by welding together the front of the Honda to the back of the Audi, do you think I will be able to make a working car? No, of course not. This is a similar situation.

You are attempting something which is well beyond your current level of experience, so it's not a surprise that it does not work. You need to start from scratch and keep things as simple as possible to begin, then build up, little by little, testing at each stage.

Perhaps put the display aside for now. Start with a blank sketch and write a very simple sketch which will read the flow sensor input (high/low) and echo it to serial monitor. Then you will know that you are able to read the flow meter correctly. Next, bring in some basic timing and calculate a flow rate. Send that to serial monitor. Then test your code with very fast and very slow flow rates. Is it accurate enough? Does it update often enough or too often? You can use other people's code as examples, but do not be tempted to copy large sections of that code into your own sketch. You need to understand the exact function of every line of code you add to your sketch.

Only when you have that working should you connect the display and run some example sketches to test that the display is working. After that, it is time to start adding commands to your flow meter sketch to use the display, keeping it as simple as possible to begin.

I would also recommend that you start using the Auto Format function in the IDE. Indentation and formatting don't change how the sketch works, or fix a broken sketch, but they are very important to help you read and debug your code. The brackets and braces are very important in C and very easy to get wrong, especially for a beginner. Indentation will help you get them right.

You wrote such a long post but didn't understand my question

The display is working, the readings from flow meter are displayed. I just don't understand how to start the stopwatch using the flow sensor input.

Definetly something is wrong in this part, coz it was coded for a button, not for a sensor

void CheckStartStop()
{
  
int x = digitalRead(2); 
if (x < 800 && x > 600 )
{
if ((millis() - lastButtonPressTime) > debounceDelay)
 {

if (r == false)
  {
tft.fillScreen(BLACK);
tft.setCursor(3, 100); 
tft.setTextSize(2);
tft.print("Time Past");
start = millis(); 
}
else if (r == true)
{
tft.setCursor(2, 120); 
tft.print(" Final Time ");
}
r = !r;
    }
lastButtonPressTime = millis();
   
   
   }
 }

ok, fixed it. Now everything is working correctly. Here is the code. I will try to add some more data and make it more stable

#include <Adafruit_GFX.h>    // Core graphics library by Adafruit
#include <Arduino_ST7789.h> // Hardware-specific library for ST7789 (with or without CS pin)
#include <SPI.h>
//#include "FontsRus/TimesNRCyr16.h"

#define TFT_DC    8
#define TFT_RST   9 //RES
#define TFT_CS    10 // only for displays with CS pin NIL
#define TFT_MOSI  11   // for hardware SPI data pin (all of available pins) SDA
#define TFT_SCLK  13   // for hardware SPI sclk pin (all of available pins)SCK
Arduino_ST7789 tft = Arduino_ST7789(TFT_DC, TFT_RST); //for display without CS pin


volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
void flow () // Interrupt function
{
  flow_frequency++;
}


unsigned long start, finished, elapsed;



long debounceDelay = 5; 




void setup () {


  tft.init(80, 160);   // initialize a ST7789 chip, 240x240 pixels

  uint16_t time = millis();
  tft.fillScreen(BLACK);
  tft.setTextColor(WHITE, BLACK);



  pinMode(flowsensor, INPUT);
  digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up

  attachInterrupt(0, flow, RISING); // Setup Interrupt
  sei(); // Enable interrupts
  currentTime = millis();
  cloopTime = currentTime;





}

void loop()
{
  CheckFlow();
 
  DisplayResult();

  //tft.setFont(&TimesNRCyr16pt8b);

}




void CheckFlow()
{


  tft.setCursor(5, 30);

  tft.setTextSize(2);
  tft.println("Fuel Meter");


  currentTime = millis();
  // Every second, calculate and print litres/hour
  if (currentTime >= (cloopTime + 1000))
  {
    cloopTime = currentTime; // Updates cloopTime
    // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
    l_hour = (flow_frequency * 60 / 5880); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
    flow_frequency = 0; // Reset Counter


    tft.setCursor(5, 60);

    tft.setTextSize(2);

    tft.print(l_hour, DEC); // Print litres/hour
    tft.println(" L/m");



  }

}



void DisplayResult()
{
  int x = digitalRead(2);
  if (x == HIGH)
  {
    finished = millis(); 
    float h, m, s, ms;
    unsigned long over;

   
    elapsed = finished - start;

    h = int(elapsed / 3600000);
    over = elapsed % 3600000;
    m = int(over / 60000);
    over = over % 60000;
    s = int(over / 1000);
    ms = over % 1000;
    // показать результаты

    tft.setCursor(3, 100); 
    tft.setTextSize(2);
    tft.print("Hour meter");

    tft.setCursor(0, 160);

    tft.print(h, 0);
    tft.print("h ");
    tft.print(m, 0);
    tft.print("m ");
    tft.print(s, 0);
    tft.print("s ");

    if (h < 10)
    {
      tft.print(ms, 0);
      tft.print("ms ");

    }
  }

}

You wrote such a long post but didn't understand my question

Forgive me, reading your original post and your messy code, I assumed you were a long way from a solution. Most beginners who come to the forum and ask similar questions are a long way from a solution.

For the benefit of others, what was wrong before and how did you did it?