I'm not the most efficient code writer, but I do try to keep things logical.
I have a sketch that was starting to get a bit long and untidy, so I decided to break it down in to a number of functions, each of which I tidied away to it's own tab.
The sketch compiled and uploaded fine, or at least it did. I included a new function, again in it's own tab, but I am now getting error messages for some of the original functions, despite not having made any changes to them.
The error message I am getting is ''l2rWallSlide' was not declared in this scope'. l2rWallSlide is the name of the function, and I am getting the same error message for a lot of the other functions, though not all.
I have double checked that the functions are named correctly, and don't have any upper or lower case characters where they shouldn't be. But the strange thing is that I have not changed any of them since the sketch last compiled successfully so I am at a loss as to why I am getting this error message now.
However, I seemed to have solved the problem. The new function I added actually called a second function, which is what appears to have thrown a spanner in the works.
I have re-edited the sketch to include the sub-function in to the body of the new function and it now compiles fine.
I’m guessing the Arduino environment doesn’t like nested functions.
I'm guessing the Arduino environment doesn't like nested functions.
It does not. You can have multiple function CALLS though, nested as deep as you like.
The IDE combines all of your sketches to produce one cpp file that it passes to the compiler.
The order of the sketches matches the order of the tabs, NOT any logical order. The tab order depends on the name of the file in the tab, so you can impose order by naming the tabs so that they are sewn together in the proper "order".
Or, define your own function prototypes, in the main sketch tab, and then the order won't matter.
The tabs have been named identically to the functions contained within. I have tried to name the functions as descriptively as possible to remind me what each of them does. As a result they are not called in alphabetical order, nor do they appear in alphabetical order across the tab list at the top of the IDE screen.
When you have several .ino files in a project the Arduino IDE first loads the principal project file and then loads the other files in alphabetical order.
Variables defined in a early file can be accessed by code in a file loaded later, but not vice versa. I can’t recall whether that is also true of functions. If it is it may help to put function prototypes in the principal file. For a function like this
void myFunction() {
Serial.println("this is myFunction");
}
Robin2:
I can't recall whether that is also true of functions.
Generally it should not be. The Arduino IDE adds prototypes for all functions in .ino files at the top of the first .ino file. Sometimes function prototype generation doesn't work correctly but I've never seen that be caused by putting files in tabs. It's more likely that a prototype was never correctly generated but it didn't matter before since the function was defined before it was called. After the sketch was split into tabs this was no longer the case and so it exposed the prototype generation failure.
pert:
Generally it should not be. The Arduino IDE adds prototypes for all functions in .ino files at the top of the first .ino file. Sometimes function prototype generation doesn't work correctly but I've never seen that be caused by putting files in tabs. It's more likely that a prototype was never correctly generated but it didn't matter before since the function was defined before it was called. After the sketch was split into tabs this was no longer the case and so it exposed the prototype generation failure.
One can, of course, enable verbose mode during compilation, and look at the resulting cpp file with function prototypes added, and remove all doubt.