Ok, it looks like a lazy question but I really searched about this. I've found the arduino post explaining how to write a library, but I don't wanna do this, I just wanna make a project with an external class, written in a file called analogMultiplexer.cpp with the heder analogMultiplexer.h
I don't know why arduino does not have a tutorial about this, and I couldn't find good explanations about arduino classes. I've searched for 'arduino class' thinking this would give some easy results but it looks like a 'hidden technique' or something like this.
Well, I'm trying to write this simple code, but when I try to compile it says that I have multiple functions definitions:
I don't know why, since I took care of the multiple includes problem and only declared the functions in the .h file and defined it one time in the .cpp file.
Could somebody explain what's happening here?
But I don't wanna people that open my project, to need to 'add library' to the arduino IDE. I just want them to open the project and compile with, with no 'library installation'
analogMultiplexer\analogMultiplexer.cpp.o: In function analogMultiplexer::Read(int)': C:\Users\lucas\Documents\Arduino\libraries\analogMultiplexer/analogMultiplexer.cpp:21: multiple definition of analogMultiplexer::Read(int)'
analogMultiplexer.cpp.o:C:\Users\lucas\AppData\Local\Temp\build2292162650678407849.tmp/analogMultiplexer.cpp:21: first defined here
analogMultiplexer\analogMultiplexer.cpp.o: In function analogMultiplexer': C:\Users\lucas\Documents\Arduino\libraries\analogMultiplexer/analogMultiplexer.cpp:11: multiple definition of analogMultiplexer::analogMultiplexer(int, int, int, int, int)'
analogMultiplexer.cpp.o:C:\Users\lucas\AppData\Local\Temp\build2292162650678407849.tmp/analogMultiplexer.cpp:11: first defined here
analogMultiplexer\analogMultiplexer.cpp.o: In function analogMultiplexer': C:\Users\lucas\Documents\Arduino\libraries\analogMultiplexer/analogMultiplexer.cpp:11: multiple definition of analogMultiplexer::analogMultiplexer(int, int, int, int, int)'
analogMultiplexer.cpp.o:C:\Users\lucas\AppData\Local\Temp\build2292162650678407849.tmp/analogMultiplexer.cpp:11: first defined here
But I don't wanna people that open my project, to need to 'add library' to the arduino IDE. I just want them to open the project and compile with, with no 'library installation'
Where the code lives doesn't affect whether it is a library or not.
I went to your link again, and downloaded the zip file. I created a folder called zanella in my sketch folder. I copied the three files there. The IDE didn't like that the sketch name didn't match the folder name (idiots!), do it created a new folder. I moved the other two files into that folder.
Then, when the IDE opens the sketch, I see three tabs. When I Verify, I get just one error:
Binary sketch size: 840 bytes (of a 30,720 byte maximum)
Somewhere, then, you have two copies of the source code - the second is probably in the libraries folder. Since I do not have two copied, the code compiles for me.
Thanks. It worked now. I have only one more question, if you could help me...
I don't know why the pinMode is useless. I think that when i create the object, it should declare the pins as output. Only when I create it. So it should work.
I think that when i create the object, it should declare the pins as output. Only when I create it. So it should work.
Common misconception. There is hardware initialization code called for you by the program that the IDE adds your sketch to. Your constructors are called before that init function is called so they will not be effective if you're doing something that relies on the chip being in an appropriate state - such as pinMode.
This problem is often solved by having a Begin function in the class to do the initializing in setup e.g. Serial.begin.