Update function with leak rate from void loop()

Hello..

I've been having a dig around the forums for answers, and google but cant find something that fits what I'm trying to do - or something that I can understand and make work.

I have a flow meter hooked up to a touchscreen. You select the touchscreen button to enter another screen (detailed as void leakState()). It should display the calculation from the current flow rate, which is calculated in the void loop (so that it is constantly being updated).

This is the void loop setup for the flow meter. it reads to the serial monitor correctly.

const int chipSelect = 53; // setup CS (chip select pin as 53 on Mega)
volatile int NbTopsFan; // requirement to measure the rising edges of the flow signal
int calc; //define the calc requirement for the flow
int hallsensor = 19; //define the flow sensor onto pin 19 (PWM pin, with 4 interrupt)
void rpm()
{
  NbTopsFan++; // this function should measure the rise / lose of the hall effect sensor
}
void loop() 
{
   NbTopsFan = 0; //Sets NbTops to 0 ready for calculations
  sei(); // enables interrupt
  delay(100); //pause for interrupt
  cli(); //disables interrupt
  calc = (NbTopsFan * 60 / 7.5); //pulse frequence * 6 / 7.5Q = Flow Rate
  Serial.print (calc, DEC);
  Serial.print(" L/Hr\n");

This is the code from the leakstate function for displaying it in that screen.

void leakstate() //function to display leak rate 
  tft.print(calc, DEC);
  tft.print(" Lt/Hr.");

It's my extremely limited knowledge that I need to update the calc in the function for it to work. Currently it doesn't do anything, unless there is a tft.print(calc, DEC) call in the main loop.

I can post the whole code if anyone needs - there is around 300+ lines in total (As there is a lot of graphics use, and SD card logger and other functions), but I've posted the applicable stuff..and my code is probably quite embarrasing and horrendous to look at! It's not easy being a learner!!!

Also, just noticed that it only updates the total (not a bad thing of course)..

but isn't a LIVE update....

tft.print() almost certainly uses interrupts but interrupts are automatically disabled when in an ISR, so if leakstate() is actually your ISR (impossible to tell from the code fragments that you have posted) the tft.print() won't work.

UKHeliBob,

Thanks for your reply. Thats what I feared to be honest. I've decided that if I change it, and just have the total leak rate displayed (via leakTotal += calc) it should work.

It's not really how i wanted it, but it does what I need.

Thanks!

ISRs should be as short as possible. Set a flag, save the time, save a value and exit. Nothing much more. What is wrong with displaying the values in loop()

It's just the way I have my whole programme setup.

It's split down into various functions, that are called if certain buttons are pressed on the touchscreen.

For this one, you press the button to see the current leak rate, it calls the leak rate function with all the screen settings in it, and should display it. The calculations for that are all in the void loop(), as that needs to be run continously.

I might just change my idea to show the total amount of leaking in the system, and change the display to indicate red / green for a bad or good leak. That's a easy get out.

Next on the agenda is to figure out how to display all of it on a graph, with leak rates V time....

Hmm...

it reads to the serial monitor correctly.

It WRITES TO the serial monitor or it READS FROM the serial monitor (application) correctly.

You do not "read to" anything, except an audience.

Phoenix492:
It's just the way I have my whole programme setup.

But you have not posted the whole program.

...R