Hi there. if i created a function like the following and i wanted to create multiple instances of it i believe i would have to create a class for it. is there any way that i could instate an object without having to set up the constructor and also execute the methods separately? so i could just say for example printTimed(time,data); throughout my code? i am thinking that perhaps this might have something to do with making ones own library if it is possible at all. thanks.
void printTimed(int timingVal, int data){
static unsigned long timer = millis();
if (millis() - timer > timingVal){
timer = millis();
Serial.println(data);
}
}
is there any way that i could instate an object without having to set up the constructor
You don't NEED to define a constructor. If you don't, the compiler will create one for you that does nothing.
and also execute the methods separately?
I don't understand this part. Separate from what?
so i could just say for example printTimed(time,data); throughout my code?
If you make printTimed() a class method, you need an instance to call the method on. That is how the compiler knows WHICH printTimed() function to call.
PaulS:
You don't think we need to see the code that doesn't work to tell you why it doesn't work?
You have more confidence in us that I do.
the other code i tried without a constructor other than in the method call. works as if i was calling a single function. (only data from one instance is displayed)
Should timer be a member variable of your class? Is that what you're missing?
Note that Delta_G is asking about making that a class member, not a variable local to the function.
The reason for making it static, in the function, was to avoid making it global AND to keeo it from being re-initialized every time the function was called. Making it a member variable keeps it from being global and keeps it from being reset each time the function is called.
Though you will need to determine an appropriate point to initialize/reinitialize it. The initialization could be done in the constructor you didn't want to create, but it really should be done in a begin() method, so that you have control over when the timing starts.
Delta_G:
If timingVal and data are coming in with the function call then you don't need member variables to hold that information. This is a waste of four bytes per instance. Only timer really needs to be a member here unless you plan to send in timingVal and data in a constructor and not in the run() method.
right you are, thanks for that! i would also very much like to know whether its possible to give the method arbitrary data types so that the class is more versatile so i could print a string for example or would i be better off just creating another method do you think?
Delta_G:
The search term you want is "function template"
Hi ive just been researching them and have yet to get them to work. i have put together some code as i thought it should be but it does not compile. there are 2 functions. one 'normal one' and one which uses a template. the normal one works as it should but the template version does not. would it be possible to point me in the right direction please? thanks.
void myfunc(int x, String y) { //normal function
Serial.println(x);
Serial.println(y);
}
template <typename type> void type myfunc_template( type x, type y) { //template function
Serial.println(x);
Serial.println(y);
}
void setup() {
Serial.begin(9600);
// myfunc(5, "hi");
myfunc_template(5,"hi");
}
void loop() {
}
template <typename type, typename type2> type myfunc_template( type x , type2 y) { //template function, two data types
Serial.println(x);
Serial.println(y);
}
void setup() {
Serial.begin(9600);
myfunc_template(5, "hi");
}
void loop() {
}
the bit which is confusing me now is that i originally tried declaring the function as void but this didnt work. now i have used the 'type' name it does work. does it matter which of the typenames i use as the functions output data type?
template <class T1, class T2> void templateMethod(T1 arg1, T2 arg2)
{
Serial.println(arg1);
Serial.println(arg2);
}
void loop()
{
//You need to tell the compiler about the wanted data types with <X, Y>
templateMethod<char*, byte>("cstring", someByte);
}