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!!


water_timer_1_23_2016.ino (9.24 KB)

eliminate that break before the return:

    if (frequency <= freq_lowlimit) 
      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.



You call the function, but you don't do anything with the returned value.

Return doesn't mean it set's some value in your code. Think of return as replacing your function call with the number you tell it to return. You could have a line like:

float val = get_duration(new_duration);

and val would now have the value returned from the function.

Your function is weird though. Generally you should avoid naming a parameter to a function the same as one of your global variables. That's confusing and bound to cause you issues. If you really just want to alter your global variable, then the function doesn't need to take any parameters as it already has access to the global new_duration and it doesn't need to return anything since it has full access to modify the global new_duration.

The parameter new_duration only exists inside that function and it is shadowing the global variable new_duration while that function is running.

I think this is a classic case of completely misunderstanding how functions work.

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.

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

Since when? You can't call a function or run executable code out there at global scope, but you can calculate whatever you want. The compiler will handle the calculation and put in the initial value it calculates.

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.


especially using val to get the value out of the function call.

That was an example, not something you should do. You appear to be wanting to alter the value of the global variable new_duration. In that case you should be writing a function that returns nothing (void) and takes no parameters. Lose the return statement and the parameter and change the return type of the function to void and I bet you get what you thought this would do.

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.


Again, with that function the variable frequency that you reference inside that function is NOT the global variable frequency that you defined at the top of your sketch. This function too should return void and take no arguments.

NEVER name one of your function parameters the same as a global variable. That’s just bad.

I like to put an ‘a’ in front of parameter names just to remind myself inside a function that they are parameters.

int calculateArea(int aWidth, int aLength){
    return aWidth * aLength;

void setup(){
    int wide = 3;
    int long = 4;
    int area = calculateArea(wide, long);
    Serial.print("The area is ");

void loop(){}

But you have a different situation entirely. All those variables you are using are global. You’re in this situation:

int wide = 3;
int long = 4;
int area;

void calculateArea(){
   area = wide * long;

void setup(){
   Serial.print("The area is ");

void loop(){}

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() {

void loop() {