Porbleme sending data between two arduino

Hello everybody

I’m new using arduino and im pretty motivated .

I have probleme sending data and receiving it between arduino Uno and arduino Nano

i Use PROTEUS isis ; i use the tempature sensor LM35 !!

I had no problem displaying the temperature in the virtual terminale but my probleme is how to send this information from the first arduino with the Tx/RX serial module, and How to receive it from the other side and display it on LCD .

This is the code , would you please correct it to me ?

receiver :

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int Temperature;

void setup() {

Serial.begin(9600);

lcd.begin(16, 2);

lcd.print(“Wainting for data”);

delay(2000);

lcd.clear();

}

void loop() {

// send data only when you receive data:

if (Serial.available() > 0) {

Temperature=Serial.read();{

lcd.setCursor(0, 1);

lcd.print(Temperature);

}

}

}==============================================================================================

sender :

#define TempPin A0

int TempValue;

void setup()

{

Serial.begin(9600); // Initializing Serial Port

}

void loop()

{

TempValue = analogRead(TempPin); // Getting LM35 value and saving it in variable

int TempCe = ( TempValue/1024.0)*500; // Getting the celsius value from 10 bit analog value

float TempFarh = (TempCe*9)/5 + 32; // Converting Celsius into Fahrenhiet

Serial.print("TEMPERATURE in Celsius = "); //Displaying temperature in Celsius

Serial.print(TempCe);

Serial.print(“*C”);

Serial.print(" | ");

Serial.print("TEMPRATURE = "); // Displaying Temperature in Fahrenheit

Serial.print(TempFarh);

Serial.print(“*F”);

Serial.println();

delay(1000);

{

if (Serial.available()) {

Serial.write(TempCe);

}

}

}

It’s not quite clear what the exact problem is. Do you receive anything or nothing at all?

If you do not receive anything, how did you connect the two? Hopefully you connected the TX of the sender to the RX of the receiver and the RX of the sender to the TX of the receiver.

If you do receive something, but it only displays ‘one character at a time’, the problem is in your receiver loop. Every time that you receive a character, you set the LCD to position 0,1. So you receive the ‘T’ of the word ‘Temperature’ and display that on the LCD, next you receive the ‘e’ (the second letter of the word ‘Temperature’) and display that at the same position (overwriting the T) and so on.

So you need to collect your data first by placing it in a buffer and once you know that the data is complete (a test for ‘\n’ should do), you can display it. The below demonstrates it, and you can test it with the serial monitor first (virtual terminal?).

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int Temperature;

char serialbuffer[64];
char serialposition = 0;

void setup() {

  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Wainting for data");
  memset(serialbuffer, 0, sizeof(serialbuffer));
  delay(2000);
  lcd.clear();

}
void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read character from serial port
    serialbuffer[serialposition] = Serial.read();
    // display at first position of lcd for debugging purposes
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(serialbuffer[serialposition]);
    // if it's LF
    if (serialbuffer[serialposition] == '\n')
    {
      // replace LF by null character
      serialbuffer[serialposition] = '\0';
      // display received character string on second line of lcd
      lcd.setCursor(0, 1);
      lcd.print(serialbuffer);
      // clear buffer and reset position
      memset(serialbuffer, 0, sizeof(serialbuffer));
      serialposition = 0;
    }
    else
    {
      // next position
      serialposition++;
      // test for overflow
      if (serialposition >= sizeof(serialbuffer))
      {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("buffer overflow");
        delay(5000);
        // clear buffer and reset position
        memset(serialbuffer, 0, sizeof(serialbuffer));
        serialposition = 0;
      }
    }
  }
}

Before the setup(), I’ve declared a buffer to store the serial data and variable for the position where the next received character can be stored.

In the setup, the buffer is clear using memset.

In the loop, we collect data till we see a defined character (LF, linefeed) that will indicate the end of the message. We store the character in the buffer (and display it for debugging purposes), check if it’s the defined character. If not, we increment the position for the next character. If it’s the defined character, we replace the the LF by a string terminator (null character) and display the complete message on the second line

I hope this helps.