-1

I have only one pin available and I'd need to use it both for a digitalRead and for sending serial messages (assigning it the Tx role within a SoftwareSerial). I can alternate these two operations, but I don't seem to succeed. I could be interested in other pairs as well (digitalRead+Rx, digitalWrite+Tx and digitalWrite+Rx) and I hope that understanding one will solve them all, but for now I'd be happy to get the first one working. My first naive attempt would be something like:

 #include <SoftwareSerial.h>

SoftwareSerial swSerial(7,6); int val;

void setup() {

pinMode(6, INPUT_PULLUP);

}

void loop() {

val = digitalRead(6);

delay(2000);

swSerial.begin(19200); swSerial.print("myData"); swSerial.end();

delay(2000);

}

But I've made other experiments, for example with a digitalWrite, hoping that combining two forms of output would be feasible, but again no way. I know I can alternate a pin role between output and input as long as common digital read/write operations are involved, but SoftwareSerial appears to be less flexible. It seems to me it refuses to work as soon as the pin is declared/used for common digital purposes. Is there a way to accomplish my goal?

I guess that similar requirements and fixes (make a pin role more flexible) led to the Software Serial with Half Duplex library, but I'd refrain from adopting it because I feel it is overcomplicated and seems like overkill for my purpose, moreover it doesn't seem actively supported.

To avoid a XY problem, my very initial need (I may have further ones, requiring the different pairs mentioned at the beginning) would be to check whether this pin is floating or connected to something (I can chose between Vcc or Gnd, setting up the pull-up or an external pull-down accordingly) and to send back some information (obviously this is useful if the pin is actually connected, but I have no reason not to send data anyway) alternating the tasks a few times per second at least.

lesath82
  • 101
  • 4

1 Answers1

2

First of all, for using the Software Serial you need to set your tx and rx pins to OUTPUT, that what the library does under the hood.

What you may try to do is to set a millis timer (forget about delay(), never ever use it) and change pin mode to INPUT once every 200ms let's say. And after your measurements put it back to output as soon as you can.

200ms delay is normally ok for some readings like button click. If you can read the value more rare do it (for example you may read some sensor value only once per hour). Here is an example:

    #include <SoftwareSerial.h>

SoftwareSerial swSerial(7,6); int val;

byte timer = 200; // Read value every 200ms uint32_t prevTime = 0;

void setup() {

// Don't change pin mode here
swSerial.begin(19200);

}

void loop() {

if (millis() - prevTime &gt; timer) {
    // you could try swSerial.flush(); here...
    prevTime = millis(); // reset timer
    pinMode(6, INPUT_PULLUP); // changing pin mode and reading value
    val = digitalRead(6);
    pinMode(6, OUTPUT); // changing pin mode back
}

swSerial.print(&quot;myData&quot;);
// swSerial.end(); // I don't think you need this line;

}

It may be a solution from a programming point of view. But another task is to organize an electric multiplexing for 2 different devices on the same pin as was mentioned by @the busybee