return is not returning a value from function! Help!!!

I am a newbie, but have embarked on a project. I making a water meter. I have a flow meter on my water line, and want to use the Arduino to read the meter and sound an alarm if it is on to long.

There is a function call (get_duration(new_duration)) to calculate the run time. The code compiles ok. I check the value "new_duration" inside the function and it shows correct, but the value is NOT returned to the main body of the program.The attachment shows just what is going on. Help!!

Peter

water_timer_1_23_2016.ino (9.24 KB)

eliminate that break before the return:

    if (frequency <= freq_lowlimit) 
    {
      break;
      return new_duration;
    }

Just the first thing I noticed...

Thanks for the response! I removed the "break". That did not help though...

I think I put it there for debugging.

Peter

Could you make your code better looking.

For example more comments, more empty lines to seperate different parts and a seperate section for the pin numbers.

// Pin numbers
const int inputPin = 12;
const int ButtonPin = 11;
const int LedPin = 13;
const int AlarmPin = 10;

That makes it better to read, and avoids pin 13 defined twice as you have now.

When you declare global variables, you declare some with a calculation.

float minute_duration=duration / 60;
float gpm=frequency/gpm_constant;

A calculation has to be done in the code, not when declaring a variable.

The variable 'frequency' is a global variable. It is also returned by the function 'get_frequency'. That is confusing. Give it a different name, or use 'frequency' local.
You do the same with 'get_duration' and 'new_duration'.

To make the code better to read, I prefer use floating point numbers when doing floating point calculations.
For example "60.0" instead of "60" and "0.0" instead of "0".

Floating point numbers are almost never exact.
This is weird: "if(new_duration!=old_duration)", since both are floating point numbers. They are almost never the same.

The 'duration' is an integer and 'minute_duration', 'old_duration' and 'new_duration' are float. In the sketch, calculations are done with those numbers, even millis() is used for them.
The result is not accurate and will go wrong in many places.
When using millis(), you have to use unsigned long variables.
The same applies to 'alarm_time;

My suggestion is to make the code better looking. Try to use local variables inside the functions if possible. Rewrite everything you do with millis() and duration.

Thanks! I will work on that, especially using val to get the value out of the function call.

I have only been doing this for a few weeks.

Peter

Since you have looked over this code, there is another problem..

The "get_frequency" function seems to be getting unstable if reading the frequency from the input (pin 12). Sometime it returns 1/2 the actual frequency. (I am using a signal generator.) It was ok when the code was shorter, but seems to have developed this problem when I added the function calls. Its important that the program read the frequency accurately and quickly.

Peter

Delta_G:
You can't call a function or run executable code out there at global scope

Actually you can call a function. For example:

const byte x = digitalRead(8);

void setup() {
  Serial.begin(9600);
  Serial.println(x);
}

void loop() {
}