Function that passes global variable every 30 minutes

I am calling a Function every thirty minutes. Function passes a global variable back into loop. Global variable is passing to the loop; however, I would like to use a starting pressure value, store it for thirty minutes, then calculate the pressure difference at the thirty minute interval. Presently I am not getting the desired result; only want upadateDifference every thirty minutes.

Function is being called every thirty minutes from the loop (Reads real time clock 0 and 30 minute and 00 seconds). Timer portion works as desired in calling the function. Lines 213-215.

Could someone give me some assistance on what is needed to accomplish 30 minute, pressure differences in the function?

Function in the attached “Wifi_Weather_Webserver.zip” sketch is “float updateDifference()”

/**************************************************************************/
	float updateDifference(){
  
           	pressure2 = pressure1; 
		difference = (pressure1 - pressure2);
		return(pressure2);
}

 /**************************************************************************/

William

Wifi_Weather_Webserver.zip (5.27 KB)

Techno500:
I am calling a Function every thirty minutes. Function passes a global variable back into loop.

Actually, no, it doesn't. Your function assigns a value to the global variable difference. You return the value of pressure2, but you don't assign it to anything. It really doesn't matter that you don't assign that value to anything, because it will ALWAYS equal pressure1, because every time you call updateDifference, you set pressure2 equal to pressure1.

If you have a function that you want to return a value from, you call it with something like:

   difference = updateDifference();

   //  and the function will look something like:

float updateDifference(){
   return (pressure1 - pressure2);
}

If you want to update a global variable within a function, you just assign something to it, like:

void updateDifference(){
   difference = (pressure1 - pressure2);
}

Of course, if you only have one place where you call the function, you're better off doing it in-line, like this:

   if (((RTCTimedEvent.time.minute) == 0) || ((RTCTimedEvent.time.minute) == 30) && ((RTCTimedEvent.time.second) == 00)){
      difference = pressure1 - pressure2;
   }

And in your setup(), get pressure1, and assign the value to pressure2. Then each time you get a pressure, calculate the difference, but don't forget to make pressure1 equal pressure2 after you get the difference.

lar3ry Thank you for the reply.
e
I am taking a pressure reading in setup(), setting pressure reading eaual to pressure1 and setting pressure1 equal to pressure2 (Lines 175-180).

Outside of server.available(); I am placing event timer for 0 minute and 30 minute at 00 seconds and assigning difference to pressure1- pressure2. (Line 218-220).

After out putting difference on webpage and when connection closes a put pressure 1 = pressure2 statement. (Line 362).

Problem is it always displays 0.000.

Wifi_Weather_Webserve_2.zip (5.26 KB)

Your indenting is atrocious. I find it very difficult to follow.

.Printing anonymous values makes it difficult to determine just what the value represents. “It always prints 0” doesn’t tell us squat.

What is always 0?

Can’t you find names that mean more than pressure1 and pressure2? Maybe currPressure and prevPressure?

The getPressure() call writes to Pressure. Why do you have 3 variables with nearly indistinguishable names?

	float updateDifference(){
  
            pressure2 = pressure1; //This will be pressure from this pass thru loop, pressure1 will be new pressure reading next loop pass
			difference = (pressure1 - pressure2);
		    return(pressure2);

}

Since pressure2 is assigned the value in pressure1, it doesn’t take an advanced degree in mathematics to determine that the difference is 0.

PaulS, Thanks, I’ll be more thoughtful in naming variables and I’ll do my best to keep indentations readable.

Renamed pressure1, pressureNow . Renamed pressure2, pressureThirty.

Attached updated version of “Wifi_Weather_Sketch_testing.zip.”

lar3ry’s Suggestion worked. :slight_smile:

Wifi_Weather_Webserver_testing.zip (5.16 KB)

Well, you almost have it...

You call the function like this: updateDifference();

But the function is written:

float updateDifference(){
   difference = pressureNow - pressureThirty;
   return(difference);
}

The reason it works is because of the first line in the function, difference = pressureNow - pressureThirty;
The second line in the function returns the value in difference, but it is never used, because you don't assign that value to anything. If you remove the return line completely, it will still work.

Re-read my previous posting to see the two methods of getting a value into the variable difference. Pay particular attention to the type of the function (float vs. void), which stipulate what data type the function returns, if any. As well, pay attention to how the function is called in the case where the function contains a return, and the case where it doesn't.

Functions are a very powerful concept, and anyone writing code should learn the principles of them. Have a look at the following tutorials:

http://playground.arduino.cc/Code/Function