NeoSWSerial icon indicating copy to clipboard operation
NeoSWSerial copied to clipboard

NeoSWSerial does not *write* to multiple instances without setting listen()

Open eclab opened this issue 2 years ago • 1 comments

listen() is notionally for reading, as only one software instance can listen at a time.

However, listen() is also required on NeoSWSerial for writing, or else everything you write out will be written out to the last instantiated NeoSWSerial regardless of which NeoSWSerial instance you wrote to.

Example code below. (Note this code also works around another NeoSWSerial error: you must have a valid pin on RX or it won't write out TX properly. You can't add 255 or whatnot).

#include <NeoSWSerial.h>

#define CV_POT_IN1    A2    // CC 1 Value
#define CV_POT_IN2    A1    // CC 2 Value
#define CV_POT3       A0    // CC 3 Value
#define CV_IN3        A3    // PORT 2
#define CV_AUDIO_IN   A4    // PORT 3
#define CV_AUDIO_OUT  11    // MIDI Clock (only when between start/continue and stop)
#define CV_GATE_OUT   8     // PORT 1
#define SERIAL_1_RX    5   // Blank Serial Pin
#define SERIAL_2_RX   6   // Blank Serial Pin
#define SERIAL_3_RX   7   // Blank Serial Pin

NeoSWSerial mySerial1(SERIAL_1_RX, CV_AUDIO_IN);
NeoSWSerial mySerial2(SERIAL_2_RX, CV_IN3);
NeoSWSerial mySerial3(SERIAL_3_RX, CV_GATE_OUT);


void setup() 
{
    mySerial1.begin(31250);
    mySerial2.begin(31250);
    mySerial3.begin(31250);

}

void write(NeoSWSerial* sw, uint8_t a, uint8_t b, uint8_t c)
  {
    // THIS LISTEN IS REQUIRED
    sw->listen();

    sw->write(a);
    sw->write(b);
    sw->write(c);
    }

void loop() {

    write(&mySerial1, 0x90, 60, 127);

  delay(1000);    

    write(&mySerial1, 0x80, 60, 127);
    write(&mySerial2, 0x90, 62, 127);

  delay(1000);    

    write(&mySerial2, 0x80, 62, 127);
    write(&mySerial3, 0x90, 64, 127);

  delay(1000);

    write(&mySerial3, 0x80, 64, 127);
}

eclab avatar Jan 03 '24 15:01 eclab