Passing variables in function I have defined(test), works as expected, but when I put it in to the Ticker function, it does not work.
Have not found an answer this far, and all my ideas to make this work, have failed.
#include <Ticker.h>
Ticker timer;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//
}
Error: invalid use of void expression
void loop() {
// put your main code here, to run repeatedly:
//test(1,2);
timer.attach (2000, test(1,2));
}
void test(int c, int d)
{
Serial.println(c+d);
}
Look at the examples with the library. You need to provide a callback function with optional argument when you attach. You're calling your test function in the attach where you should be passing a pointer to it. Also, your test function can only have a single argument, not two.
As a separate issue, you have a callback function with parameters. I'm not certain how to do that as I haven't studied the library and how it sets up the callback functions.
Using ticker.h instead of basic millis() timers has added a layer of complexity.
WemosAlaPlaya:
But this works fine when I call function without timer!?
Sure it does, but the library expects a function with no more than a single parameter. That parameter could be a pointer to a struct with two fields though.
WemosAlaPlaya:
Fading the led stripe in or out, with given speed. Two arguments that is.
What are those arguments?
As Cattledog says, it would probably be easier to do this without the ticker library. Apparently neither he nor I has used it, so I suspect that you would get better assistance just using millis.
I am not aware of other libraries with the same name. I think that the OP has wandered onto swampy ground, and is stuck for a variety of reasons. Wrong library syntax, and use of a callback function with parameters without using a function pointer.
Yes, I have come across at least two "Ticker" libraries, which is more than a bit confusing.
As I know, one works only in Arduino (clone) board, when other "Ticker" in ESP8266.
This same thing is also with BH1750 libraries. All of them have same name, but some of them work with me, some don't.
So the actual version of test function fades an LED strip in or out. Are the parameters it takes the pin number of the strip and whether to fade in or out?
How do you envisage using the ticker functionality to use test?
I do know milliseconds, but my thought was that with Ticker I do not have to worry about roll overs and restart, etc.
With making function that would be for my needs, takes some time. That does not matter, but why write a code when it's already done? Or have I missed something here.
What I'm poking at is how you will use the ticker(s). If you pass the callback function as a lambda as shown earlier, you will always call test with parameters 1,2. If that's ok, you're done. But I suspect it isn't. Maybe it would be enough to call another function that calls test and has some extra logic to determine what parameters to pass it.
In any event, it seems that Ticker really doesn't do much for you in this scenario. YMMV.
Try this for multiple arguments to your callback function (compiles but untested):
I tried to run this on a Wemos D1 mini, but saw no output.
This is what I managed on a UNO with the syntax of the avr library and the lamda function for the callback. I have done versions which pass the arguments by value and by reference following gfvalvo's model.
cattledog:
I tried to run this on a Wemos D1 mini, but saw no output.
Hmmm ... perhaps it was the Serial.println() in the callback? The ESP version doesn't declare an update() function, so I'm guessing the callback runs in an interrupt context. Anyway, all my ESP's are otherwise occupied right now so I can't test. I'm sure I could make it work after banging on it a little while.