NeoSWSerial - how to use my own PCINT?

Hi

In my sketch I have used my own PCINT0
after that I have added NeoSWSerial library but as far as I understand, it overrides all vectors, even if another one is used for serial as result I get compile error.

PINB - is meaning some pins from port B

so please correct me my understanding
if for my PCINT I'm using PINB and for NeoSWSerial PIND then I should just uncomment NEOSWSERIAL_EXTERNAL_PCINT
correct?

and
if for my PCINT I'm using PINB and for NeoSWSerial also PINB (sure not the same pins) then I should uncomment NEOSWSERIAL_EXTERNAL_PCINT and in my PCINT function I should add first line NeoSWSerial::rxISR( PINB);
correct?

please share the code (within </> code tags !) and the error message (also within </> code-tags)

No, if you do that no interrupt handler for NeoSWSerial is defined and it won't work.

This should work.

not true
I setup as I mentioned above
and I get all symbols from Nano board to Serial terminal...
to honest say I cant sent back anything dont know why/how ...Im using putty and it dont want type any symbols in terminal... trying figure out what happens

Please help me understand what is wrong...

#include "UART.h" //
#define Pin11 bit(PB3) //sensor 3
#define Pin10 bit(PB2) //sensor 2
#define Pin9  bit(PB1) //sensor 1
#define Pin8  bit(PB0) //sensor 0
volatile bool flag = 0;
#include "NeoSWSerial.h" //

NeoSWSerial terminal(14, 15); //receivePin, transmitPin

void setup() {
  serial.begin(9600);             //
  terminal.begin(9600);           //
  DDRB  &= ~(Pin11 | Pin10 | Pin9 | Pin8 );
  PORTB &= ~(Pin11 | Pin10 | Pin9 | Pin8 );
  PCMSK0 = 15;
  PCIFR |= bit(PCIF0);
  PCICR |= bit(PCIE0);
  serial << F("serial") << endl;
  terminal.println( F("terminal"));
}

void loop() {
  while ( serial.available() ) {
    terminal.write( serial.read() );
  }
  while ( terminal.available() ) {
    serial.write( terminal.read() );
  }
  if (flag) {
    PCICR |= bit(PCIE0);
    flag = 0;
  }
}

void flag_blinking() {
}

ISR(PCINT0_vect) {
  byte newState = PINB;
  terminal.println(newState);
  flag = 1;
  PCICR &= ~bit(PCIE0);
}

In the NeoSWSerial I have uncomment NEOSWSERIAL_EXTERNAL_PCINT otherwise I get compile error

now I have connected my Nano (CH340) board with Serial-USB(CH340) board as follow

GND - GND
14 - TX
15 - RX

and start arduino
I get welcome message on both, from setup()
Now, when I type some text "how are you" on com12 (serial) and send it, I can receive it on com7 (terminal) but... I want type on terminal "ok"..
I press on terminal "o" just one key w/o enter, immediately in the terminal I get "terminal" and on serial I get "ssserial"
I press on terminal "k" just one key w/o enter, immediately in the terminal I get "terminal" and on serial I get "ssserial"
I press on terminal "enter" just one key, immediately in the terminal I get "terminal" and on serial I get "ssserial"
I press on terminal "space" just one key, immediately in the terminal I get "terminal" and on serial I get "ssserial"
when I type any key on terminal I get the same message on both

what is wrong ? is it putty bug?

I tried different pins (from port B/D/C) - the same behavior
I tried to add NeoSWSerial::rxISR(PINC); in my ISR - the same behavior

any body know how to work with NeoSWSerial ? :frowning:

i guess you did illustrate the issue with a complete code, but i am still wondering why.
I see no purpose, and the one of the main things about any swSerial is that it is provide as a possible solution to the lack of UARTs. You have on the other hand changed it into a problem. You have not shown that you need the vector elsewhere, and why you need two Serial connections.

About the issue at hand, i think you need to create your own ISR within the class, or at least call the ISR from yours.

  // visible only so the ISRs can call it...
  static void rxISR( uint8_t port_input_register );

  //#define NEOSWSERIAL_EXTERNAL_PCINT // uncomment to use your own PCINT ISRs
};

