I’m coming as background from Intel assembly language (all series from 4004 to 486) I feel the need of using subroutines. Can you, please, point me to some reading to be able to do this in the Arduino world? I want to build a PWM driven power supply, looking to use the same Nano for PWM, voltage and current reading and display, current control.
I learned assembler as a high level language after machine code. Things are sure different now.
I think there is a tutorial on program organization. There are two ways but I feel only one makes sense. The first and not desirable in my opinion is to just create multiple .ino files. The superiour way is one .ino. several .cpp and (not sure) one .h or maybe several. The only time I did this I had one .h, 1 .ino, 1 .cpp but with several procedures/functions. I loosly call them 'helper functions'. In a pure class world (I am not cpp trained) I think these are probably private members.
Here I already have problems, how to create .cpp files. Next how to me the cpp files on demand insde the .ino file. After running a cpp file inside the ino does the control return to the next line in the ino file?
Hello again @mikemarcu. Although the information @sonofcy provided can be useful once you reach a more advanced stage in your Arduino learning journey, from your response I think it will be best to avoid introducing the unnecessary complexity of a multi-file sketch for now.
The first thing you need to understand is that the correct term in this context is "function", not "subroutine". Another thing to know is that the "Arduino programming language" used in .ino files is actually just a superset of C++. The Arduino sketch build system does a bit of minor preprocessing on the code from the .ino file before compiling it using an industry standard C++ compiler (GCC). Knowing the correct term as well as the fact that information about the C++ language is also applicable to Arduino sketches will allow you to effectively perform research on the subject.
Although it can be convenient to organize them into separate source files when you are creating a large sketch project, you can also define functions in the .ino file of your sketch, so there is absolutely no need to create a multi-file sketch in order to use functions in your code.
Here is a simple example of the use of a function in a sketch:
// Definition of a function named `blink`.
void blink() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
blink(); // Call the `blink` function.
}
Are you using Arduino IDE, or VS Code?
I know you mentioned VS Code + PlatformIO earlier, but am not sure what you are using now:
You don't "run a cpp file inside the ino". However you can call functions that are defined in a .cpp file from the code in your .ino files if you like.
The correct thing to ask would be:
does the control return to the next line in the ino file after a function call returns?
The answer is: yes.
If you realize that you almost certainly already have function calls in your existing Arduino sketches, then you will realize that you already know the answer to this. In this respect, there is no difference between a function like digitalWrite defined in the Arduino core and a custom function you define in your sketch.
OK, great. My recommendation from my previous reply regarding sticking with a single .ino file for now still stands, but I'll go ahead and answer your remaining question anyway:
Open the sketch you want to add a file to in Arduino IDE.
Click the ●●● icon at the right side of Arduino IDE's tab bar.
A menu will open.
Select "New Tab" from the menu.
The "Name for new file" dialog will open.
Type the filename of the file you want to add to the sketch in the field in the dialog. If you want to add a C++ file to the sketch, then make sure to add the .cpp extension to the filename (e.g., Foo.cpp).
Click the "OK" button in the "Name for new file" dialog.
The dialog will close and you will see that a new editor tab has been added to the Arduino IDE window. You can then add code to that tab. If the added file has a .cpp file extension, the code in it will be compiled as C++ when compiling the sketch. You don't need to do anything special to cause the file to be compiled.