Two Nanos and Serial.read() _ Serial.write()

Hi Everyone.

I connected the Serial pins on two Nanos TX Pin to RX Pin.

The one Nano sends "PROGRAM".

The Receiving Nano only print "PROGRAM" on the LCD if there is a Serial.print funtion on the first line below void loop() it also works even when the Serial cable to my PC is not connected.

When I remove the first Serial.print in void loop() the LCD only shows "P" and 6 blocks.

Thanks.

void setup()
{
  Serial.begin(9600);

  delay(1000);
  Serial.write("PROGRAM");
}

void loop()
{

}

Receiver side.

#include <LiquidCrystal.h>

const unsigned int numReadings = 7;
unsigned int myArray[numReadings];
int counter = 0;
int doneRead = 0;
char myChar = 0;

//                RS  E  D4  D5  D6  D7
LiquidCrystal lcd(A4, A3, 4, 5, 6, 7);
//----------------
void setup()
{
  Serial.begin(9600);

  lcd.begin(20, 4 );
  lcd.setCursor(0, 3);
  lcd.print("LCD_Serial_Read");
  delay(1000);

  //lcd.clear();
}

void loop()
{
  
  Serial.println("start");//Only works if this line is here.

  if (Serial.available())
  {
    Serial.println("serial avail");

    for (unsigned int i = 0; i < numReadings; i++)
    {
      myArray[i] = Serial.read() ;
      Serial.println("serial read");
    }

    lcd.setCursor(0, 0);

    for (unsigned int i = 0; i < numReadings; i++)
    {
      char aaa = ( myArray[i]) ;
      lcd.print (aaa);
      lcd.print(" ");
      Serial.print(aaa);
      counter ++;

      if (counter == 7)
      {
        while (1)
        {}
      }
    }
  }
}

You have no function below loop().

After compilation Serial.print() will get replaced by the Library function.
So why not just call it a function .. a choice of words.

So the Serial.print(); inserts a small delay so the read buffer will catch all the bytes?

I found this.

http://www.gammon.com.au/serial

I have not seen any examples where a small delay is used between Serial.read();

This link explains (briefly) the physical/electrical reason using hardware serial (Uno/Nano Tx/Rx/Pin0/Pin1) will not work.

This link (shortly following the link above) says you should use software serial and shows how this will work.

AND... the following link shows how to use ONE Arduino (the example is using Mega2560) to configure TWO serial ports to do the tx/rx...

https://docs.arduino.cc/tutorials/communication/TwoPortReceive/

1 Like

I still dont get it , is that not what I am doing?

I removed the Serial.print(); in the void loop(); and replace it with delay(100); and this works , " P R O G R A M" is printed on the LCD every time.

I have 2 Nanos RX TX pins cross connected with the Serial USB Cable connected to my PC and "PROGRAM" is printed on the LCD and also in the Serial monitor.

I can however not program the Nano while the RX TX pins is connected , that's why I have 2 header pin that can be removed when programing.

See Photo.

Latest code with delay in first line of void loop();
While(1) stop removed.

Thanks , yes took me way to long to "see" what was wrong.

Thanks for the Link , very interesting.

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