Hello,
I finally got around to updating the Arduino IDE and now have 1.6.8.
However, the project I was working on (and which worked thus far) will not compile under 1.6.8
.
The project contains a method of calling functions at timed intervals (for example, every 60 seconds). I submited the gneral concept as
There is a structure which contains, among other things:
struct timed_event
{ ...
void (*function)(); // (pointer to) routine to be called
};
Then, there is an array of that structure:
timed_event timed_events[] =
{
{ ..., *function1 },
{ ..., *function2 }
};
The functions themselves are defined normal.
void function1()
{//does something}
This syntax was working fine in IDE 1.0.6. Now that I have upgraded to 1.6.8, upon testing the script, I receive an error:
error: 'function1' was not declared in this scope
It would appear that either something has changed in the syntax, or that the original syntax was technically incorrect but the 1.0.6 IDE was forgiving enough to allow it and the 1.6.8 is no longer so forgiving.
Anyone have an idea of what I Need to change in order for it to work again?
JaBa:
Hello,
I finally got around to updating the Arduino IDE and now have 1.6.8.
However, the project I was working on (and which worked thus far) will not compile under 1.6.8
...
Anyone have an idea of what I Need to change in order for it to work again?
Thanks in advance.
All you need to make the code compile is to prototype your functions before the variables that use their addresses.
// Prototype functions used to init timed_events[]
void function1();
void function2();
//There is a structure which contains, among other things:
struct timed_event
{
void (*function)(); // (pointer to) routine to be called
};
// Then, there is an array of that structure:
timed_event timed_events[] =
{
{*function1},
{*function2}
};
// The functions themselves are defined normal.
void function1()
{//does something}
This is something you normally need to do in C anyway, so it's not a bad habit to get into. The Arduino environment helps new programmers by not requiring perfect prototyping, but apparently earlier versions were more tolerant than later versions.
Also note that you don't need the *s in your initialization of timed_events.
// This will work with the minimum number of characters.
timed_event timed_events[] =
{
{function1},
{function2}
};
// Given the flexibility of the C language, this will also work by why use more characters if you don't have to?
timed_event timed_events[] =
{
{**************function1},
{****************************function2}
};
[\code]
Thanks BigBobby!
Prototyping before initializing the array worked!
And, of course, you are right that I should have done that in the first place. Because it worked before, I would never have thought in that direction.
I will also admit that of all the languages I program in, C is my weakest and I have never understood those pointers anyway...
Thanks for the solution.