and i think you need to attach it through the member function

  void attachInterrupt( isr_t fn );
  void detachInterrupt() { attachInterrupt( (isr_t) NULL ); };

just commenting out that statement is not enough.

But as before, if swSerial is a solution... Great !
if it is a problem, don't use it. Choose a different board or look for a different solution.
swSerial takes up a lot of resources, and many limitations.

thank you sir but I need both serial on 328p, and according to dev_1 it should work flawlessly :slight_smile:

pls read post#6.
no difference with calling rxISR from my ISR, or w/o calling it - behavior the same in both cases.

and how can putting my ISR inside the class help me?

because ? and why in combination with PinChangeInterrupts ?
If it can be done according to that post, then why doesn't it work ? Well the OP never told us i guess.

question is not why I need pcint. question is how to setup/make NeoSWSerial is working parallel with my pcint ?

If you want to make an XY issue out of it that's fine, but i am not here to solve puzzles without a point.
I can be her to help people solve their issue, but i am afraid your issue is not with coding at all.

Hi all, after 2 days of experiment, I have noticed that impossible to use my own PCINT and Rx pin in NeoSW

first of all I tried connect Micro (USB+HWSerial) and Nano (HWSerial+SWSerial) just as transmitters.
Micro code

  while ( Serial.available() ) {
    Serial1.write( Serial.read() );
  }
  while ( Serial1.available() ) {
    Serial.write( Serial1.read() );
  }

Nano code

NeoSWSerial terminal(6, 7); //receivePin, transmitPin
  while ( serial.available() ) {
    terminal.write( serial.read() );
  }
  while ( terminal.available() ) {
    serial.write( terminal.read() );
  }

Paired as

Gnd - Gnd
Tx - 6
Rx - 7

and its working flawlessly.

But as said author in the comment here if I want my ISR I should did that and it will work. Ok I did it

Nano code

#include "UART.h" //
#include "NeoSWSerial.h" //
NeoSWSerial terminal(6, 7); //receivePin, transmitPin

void setup() {
  serial.begin(9600);             //
  terminal.begin(9600);           //
  PCMSK0 = 15;
  PCIFR |= bit(PCIF0);
  PCICR |= bit(PCIE0);
  serial << F("serial") << endl;
  terminal.println( F("terminal"));
}

void loop() {
  while ( serial.available() ) {
    terminal.write( serial.read() );
  }
  while ( terminal.available() ) {
    serial.write( terminal.read() );
  }
}

ISR(PCINT0_vect) {
  NeoSWSerial::rxISR(PINB);
  byte newState = PINB;
  terminal.println(newState);
}

and when I send text from Nano to Micro is working. But when to Nano only some letters and looks like board is resetting.

So, is the problem in my code or in the NeoSWSerial library?

Pins 6 and 7 are on PIND.

thank you fixed but the same

serial
sesssserial
sessssssserial
sesssssssssssssssssssssssserial

so looks text on nano , when I sent it from micro to nano (NeoSwSerial Rx pin). but text was "hi people" "hello my friend !!!" etc.... the larger the text, the longer it gets, but as 'ssssssssserial'

Why are you printing to the software serial port inside the interrupt for the software serial port?

I think you may be missing something important, if you are detecting multiple pin change interrupts on the same port as NeoSWSerial, then you only call the rxISR when the interrupt was caused by the pin being used by NeoSWSerial.

In reference to my previous comment about printing within the ISR, you are going to quickly overflow the Tx buffer if you print the input value from the port for every level transition on the Rx pin.

Do not cross post.

@alexblade ,

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

I want to close this topic but it say I have no rights...
Please , could you close this one and open latest (with correct statement and examples) - https://forum.arduino.cc/t/neoswserial-despite-the-authors-statement-it-is-impossible-to-use-your-own-interrupts/1060967/2

Hello Alex,

I merged the 2 topics as the second one is clearly a continuation of the first one. Anyone wishing to help you can read the whole thing for context. If you have a new topic then someone trying to help might not be aware of the original and waste their time making comments that have already been made.

So no, you don't get to have a new topic on the same subject. Please don't start one, if you do it will get added to the end of this one and you'll get a warning and possibly be prevented from posting in the forum.

Thank you.