stoduk / PingSerial

Arduino library for using serial enabled ultrasonic distance modules (eg. US-100)
MIT License
16 stars 10 forks source link

using a SoftwareSerial mySerial(2, 3); <in> PingSerial us100(mySerial, 50, 400); #2

Closed TalkingMonkeyWithComputer closed 7 years ago

TalkingMonkeyWithComputer commented 7 years ago

HI stoduk,

  1. thx for excellent library - works perfect when using a hardware serial port, as in your examples

    include

    include

    PingSerial us100(Serial, 50, 1200);

  2. Pls advise how to use a SoftwareSerial port to initialise PingSerial 2.1 Seems you have a already thought of this - as I understand you have made initialization available in PingSerial.h

    ifdef PS_INCLUDE_SOFTWARESERIAL

    PingSerial(SoftwareSerial& serialport, uint16_t min_mm_distance = 0, uint16_t max_mm_distance = 500); PingSerial(byte rx_pin, byte tx_pin, uint16_t min_mm_distance = 0, uint16_t max_mm_distance = 500);

    endif

2.2 OK so I naively used the following code snippet -

define PS_INCLUDE_SOFTWARESERIAL

include

include

SoftwareSerial mySerial(2, 3);
PingSerial us100(mySerial, 50, 400);

2.3 I get a lnking error undefined reference to `PingSerial::PingSerial(SoftwareSerial&, unsigned int, unsigned int)' collect2: error: ld returned 1 exit status

2.4 Sure I've made some stupid error - due to my ignorance in programming - pls help!! thx V (PS using an arduino uno)

stoduk commented 7 years ago

It has been many moonssince I looked at this, so I'm rusty! I remember testing it with softwareserial, but I don't know if that was before or after I made it a proper library.

Arduino is a bit of a hack - it doesn't have the usual C toolchain or process, so sometimes things behave funny.

What version is your Arduino IDE?

Can you try adding a #define PS_INCLUDE_SOFTWARESERIAL 1 in PinSerial.h before the #ifdef check you spotted? That gets things to compile for me (though only if I include PingSerial before SoftwareSerial (otherwise we lose the definition of UINT16_MAX, suggesting SoftwareSerial.h is breaking it some how).

TalkingMonkeyWithComputer commented 7 years ago

Thx stoduk

  1. OS: archlinux & package: community/arduino 1:1.8.0-3
  2. Awesome!! adding the #define in header file did it - as already had the order of #include as you prescribed Thx very much! V
stoduk commented 7 years ago

Thinking more, the Arduino behaviour is probably as expected. A #define in an .ino file (which eventually becomes a .cpp file, with some munging) probably isn't going to affect the .cpp file in a library. We'd need magic in Makefiles or similar to pass the right flags to the library when it is compiled.

So I think the hack I told you is actually the best option - luckily it worked :)

I think I added the selective inclusion of SoftwareSerial because of a real Arduino oddity - to use a library that depends on SoftwareSerial.h would require every .ino including that library to include that header file. Which seems odd if you don't want to use a SoftwareSerial port, and might be a memory concern. So I went heavy handed and excluded it, which requires the hackery to re-include it as we've found.

I'll add a comment somewhere that this is how you enable things - unless some internet stranger comes up with a better idea sometime.