Adding function

hello,
i created a function that i'd like to use in all of my projects.
how can i achive that without copy/pasting the function in all projects/scetches?

do i have to write a library (https://www.arduino.cc/en/Hacking/LibraryTutorial) if i want to use my own function in all projects?
or there is a different way ?

thank you

I use a library way for this purpose.

AZZ:
do i have to write a library?

Yes. After all, you have to write it somewhere. Best place for it is in the 'libraries' folder, so the IDE can 'see' it.

I'm quite happy to copy and paste. For the few times I need to it is not worth the trouble of making a library. And usually, even though most of the function code is OK I probably need to change it a little bit.

If I put my code in a central library I could not change it without risking the failure of some other programs that refer to the same library.

You could put the function in its own .ino file and just copy that file into each of your projects.

...R

Robin2:
I'm quite happy to copy and paste. For the few times I need to it is not worth the trouble of making a library. And usually, even though most of the function code is OK I probably need to change it a little bit.

If I put my code in a central library I could not change it without risking the failure of some other programs that refer to the same library.

You could put the function in its own .ino file and just copy that file into each of your projects.

...R

That's the best answer.

I only responded in the way I did because of this: "how can i achive that without copy/pasting the function in all projects/scetches?"

If I put my code in a central library I could not change it without risking the failure of some other programs that refer to the same library.

True, but if the library is a "black box" that achieves a certain, documented, purpose, that should not happen.

Great. great. Thank you Gents!

if i go with library path, do i have to implement the function as a class?
it seemes like an overkill . At least at this point. Maybe when(if) i'll decide to expend the functionality of my lib, then it will be warranted to go with the complexities of the class implementation.
All right, it's not that complex :).
I guess the actual question is - can (and is this the recommended way) the lib be implemented "C" way - i.e without cpp classes?

This

AZZ:
Great. great. Thank you Gents!

if i go with library path, do i have to implement the function as a class?
it seemes like an overkill . At least at this point. Maybe when(if) i'll decide to expend the functionality of my lib, then it will be warranted to go with the complexities of the class implementation.
All right, it's not that complex :).
I guess the actual question is - can (and is this the recommended way) the lib be implemented "C" way - i.e without cpp classes?

This is one of those questions that might best be answered by just trying it, you might teach your self something and get an answer sooner.

:slight_smile: you are right.
At this point im puzzled with c vs cpp usage in arduino.
I thought that i can write in C, but then the lib tutorial uses cpp for libs.

so which way is the right way?

// yes, i'd rather prefer C then cpp :slight_smile:

AZZ:
:slight_smile: you are right.
At this point im puzzled with c vs cpp usage in arduino.
I thought that i can write in C, but then the lib tutorial uses cpp for libs.

so which way is the right way?

// yes, i'd rather prefer C then cpp :slight_smile:

c++ doesn't 'require' classes. Your functions do not need to be wrapped in a class.

And if you do a search in your 'libraries' folder for *.c files, you'll see heaps. "Ping.c", for example. Not all Arduino libraries are C++.

Just write your code in a *.h and *.c file, add a "keywords.txt" file if you want highlighting, and see how you go.
That's pretty much what Keith was saying.

great. thank you i'll be looking into that tonight.