Error in first data serial communication

Hello everyone, i have some problem with my code. i want to send data from arduino with serial communication. but in the serial monitor why in the first data always error with display data for the next data the display is normal. what the solution for that ? thank you for advance



Ask yourself, what is the ASCII for Newline?

Please do not post pictures of code and Serial monitor output. They are difficult to read and cannot be copied for examination and testing

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

this is my code

int A = 8;
int IN3 = 9;
int IN4 = 10;
int B = 11;
int IN1 = 12;
int IN2 = 13;

float sementara_1_min;
float sementara_1_max;
float sementara_2_min;
float sementara_2_max;
float sementara_3_min;
float sementara_3_max;

unsigned long samplingTime_serial = millis();
long interval_serial = 1;

void setup() {
  Serial.begin(9600);
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
}

void loop() {
  unsigned long currentTime = millis();
  if (Serial.available()) {
    int pwmvalue_A = Serial.parseInt();
    int pwmvalue_B = Serial.parseInt();
    int serial1 = Serial.read();
    if (serial1 == char(13)) {
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      analogWrite(A, pwmvalue_A);
      analogWrite(B, pwmvalue_B);
    }
  }
  if (currentTime - samplingTime_serial > interval_serial) {
    sementara_1_min = 27.5;
    sementara_1_max = 50;
    sementara_2_min = 7.5;
    sementara_2_max = 14;
    sementara_3_min = 225;
    sementara_3_max = 500;
    Serial.print(random(sementara_1_min, sementara_1_max));
    Serial.print(" ");
    Serial.print(random(sementara_2_min, sementara_2_max));
    Serial.print(" ");
    Serial.println(random(sementara_3_min, sementara_3_max));
    samplingTime_serial = currentTime;
  }
}

I'm sorry I don't understand the meaning of the question because I'm still learning about it. the system I want is data can be sent serially and it works, but on the first data always a symbol appears like in the picture I put a red box.
what I want is the data can be displayed like the 2nd data and so on without any symbols like that

if by works you mean that

if (serial1 == char(13)) {...

is never entered, then mmmk

how to solution for fix the problem sir ?

see post #2

int serial1 = Serial.read();
    if (serial1 == char(13)) {

for in this script the function is, I previously sent sensor data input serially then processing was carried out to get output received serially also for the 2 codes above I used to receive serial output buffer data. I don't understand what you mean by asci

Your Arduino console is set to end the input with Newline, new line ASCII code is 10, you are using 13, 13 is ASCII code for carriage return, which you never send.

As for garbage output, you are spamming serial with delay of 1ms, so before you open your console you get trash data accumulated.

this is 1 ms, 1s will be 1000 for example, try that

so for the spamming serial, does this happen because the time interval is too fast is it true?

Have you tried slowing it down?

Previously I have combined several timings but the error code appears but in a different place. I've used 500,1000, even 2000 the results still generate garbage code like before with different locations

It only generates trash for me when it is spamming, at 1 sec interval everything is clean.

Try adding delay(200); after Serial.begin(9600);.

When you upload a new sketch it will start running immediately. About 180 milliseconds later the Serial Monitor will connect to your Arduino and cause a reset. Any output generated by your sketch in those 180 milliseconds (about 3000 instruction cycles) will be received by your PC at 9600 baud and put in the input buffer. That may be the crud you see at the start of your input. Adding the delay keeps your sketch from sending output before the reset.

I have tried this method, but several times it still appears. some recommend using Serial.flush(); but I haven't found an answer for laying it.