problem sending data between two arduino using serial

Hi
i am trying to send (servo position ,lets say "150") to another servo connected to second arduino using TX,RX pins .

the problem is the position sent to second servo is not correct at all .

this is the first code (transmitter):

#include <Servo.h>
Servo servo1;
int pos=150;
void setup(){
servo1.attach(9);
Serial.begin(9600);}

void loop(){
Serial.write(pos);
Serial.flush();
}

and the receiver code :

#include <Servo.h>

Servo myservo2;

  int val;

void setup() {

  Serial.begin(9600);

  myservo2.attach(9); 

  myservo2.write(90);      
}

void loop() {
  
if (Serial.available()){

            val=Serial.read();
            Serial.flush();
            Serial.println(val);
}
}

  

}

serial monitor result

can anyone help please ?

thank you .

It looks to me like the code that is actually sending data is using Serial.print(), not Serial.write().

49 is a '1'
51 is a '3'
113 is a 'q'
10 and 13 are carriage return and line feed.

It also looks like you are trying to use the serial port to talk to the PC and to the other Arduino. That is not going to work.

what can i do ?

When in doubt, run in circles, scream and shout.

About what? Making sure that the code on the sender is actually using write()? About using Tools + Auto Format before posting code? About using some other pins, and SoftwareSerial, for communications between the two Arduinos?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. As you are in charge of both sending and receiving you can use the 3rd example which will be most reliable.

...R