Pages: [1] 2   Go Down
Author Topic: #include madness  (Read 772 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
#include "C:\Users\dgrat\Documents\Arduino\RPiAPMCopter\RPiAPMCopterDefs.h" // The procreate ?!

It makes me mad, so where to set paths?
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is more or less for constants. I put it into the same dir.
But: #include "foo.h" is not working.
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://forum.arduino.cc/index.php/topic,37371.0.html
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
#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.

Code:
// includes the whole SoftwareSerial library in your sketch, as well as including the .h file in this compilation unit
#include "SoftwareSerial.h"
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Queens, New York
Online Online
Faraday Member
**
Karma: 101
Posts: 3651
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: September 26, 2013, 10:51:45 pm by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18815
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18815
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged


Queens, New York
Online Online
Faraday Member
**
Karma: 101
Posts: 3651
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

.h file
Code:
//CustServo.h
#ifndef CustServo_h
#define CustServo_h

#include <Arduino.h>
#include<SoftwareSerial.h>
class CustServo
{
  public:

     void Number_of_Channels(uint8_t channel = 4);
     void SetBaud(unsigned long baud);
     void SetBaud(long baud, uint8_t TX);
     void Move(uint8_t channel, uint8_t position);
};
#endif

.cpp file
Code:
//CustServo.cpp

#include "CustServo.h"
#include <SoftwareSerial.h>
#include <HardwareSerial.h>

int ServoNum[40];
bool SoftSerial = false;


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::SetBaud(long baud, uint8_t TX)
{
  SoftwareSerial(-1, TX,false);
  begin(baud);
  SoftSerial = true;
}
void CustServo::SetBaud(unsigned long baud)
{
  Serial.begin(baud);
}

void CustServo::Move(uint8_t chan, uint8_t position)
{
  if(SoftSerial){
    // mySerial.write(ServoNum[chan]);              
    // write(map(position,0,180,7,247));
  }
  else {
    Serial.write(ServoNum[chan]);            // What channel to use  
    Serial.write(map(position,0,180,7,247)); // send position command
  }
}
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18815
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What isn't working? (error message).

What is your main sketch?
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18815
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code has errors, for example:

Code:
  begin(baud);

But nothing to do with #include madness, that I can see.
Logged


Queens, New York
Online Online
Faraday Member
**
Karma: 101
Posts: 3651
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know that's an error, I didn't change it back when I posted it.

Sketch:
Code:
#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.
« Last Edit: September 27, 2013, 12:30:23 am by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18815
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's a simple way of doing what you are attempting:

CustServo.cpp:

Code:
//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
}

CustServo.h:

Code:
//CustServo.h
#ifndef CustServo_h
#define CustServo_h

#include <Arduino.h>

class CustServo
{
  Stream & port_;   // generic serial port
  
  public:
  // constructor
  CustServo (Stream & port) : port_ (port) { }
  
  // methods
  void Number_of_Channels(uint8_t channel = 4);
  void Move(uint8_t channel, uint8_t position);
};
#endif

Sketch using SoftwareSerial:

Code:
#include "CustServo.h"
#include <SoftwareSerial.h>

const byte TX = 5;

SoftwareSerial mySerial (-1, TX, false);
CustServo servo (mySerial);

void setup ()
  {
  mySerial.begin (9600);
  }  // end of setup

void loop () { }

Now with HardwareSerial:

Code:
#include "CustServo.h"

CustServo servo (Serial);

void setup ()
  {
  Serial.begin (9600);
  }  // end of setup

void loop () { }
« Last Edit: September 27, 2013, 12:36:40 am by Nick Gammon » Logged


Queens, New York
Online Online
Faraday Member
**
Karma: 101
Posts: 3651
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok I went about it the wrong way. Yours works, but what I don't understand is "Stream & port_; " What is this doing?

Update: Library provided.

* SerialServo.zip (2.19 KB - downloaded 44 times.)
« Last Edit: March 20, 2014, 01:21:24 am by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18815
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

SoftwareSerial and HardwareSerial are both derived from Stream.

eg.

Code:
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:

Code:
    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.
Logged


Pages: [1] 2   Go Up
Jump to: