The only reason you would use SoftwareSerial is if you don’t need to RX and TX simultaneously and your baud rate is >= 57600 or <= 4800. You are using baud rate 9600, so there’s no reason to use SoftwareSerial.
I have to R/W on one channel and write on the other and don’t have to do it simultaneously.
You could use AltSoftSerial+NeoSWSerial or 2 NeoSWSerial on those 3 (or 4) pins.
However, your sketch shows R/W on one channel and read on the other (not write). Be aware that writing on the NeoSWSerial channel prevents reading and writing on the other channel:
* AltSoftSerial and NeoSWSerial can read at the same time.
* AltSoftSerial can R/W at the same time (NeoSWSerial can read, too).
* NeoSWSerial can R/W at the same time, but
* AltSoftSerial cannot do anything while NeoSWSerial is writing.
If that is acceptable, here is your sketch, modified to use AltSoftSerial and NeoSWSerial:
#include <NeoSWSerial.h>
#include <AltSoftSerial.h>
NeoSWSerial s1(10, 11); // RX, TX
AltSoftSerial s2; // RX 8, TX 9 *ONLY*
const int ledpin = 13; // On board LED
int blinking = 0; // 6 = on, 5 = off, 4 = on, ... 1 = off, 0 = not blinking
unsigned long lastBlink;
void setup()
{
pinMode( ledpin, OUTPUT );
Serial.begin(9600);
s1 .begin(9600);
s2 .begin(9600);
Serial.println("Ready");
}
void loop()
{
// Read, forward and echo chars from s1
while (s1.available()) {
char c = s1.read();
Serial.print( c );
s2.print( c );
if (blinking == 0) {
// Start blinking
lastBlink = millis();
digitalWrite(ledpin, HIGH); // turn ON the LED
}
// Reset the blink state to 5 or 6
while (blinking <= 4)
blinking += 2;
}
// Time to toggle the LED?
if (blinking && (millis() - lastBlink > 250)) {
blinking--;
if (blinking) {
lastBlink = millis();
digitalWrite( ledpin, !digitalRead( ledpin ) ); // toggle LED
}
}
// Read and echo chars from s2
while (s2.available()) {
char c = s2.read();
Serial.print( c );
}
}
Notes:
* s1
is NeoSWSerial, because you never write to s1
.
* It does not use the String
class. Each character is processed by itself.
* It does not println each character. It just prints each character (no newline). You need to be careful about printing too much. Using println means that you try to print two characters (c + newline) for every character you receive (c). And doing the same thing from s2 could double the problem. Your program could spend most of its time waiting for Serial.print to complete. This will eventually cause s1/s2 characters to be lost (not read) because their input buffers overflow.
* It does not use delay
to blink the LED. Instead, a blink “state” variable and a timestamp from millis() is used. This is the “Blink without delay” or “How to do multiple things at a time” technique. This lets loop run as fast as possible, checking for characters from either port, echoing them to Serial (careful!) and forwarding from s1 to s2.
Cheers,
/dev