I am a seasoned programmer, but I am learning Arduino coding for the first time. I am really confused as to how I'm supposed to format a large project with many code files. I'm seeing a lot of mixed responses saying everything should be a .ino file, and others saying only the main file should be a .ino, and everything else should be .cpp and .h files.
I'm not a fan of the standard Arduino IDE, so I'm using Visual Studio Code with the Arduino plugin from Microsoft. I installed the package for the Due, which is the model I have. Right now I have a .ino file for my main file, and a .cpp and corresponding .h file separate. The .h file is included in the other two files so VSCode will give code hints for it.
Now on to my actual issue.
I got the Due as my first Arduino because I saw it was the most powerful and not much more expensive. I'm now aware of the difference in architecture. So I'm not sure if the issues I'm having are just because of the ARM architecture of this board and I'm not doing things the right way, or if it's my environment.
VS Code does not highlight any errors in my code, but I still get the following compiler errors when verifying:
error: 'byte' does not define a type
error: 'malloc' was not declared in this scope
error: 'sin' was not declared in this scope
error: 'PI' was not declared in this scope
I cannot access class members declared in a header file, even though that header file is included.
How different is the standard library on the Due compared to the regular models? Is is really so different that none of these things are available or am I doing something wrong here?
On my complex programs, I use a lot of tabs. Each tab represents a function or group of related functions. For example, I have a tab named setup, a tab named loop, and tabs named wifi, mqtt, ota, and then tabs for code unique to my project.
Every tab is an ino file. The first tab is the project name where, in my case, all declarations, includes and globals go. The IDE compiler aggregates all of the ino files into a single source document.
(It also makes starting new projects easier. For example if I know I am going to use mqtt and ota, I just copy those .ino files from a prior project.)
If you want to make library files from your functions, then those go into a .cpp and .h pair. But I have practically no experience in creating my own library files. I did one to learn, and it worked. I learned that I don't want to do that again.
By the way, the record (that I've seen) for the number of tabs (.ino files) in a single project is 168 in the Tasmota project. (I have to compile my own because my router is ancient and does not support WEP, but that's another story).
I don't know if this is standard, but the compiler reads the .h file when it encounters it. I use this to put my WiFi credentials into an .h file and in my wifi.ino tab, I just do a #include and I have all of my wifi credentials defined.
FYI, In the Arduino IDE, I use one .ino file and as many .cpp and .h files as are appropriate, placed in the project folder so they open as tabs, same as above. I have no idea about VS. Someday I might get motivated and try Eclipse/Sloeber, but my time is limited to what is absolutely necessary right now. I use Eclipse as a code editor for library files, running alongside the Arduino IDE as I develop.
I use Eclipse / Sloeber, but I suspect VS has similar capabilities. The use of appropriate .h and .cpp files facilitates better project organization and code modularity. See My Reply #3 in this thread.
Note that you can also use these same techniques with the Arduino IDE but don't go the "multiple .ino" route. It's next to useless for organization and code modularity. I also don't consider the Arduino IDE usable for serious code development due to features it lacks. If you have any software development experience or have worked in the industry, you'll soon recognize these shortcomings. However, it does excel at helping folks who have never programmed before get going. I may give the "pro" version (also claimed to be Eclipse-based) a try once it gains some maturity.
Thanks for the replies guys. After reading this I think I'm doing everything correct, so my issue has to do with something else.
I am unable to use the "byte" type, "malloc" does not exist for me, and none of the math functions exist. I have my board set to the "Arduino Due (Programming Port)".
aarg:
I suppose you already know, there is a place where those are stored... sin and PI are in math.h 'malloc' is in stdlib.h, byte is in arduino.h.
Alright welp, that works, thanks. I was under the assumption that the Arduino compiler included those things automatically since I don't see any include statements in the documentation.
The compiler does what it's supposed to do, compile. In the Arduino IDE, there is a so-called builder that does a lot of the work of including. But it does not touch your .h and .cpp files that are part of your project.
So yes, you have to include manually; including Arduino.h is usually enough as it includes all other includes that you might need.