Not receiving 2 from Arduino-Arduino write/read, why?

// Send
#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 11); // RX, TX


void setup() {
  Serial.begin(19200);
  mySerial.begin(9600);
  delay(500);

}

void loop()
{
  mySerial.write(2);
  delay(1000);
}
// Recieve
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 7); // RX, TX

int integerValue = 0;

void setup() {

  Serial.begin(19200);
  mySerial.begin(9600);
  delay(500);

  if (mySerial.available() > 0)
  {
     integerValue = mySerial.read();
  }
  Serial.println(integerValue);
}



void loop() {}

I just get zero or a few zeros on the serial monitor. What is wrong with either sketch? I would like to send numbers.

since you're checking for serial input, just once in setup, what happens if there's not serial data at that time? what gets printed if there's no serial data

gcjr:
since you're checking for serial input, just once in setup, what happens if there's not serial data at that time? what gets printed if there's no serial data

Even when it's put into void loop(), the same thing happens (zero indefinitely).

adamelli:
Even when it's put into void loop(), the same thing happens (zero indefinitely).

What are pins 7 and 11 connected to? Maybe a wiring diagram would help us.

Serial monitor 19200 baud.

Both Arduinos connected on pin 11:

adamelli:
Both Arduinos connected on pin 11.

Serial monitor 19200 baud.

Do you have their grounds connected?

ToddL1962:
Do you have their grounds connected?

Yes, but they're connected through the USB ports ground anyway.

adamelli:
Even when it's put into void loop(), the same thing happens (zero indefinitely).

but it looks like loop() doesn't do anything

Your receiver reads ONE character, which might well be garbage, then prints that value. It never again even attempts to read the Serial port.

I put the read method into mySerial.available. Nothing is printing. Why is mySerial never available?

// Recieve
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 7); // RX, TX

int integerValue = 0;

void setup()
{
  Serial.begin(19200);
  mySerial.begin(9600);
  delay(500);
}



void loop()
{
  if (mySerial.available() > 0)
  {
     integerValue = mySerial.read();
     Serial.println(integerValue);
  }
}

Here is the sender sketch again:

// Send
#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 11); // RX, TX


void setup() {
  Serial.begin(19200);
  mySerial.begin(9600);
  delay(500);

}

void loop()
{
  mySerial.write(2);
  delay(1000);
}

does the tx pin need to be configured as an OUTPUT?
see SoftwareSerial:

gcjr:
does the tx pin need to be configured as an OUTPUT?
see SoftwareSerial:

Ough, thanks for the suggestion! Although, I tried that just now, and it did not make a difference. I'm keeping it in though. I think that's the correct way from the link.

OK, it works now.

Both the sending and receiving Arduino needed pinMode defined:

// Send
  pinMode(11, OUTPUT);

// --/--

// Recieve
  pinMode(11, INPUT);

That's confusing how input is not default pinMode.

Both the sending and receiving Arduino needed pinMode defined:

This makes no sense given the source code for SoftwareSerial.h. I see both pins set to the correct pinMode in the library for the AVR boards like the UNO, Nano, etc.

What Arduino are you using, and what IDE version.

Here's what I see for the constructor

// Constructor
//
SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) : 
  _rx_delay_centering(0),
  _rx_delay_intrabit(0),
  _rx_delay_stopbit(0),
  _tx_delay(0),
  _buffer_overflow(false),
  _inverse_logic(inverse_logic)
{
  setTX(transmitPin);
  setRX(receivePin);
}

And the setTX() and setRX() look like this

void SoftwareSerial::setTX(uint8_t tx)
{
  // First write, then set output. If we do this the other way around,
  // the pin would be output low for a short while before switching to
  // output high. Now, it is input with pullup for a short while, which
  // is fine. With inverse logic, either order is fine.
  digitalWrite(tx, _inverse_logic ? LOW : HIGH);
  pinMode(tx, OUTPUT);
  _transmitBitMask = digitalPinToBitMask(tx);
  uint8_t port = digitalPinToPort(tx);
  _transmitPortRegister = portOutputRegister(port);
}

void SoftwareSerial::setRX(uint8_t rx)
{
  pinMode(rx, INPUT);
  if (!_inverse_logic)
    digitalWrite(rx, HIGH);  // pullup for normal logic!
  _receivePin = rx;
  _receiveBitMask = digitalPinToBitMask(rx);
  uint8_t port = digitalPinToPort(rx);
  _receivePortRegister = portInputRegister(port);
}

Just an Uno and a Mega, nothing fancy. Recent IDE version.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.