Arduino Mega Serial1 Noise

Hi,

I connected Max485 to Rx1 Tx1 in arduino mega, and i’m sending via Serial1 some commands (Strings) to open and close relays connected to the arduino

but i’m facing issue that there are noise/question marks or garbage when i log them it appears before the command (like screenshot serial monitor).


how to neglect these noise to be able to fix this issue

#include <ArduinoJson.h>

//DEFINE PINS USED FOR THE RELAYS
const int pin_485Dir = 8;
const int RS485Transmit = HIGH;      // TX Enable RS485
const int RS485Receive = LOW; // RX Enable RS485
const int8_t MOTOR_1 =  41;
const int8_t VALVE_PINS[20] = { -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40};
const int8_t VALVE_VALUES[19] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
const size_t response_json_cap = JSON_OBJECT_SIZE(2);
DynamicJsonDocument res(response_json_cap);
void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
 Serial1.flush();
  //ALWAYS RECEIVE FROM THE GATEWAY
  digitalWrite(pin_485Dir, RS485Transmit);
  //Serial1.flush();
  Serial1.println("Arduino Connected");
  Serial1.flush();
  //digitalWrite(pin_485Dir, RS485Receive);
  Serial.println("Arduino Connected");
  for (uint8_t i = 0; i < 20; i++) {
    pinMode(VALVE_PINS[i], OUTPUT);
  }
  pinMode(pin_485Dir, OUTPUT);
  pinMode(MOTOR_1, OUTPUT);
}

String command;
int32_t relay_bits = 0;
void loop()
{
  Serial1.flush();
  //#Serial.flush();
  digitalWrite(pin_485Dir, RS485Receive);
  //Serial1.println("Hi from arduino");
  while (Serial1.available()) {
    char ch = Serial1.read();
    //char ch = Serial.read();
    if (ch == '\n') {
      Serial.println("RX : " +command);
      parseCommand(command);
      command = "";
      //Serial1.flush();
    }
    else {
      command += ch;
    }
    //Serial.print("RX: ");
    //Serial.print(ch);
    //Serial.println("");
  }

  delay(1000);
}
/*
  void ToggleRelay(String  rel,bool state) {
  Serial.print("relay_bits : ");
  Serial.print(relay_bits);
    digitalWrite(pin_485Dir, RS485Transmit);
    //Serial1.println(digitalRead(rel));
    //Serial1.println("Hi from arduino");
    //delay(100);
    //#Serial1.flush();
    Serial1.flush();
    digitalWrite(pin_485Dir, RS485Receive);
    Serial.println(digitalRead(pin_485Dir));
  }
*/
void relayValueSet(int relay, bool state) {
  bitWrite(relay_bits, relay, state);
  digitalWrite(VALVE_PINS[relay], state);
  digitalWrite(pin_485Dir, RS485Transmit);
  if(digitalRead(VALVE_PINS[relay])){
    //const char* json = "{\"status\":1,\"valve_no\":1}";
    res["status"] = digitalRead(VALVE_PINS[relay]);
    res["valve_no"]= relay;
    serializeJson(res, Serial1);
    Serial1.println();
    //Serial1.println(json);
  }else{
    res["status"] = digitalRead(VALVE_PINS[relay]);
    res["valve_no"]= relay;
    serializeJson(res, Serial1);
    Serial1.println();
  }
  //Serial1.println(digitalRead(VALVE_PINS[relay]));
  //Serial1.println("Hi from arduino");
  //delay(100);
  //#Serial1.flush();
  Serial1.flush();
  digitalWrite(pin_485Dir, RS485Receive);
}
void parseCommand(String com) {
  String part1;
  String part2;
  //1_ON
  //1_OFF
  Serial.println(com);
  part1 = com.substring(0, com.indexOf("_"));
  part2 = com.substring(com.indexOf("_") + 1);
  //Serial.println(part1);
  //Serial.println(part2);
  //Serial.println(command);
  if (part2.equalsIgnoreCase("ON")) {
    Serial.println(part1);
    //ToggleRelay(part1,HIGH);
    relayValueSet(part1.toInt(), HIGH);
    delay(300);
    //Serial.println(arrayIncludeElement(VALVE_VALUES, part1.toInt()));
    //Serial.println("****");
    //Serial.println(digitalRead(VALVE_PINS[part1.toInt()]));
    if (digitalRead(VALVE_PINS[part1.toInt()]) == HIGH && arrayIncludeElement(VALVE_VALUES, part1.toInt())) {
      digitalWrite(MOTOR_1, HIGH);
    }
  }
  else if (part2.equalsIgnoreCase("OFF")) {
    relayValueSet(part1.toInt(), LOW);
    //ToggleRelay(part1,LOW);
    //delay(200);
    //Serial.println("RELAY *** ");
    //Serial.println(relay_bits);
    if (relay_bits == 0) {
      digitalWrite(MOTOR_1, LOW);
    }
  }
  
}

boolean arrayIncludeElement(int8_t array[], int8_t element) {
  Serial.println(element);
  //element +=21;
  for (int i = 0; i < 19; i++) {
    Serial.println(array[i]);
    if (array[i] == element) {
      return true;
    }
  }
  return false;
}

The command Serial1.flush() is intended for use when sending data. It causes the Arduino to wait until the Serial Output buffer is empty.

If you want to empty the Serial Input buffer then use this code

while (Serial1.avaliable() > 0) {
   byte dumpTheValue = Seria1.read();
}

I suspect your image is a picture of text. Please don't post pictures of text, just copy and paste the text. If it is a picture of something else then please make it visible in your Post. See this Simple Image Posting Guide

...R

Robin2: The command Serial1.flush() is intended for use when sending data. It causes the Arduino to wait until the Serial Output buffer is empty.

If you want to empty the Serial Input buffer then use this code

while (Serial1.avaliable() > 0) {
   byte dumpTheValue = Seria1.read();
}

I suspect your image is a picture of text. Please don't post pictures of text, just copy and paste the text. If it is a picture of something else then please make it visible in your Post. See this Simple Image Posting Guide

...R

I edited the post and added the image

Also, will while (Serial1.avaliable() > 0) fix the noise issue?

KareemWaheed:
I edited the post and added the image

I did ask you NOT to post an image of text - it is almost impossible to read.

Also, will while (Serial1.avaliable() > 0) fix the noise issue?

There is more to my suggestion than that. Have you tried what I suggested?

…R