I think there are still more includes there than needed,
the rest is still as gruesome as before.
To give other people (on smartphones or tablets) a chance to look at your code,
I post is like you should have done.
#include <NexButton.h>
#include <NexNumber.h>
#include <NexPage.h>
#include <NexPicture.h>
#include <NexProgressBar.h>
#include <NexText.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <NexVariable.h>
int CurrentPage = 0;
volatile int NbTopsFan; //measuring the rising edges of the signal
float Calc;
int hallsensor = 2;
float totaldispensed;
int whole_intVal;
float diffValue;
int dec_intVal;
int ml_reading;
int barval_arduino = 0;
bool a = false;
bool b = false;
int loopbreak = 0;
NexButton b0 = NexButton(1, 1, "b0");
NexButton b1 = NexButton(1, 11, "b1");
NexNumber n0 = NexNumber(1, 9, "n0");
NexNumber liveread_whole = NexNumber(1, 6, "liveread_whole");
NexNumber liveread_dec = NexNumber(1, 13, "liveread_dec");
NexPage page0 = NexPage(0, 0, "page0"); // Page added as a touch event
NexPage page1 = NexPage(1, 0, "page1");
NexPage page2 = NexPage(2, 0, "page2");
NexPage page3 = NexPage(3, 0, "page3");
NexPage page4 = NexPage(4, 0, "page4");
NexTouch *nex_listen_list[] = //list of touch events
{
&b0,
&b1,
&page0, // Page added as a touch event
&page1,
&page2,
&page3,
&page4,
NULL
};
// Page change event:
void page0PushCallback(void *ptr) // If page 0 is loaded on the display, the following is going to execute:
{
CurrentPage = 0; // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
} // End of press event
void page1PushCallback(void *ptr)
{
CurrentPage = 1;
}
//When START button is pushed then released:
void b0PopCallback(void *ptr)
{
//TURN ON COMMAND
a = true;
}
//When STOP button is pushed:
void b1PushCallback(void *ptr)
{
//TURN OFF COMMAND
b = true;
}
// Function for Flowmeter interupt:
void rpm () //This is the function that the interupt calls
{
NbTopsFan++; //This function measures the rising and falling edge of the hall effect sensors signal
}
void setup() //
{
b0.attachPop(b0PopCallback);
b1.attachPush(b1PushCallback);
page0.attachPush(page0PushCallback);
page1.attachPush(page1PushCallback);
//Flowmeter Setup Code:
pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
pinMode(13, OUTPUT);
attachInterrupt(0, rpm, RISING); //and the interrupt is attached
Serial.begin(115200);
Serial.println("Starting");
Serial.println("...");
delay(500);
Serial.print("baud=115200");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.end();
Serial.begin(115200);
}
void loop ()
{
nexLoop(nex_listen_list); //check for any touch event
if (a == true)
{
whole_intVal = 0;
dec_intVal = 0;
Serial.print("liveread_whole.val=");
Serial.print(whole_intVal);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("liveread_dec.val=");
Serial.print(dec_intVal);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
uint32_t target_volume = 0;
n0.getValue(&target_volume); //This stores the chosen target volume entered in the display (n0) to the target_volume variable
digitalWrite(13, HIGH); //"TURN ON SOLENOID" (allows fluid flow through flowmeter)
ml_reading = 0;
while (ml_reading <= target_volume)
{
//The integer of Liters units
Serial.print("liveread_whole.val=");
Serial.print(whole_intVal);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
//The decimal value of Liters units
Serial.print("liveread_dec.val=");
Serial.print(dec_intVal);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
//live progress bar in mL
ml_reading = 1000 * whole_intVal + dec_intVal;
barval_arduino = map(ml_reading, 0, target_volume, 0, 100);
Serial.print("j0.val=");
Serial.print(barval_arduino);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
//flowmeter calculations using interrupt:
NbTopsFan = 0;
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts
Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate in L/hour
totaldispensed += (((float)Calc * 1.275) / 3600); //the 1.275 calibrates our current flowmeter(Model: 50-732-C)
whole_intVal = (int)totaldispensed; // convert float PHValue to tricky int combination
diffValue = totaldispensed - (float)whole_intVal;
dec_intVal = (int)(diffValue * 1000.0);
}
digitalWrite(13, LOW); //Turn off "solenoid" that allows fluid flow
a = false; //reset a to false
// reset progress bar
int reset = 0;
Serial.print("j0.val=");
Serial.print(reset); //make value 0 again
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
}
Don't use any delay.
NbTopsFan is volatile and int, so it has to be accessed atomicaly (with disabled interrupts).
Your method to measure the interrupts does not work while relying on serial communication.
And you will have to change the blocking while to a cooperative if.