RS485 how to send 2 integers and receive in slave

sorry for the english and sorry i dont know the rules in the forum

Hello im making like a
thermostat with two ntc so i can compare the two sensors and turn on a relay when one sensor have a higher value than the other but i want to leave the "principal" board on the cave and make a simple lcd to watch the temperatures in my kitchen for example so i need to send the two ntc values to the lcd with the max485 but im a beginner in programming so i need help to understand.

Here is the code -

#include <Thermistor.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"

RTC_DS1307 RTC;

#define ledProg1 13
#define ledProg2 12

unsigned long int tanterior = 0;
int tempopremido = 0;
int tempopremido2 = 0;

int tempSolar = 0;
int tempAcomu = 0;

byte botaopcoes = 2;
byte botaoMais = 3;
byte botaoMenos = 4;
byte botaoHorarios = 6;

int indicador = 0;
int anterior = HIGH;
int seta = HIGH;

int diferencialtemp = 0;

byte setpointON = 4;
byte setpointOFF = 8;

byte tempMax = 30;

byte menuSP = 100;
byte menuHorarios = 3000;

byte releSolar = 13;
byte releReserva = 11;

byte hora;
byte minutos;
byte horaoff;
byte minutosoff;
byte hora2;
byte minutos2;
byte hora2off;
byte minutos2off;

Thermistor temp(A2);
Thermistor temp2(A1);

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() { //void_setup:...........................................................................................

  pinMode(botaopcoes, INPUT_PULLUP);
  pinMode(botaoMais, INPUT_PULLUP);
  pinMode(botaoMenos, INPUT_PULLUP);
  pinMode(botaoHorarios, INPUT_PULLUP);
  pinMode(ledProg1, OUTPUT);
  pinMode(ledProg2, OUTPUT);

  lcd.begin(16, 2);

  Wire.begin();
  RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  delay(2000);

}
void loop() {

  DateTime now = RTC.now();

  diferencialtemp = (tempSolar) - (tempAcomu);

  if (diferencialtemp <= 0) {
    diferencialtemp = diferencialtemp * -1;
  }
if (millis() - tanterior >= 1000) {
  tanterior = millis();
  tempAcomu = temp2.getTemp();
  tempSolar = temp.getTemp();

  lcd.setBacklight(HIGH);
  lcd.setCursor(0, 0);
  lcd.print("Temp. Solar:");
  lcd.print(tempSolar);
  lcd.setCursor(0, 1);
  lcd.print("Temp. Acomu:");
  lcd.print(tempAcomu);

}

  //botao opções:...........................................................................................

  while (digitalRead(botaopcoes) == LOW) {
    delay(100);
    tempopremido = tempopremido + 100;
  }

  if (tempopremido >= menuSP && tempopremido <= menuHorarios) {
    lcd.clear();
    delay(10);
  }

  while (tempopremido >= menuSP && tempopremido <= menuHorarios) { //menu de setpoints

    lcd.setCursor(0, 0);
    lcd.print("Solar.ON:");
    lcd.print(setpointON);
    lcd.setCursor(0, 1);
    lcd.print("Sol.OFF:");
    lcd.print(setpointOFF);
    lcd.setCursor(11, 1);
    lcd.print("DF:");
    lcd.print(diferencialtemp);

    indicador = digitalRead(botaopcoes);

    if (indicador != anterior) {

      if (seta == HIGH) {
        seta = LOW;
        lcd.setCursor(10, 1);
        lcd.print(" ");
        lcd.setCursor(11, 0);
        lcd.print(char(60));

      } else {
        seta = HIGH;
        lcd.setCursor(11, 0);
        lcd.print(" ");
        lcd.setCursor(10, 1);
        lcd.print(char(60));
      }
      delay(100);
    }

    if (seta == LOW) {
      if (digitalRead(botaoMais) == LOW) {
        setpointON++;
        delay(250);
      }

      if (digitalRead(botaoMenos) == LOW  && setpointON > 0) {
        setpointON--;
        delay(250);
      }

      if (setpointON <= 9) {
        lcd.setCursor(10, 0);
        lcd.print(" ");
      }
    }

    if (seta == HIGH) {
      if (digitalRead(botaoMais) == LOW) {
        setpointOFF++;
        delay(250);
      }

      if (digitalRead(botaoMenos) == LOW && setpointOFF > 0) {
        setpointOFF--;
        delay(250);
      }
      if (setpointOFF <= 9) {
        lcd.setCursor(9, 1);
        lcd.print(" ");
      }

    }

    esc(); //funcao para sair do menu selecionado

  }

  if (tempopremido >= menuHorarios) {
    lcd.clear();
    delay(10);
  }

  while (tempopremido >= menuHorarios) { //menu de temperatura maxima do termoacumolador

    lcd.setCursor(0, 0);
    lcd.print("TempMax.Acomula:");
    lcd.setCursor(7, 1);
    lcd.print(tempMax);

    if (digitalRead(botaoMais) == LOW) {
      tempMax++;
      delay(250);
    }

    if (digitalRead(botaoMenos) == LOW) {
      tempMax--;
      delay(250);
    }

    if (tempMax <= 0) {
      tempMax = tempMax * 0;
    }

    esc(); //funcao para sair do menu selecionado

  }

  // botao horarios:............................................................................................................

  while (digitalRead(botaoHorarios) == LOW) {
    tempopremido2 = tempopremido2 + 100;
    delay(100);

    if (tempopremido2 < menuHorarios && digitalRead(botaoHorarios) == LOW) {
      digitalWrite(ledProg1, HIGH);
      delay(100);
    } else if (tempopremido2 > menuHorarios && digitalRead(botaoHorarios) == LOW) {
      digitalWrite(ledProg2, HIGH);
      delay(100);
    } else {
      digitalWrite(ledProg1, LOW);
      digitalWrite(ledProg2, LOW);
      delay(100);
    }
  }

  //Condições para disparo da bomba:...........................................................................................

  if (tempSolar > tempAcomu && diferencialtemp >= setpointON && tempAcomu <= tempMax) {
    digitalWrite(releSolar, HIGH);
    delay(300);

  }

  if (tempAcomu > tempSolar && diferencialtemp >= setpointOFF && tempAcomu <= tempMax) {
    digitalWrite(releSolar, LOW);
    delay(300);
  }

}

