Screenshot of the portion of the code in which i am having problem is attached. All variables are self explanatory except mL, which is for ‘milliliters’, a variable storing current delivered air-volume.
Second screenshot is of the serial monitor. If statement “if(mL>=requied_mL-100)” executes, which is usually the case, ‘1’ is printed on the monitor with both variables being compared in the if-statement (mL is being printing somewhere later in the code).
The problem is underlined in the serial monitor - if-statement executing wrong… And it executes wrong at least one times in 50 executions. Moreover, the underlined value of mL prints every time, every time.
SaadPasha:
Code has 1128 lines. I don't want trouble for anyone to read all of it, whereas the problem is in just one line.
Then post an MCVE. This is the smallest, COMPLETE (meaning we can copy into the IDE and compile it) program that demonstrates the problem you're seeing. Leave out everything unrelated to the problem.
SaadPasha:
If i just paste all of the code, how you people will check it as you don't have the hardware? Is there a way to check the code without hardware?
Well it might give us a chance to see it and spot problems - for instance we have no idea if you've correctly
declared the right variables as volatile, and you are using an ISR so this matters.
One thing I notice is that you enable interrupts inside your ISR. Why?
Other things is possible divide-by-zero in your ISR; what type are your variables peakPressure and peakFlow? The code will not crash but unexpected behaviour can be the result.
I would not recommend enabling interrupts in the ISR without a good reason. Pretty sure you dont have one, especially since the isr will execute quickly.
Could be related to that, but I'm betting it's variables not declared volatile which need to be.
ISR is being calling after every 1msec. ISR updates the value of mL every time, and this is possible that timer interrupt comes in during "if(mL > required_mL-100)-condition" execution. And that was actually happening, which was leading to wrong if-condition execution.
Now, i put this if-condition in the ISR and used a flag in the CVM() function. Now it is working fine.
One thing left from you people, can you explain volatile variables? And why, when and where to use them?
And thanks for all your support and time. Thanks a lot.