What is this? For a library? Just some constants and declarations? The arduino has defined places for all of these. If it is just for your .ino, just put it in the same directory.
dgrat:
I tried to include a header file.
Unfortunately I was not able to find a way
The Arduino does some peculiar mucking around behind the scenes before it compiles your code, so you as the programmer don't control the compilation files and paths in the same way you would for a conventional C++ development environment.
If your .h file is in the same directory as your sketch, and you #include it using double quotes around the file name (no path), then that will work:
#include "foo.h"
If your .h file is not part of a sketch then it needs to be part of an Arduino library in order to be used. An Arduino library consists of a directory under the libraries directory of your Arduino sketch directory (preferred) or under the libraries directory under the Arduino software installation directory. The name of the directory is the name of the library. The directory must contain a file with the same name as the library with a .h extension. You include the library in your sketch by #including the library .h file inside double quotes into your sketch .ino file - just the file name, no path.
// includes the whole SoftwareSerial library in your sketch, as well as including the .h file in this compilation unit
#include "SoftwareSerial.h"
I have a question about that, what if you want to include another library inside your own library, can that be done?
So say I make a library and I want to use a function from someone else's library like SoftwareSerial. Could I get access to SoftwareSerial's functions, and if so how would I go about doing that?
dgrat:
I tried to include a header file.
Unfortunately I was not able to find a way to set include paths in the IDE so I had to do the following:
...
It makes me mad, so where to set paths?
More details are required than one line. Is this inside the .ino file or another file? If another file there is a reasonably well-documented requirement that you also have to include your file in the .ino file, whether it is obvious you have to or not.
HazardsMind:
So say I make a library and I want to use a function from someone else's library like SoftwareSerial. Could I get access to SoftwareSerial's functions, and if so how would I go about doing that?
Why don't you try it and see? However I have certainly written libraries that use Wire.h or SPI.h.
However as I said in the reply above, the main sketch then needs to include those.
It's just a line or two, and that can be part of the library documentation. In fact, it helps in a way to make it clear what libraries your sketch requires (including libraries included by libraries). So, it's a feature!
Why don't you try it and see? However I have certainly written libraries that use Wire.h or SPI.h.
I have tried it but, I just can't get it to work. I don't know what I'm missing. I know the answer is staring me right in the face, but I just don't see it.
I know that's an error, I didn't change it back when I posted it.
Sketch:
#include "CustServo.h"
#include<SoftwareSerial.h>
CustServo myServo;
void setup(){
myServo.Number_of_Channels(4);
delay(100);
myServo.SetBaud(19200);
}
void loop() {
for (int i = 0; i <= 180; i+=10){
myServo.Move(1,i);
delay(100);
}
}
Errors:
SoftwareSerial.begin(baud); => error: expected unqualified-id before '.' token
SoftwareSerial begin(baud); => error: no matching function for call to 'SoftwareSerial::SoftwareSerial(long int&)'
SoftwareSerial::begin(baud); => error: cannot call member function 'void SoftwareSerial::begin(long int)' without object
Like I said, I know the answer is simple but I just don't see it.
Here's a simple way of doing what you are attempting:
CustServo.cpp:
//CustServo.cpp
#include "CustServo.h"
int ServoNum[40];
void CustServo::Number_of_Channels(uint8_t channel)
{
for(int i = 0; i < channel; i++)
{
ServoNum[i] = i + 48;
}
// more channels to be added later
}
void CustServo::Move(uint8_t chan, uint8_t position)
{
port_.write (ServoNum[chan]); // What channel to use
port_.write (map(position,0,180,7,247)); // send position command
}
SoftwareSerial and HardwareSerial are both derived from Stream.
eg.
class SoftwareSerial : public Stream
...
class HardwareSerial : public Stream
Thus it is valid to use Stream in your class instead of either of them. It is Stream that declares read, write, available etc.
Both SoftwareSerial and HardwareSerial override these virtual methods:
virtual int available() = 0;
virtual int read() = 0;
virtual int peek() = 0;
virtual void flush() = 0;
So, a call to port_.write() calls either SoftwareSerial.write() or HardwareSerial.write() as applicable.
By storing a reference to the Stream (the & operator) we can use the one which you instance in your main sketch. So all we have to do is pass down the appropriate instance in the constructor.