I am using the elapsedMillis library for some timer stuff.
I have a global variable declared like this:
elapsedMillis tmr;
I can use it without any problems in the setup() or loop(), however I want to pass it to a function as a parameter:
void test(elapsedMillis x) {
}
Then I want to call it like this:
test(tmr);
However, when I compile, then I get: “error: variable or field ‘test’ declared void”.
Why cant I pass in a variable with type elapsedMillis? But int/bool/etc work?
ScruffR
September 15, 2015, 8:08pm
2
If you want to pass objects you’d rather want to use references void test(myClass& myRef)
And if you only want to pass the millis count (doh’ ) go with @peekay123 ’s suggestion (why did I not think of that interpretation of the question):
@Carsten4207 , elapsedMillis is a class, not a type. The “type” is unsigned long I believe so you should declare void test(unsigned long x)
.
1 Like
mdma
September 15, 2015, 8:15pm
4
This sounds like a preprocessor issue, please add
#pragma SPARK_NO_PREPROCESSOR
to the top of your .ino file.
peekay123:
unsigned long x
So that will just pass in the current duration of the elapsedMillis correct? What if I want to reset it back to 0 from within the function?
I believe I would have to use a pointer correct?
ScruffR:
myClass& myRef
This doesnt work. Get the same error.
@Carsten4207 , assuming you declared tmr
globally, then just set tmr = 0;
anywhere!
Yeah I know I can do that, but was just curious if there is a better way of doing it.
My test code:
int x = 0;
void setup() {
fun(x, 1);
fun(x, 2);
}
void fun(int &variableName, int value) {
variableName = value;
}
With the code above, I can call the function (fun), pass in the global variable I want to update, and assign a value.
I was hoping I could do the same that I did with the int but with a ElapsedTimer.
Sorry if I wasn’t clear initially.
tezza
September 16, 2015, 6:34am
9
Stands to reason that if you call a subroutine that is set as void, you cant expect to receive a value back from it.therefore the preprocessor naturally will complain that the function was not declared.
In C++ a void function does not return a value so you can not call it to return a value.
ScruffR
September 16, 2015, 1:10pm
10
As you stated above
I'd go with @peekay123 's suggestion.
But as for my proposal, this builds for my
#pragma SPARK_NO_PREPROCESSOR
#include "application.h"
#include "elapsedMillis/elapsedMillis.h"
elapsedMillis tmr;
volatile int i;
void test(elapsedMillis& x)
{
x += i++;
return;
}
void setup() { }
void loop()
{
i++;
test(tmr);
}
1 Like
Are you using the WebIDE? I get a bunch of errors with the code above.
ScruffR
September 16, 2015, 1:23pm
12
Yes, I am - have you copy-pasted my code?
The error there looks like you have not added #include "application.h"
which is required when using #pragma SPARK_NO_PREPORCESSOR
1 Like
Sorry I got it. I had #include “elapsedMillis/elapsedMillis.h” on the very top. Had to have it in this sequence:
#pragma SPARK_NO_PREPROCESSOR
#include "application.h"
#include "elapsedMillis/elapsedMillis.h"
Thank you! This will work!
3 Likes