void esc () { //funcao para sair do menu selecionado

  if (digitalRead(botaopcoes) == LOW && digitalRead(botaoHorarios) == LOW) {
    lcd.clear();
    digitalWrite(ledProg1, LOW);
    digitalWrite(ledProg2, LOW);
    tempopremido = 0;
    tempopremido2 = 0;
    delay(800);
  }

}

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

like this? thank you

Yes, thanks.

Now, can you describe the difficulties that you are having?

Also a schematic would be helpful. How to make and post a schematic.

this is the "motherboard" so i want to build another arduino with a lcd and use rs485 to receive the values from the motheboard and print the values in the second lcd arduino

Hi @Francisco_Silva1234

I suggest you divide the task into 3 parts.

1st task:
Make 2 simple sketches, 1 sending 2 numbers
via RS485 and another receiving the 2 numbers and showing on the serial monitor.
So you can be sure that the data is being sent and received correctly.

2nd task:
Add the LCD to the sketch that receives the values.

3rd task:
Adapt your sketch to incorporate data sent via RS485.

The RS485 library has sending and receiving examples.

RV mineirin

Hello thank you for the help! this is a sketch i found and changed and it works but after some time like 1 or 2 min the values received in the monitor are 0 but randomly and honestly i didnt understand the part of RX[0], this is what, a vector?

solar 0

acomu 40

solar 25

acomu 0

#include <SoftwareSerial.h>

#define TX_485 8 // Pino DI do módulo RS 485
#define RX_485 7 // Pino RO do módulo RS 485
#define RS485_control 3 // Habilita ou não a transmissão e recepção de dados

SoftwareSerial RS485_serial (RX_485, TX_485);
byte RX[4];

#define RS485transmit HIGH
#define RS485receive LOW

// variavel para armazenar a temperatura
int temp;
int temp2;
unsigned long tempo = 0;

void setup()
{

  Serial.begin(9600);
  RS485_serial.begin(9600);
  pinMode(RS485_control, OUTPUT);
  Serial.println(" Comunicacao RS485 ");

}

void loop() {

  if ((millis() - tempo) > 1000) {
    
    tempo = millis();
    digitalWrite(RS485_control, RS485receive);
    if (RS485_serial.available () > 0)
    {
      for (int i = 0; i < 4; i ++)
      {
        RX[i] = RS485_serial.read();
      }
      temp = RX[0];
      temp2 = RX[1];
      Serial.println("solar");
      Serial.println( RX[1] );
      delay(50);
      Serial.println("acomu");
      Serial.println( RX[0] );
    }
  }
}

Hi @Francisco_Silva1234
This sketch you posted is the receiver, but what is the transmitter sketch like?

RV mineirin

this is the transmitter sketch

#include <SoftwareSerial.h>

#define TX_485 8 // Pino DI do módulo RS 485
#define RX_485 7 // Pino RO do módulo RS 485
#define RS485_control 3 // Habilita ou não a transmissão e recepção de dados
SoftwareSerial RS485_serial (RX_485, TX_485);

byte TX [4];
#define RS485transmit HIGH
#define RS485receive LOW

unsigned int tempo = 0;

void setup()
{
  // velocidade comunicaçao serial
  Serial.begin(9600);
  RS485_serial.begin(9600);
  pinMode(RS485_control, OUTPUT);

}
void loop() {
  
  if ((millis() - tempo) > 1000)
  {
    tempo = millis();
    digitalWrite(RS485_control, RS485transmit);
    TX[0] = 25;
    TX[1] = 30;
    for (int i = 0; i < 4; i++)
    {
      RS485_serial.write(TX[i]);
    }
  }
}

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