serial communication system with two arduinos

with these two codes i wanted to send the value of x,that changes in some conditions, to another arduino
is it happening in my code, or is something wrong ?

tx arduino (code of the communication part)

#include <Wire.h>

int x;

void setup() {
Serial.begin(9600);
Wire.begin();
}

void loop() {
Wire.beginTransmission(5);

if (digitalRead(rap_min)==LOW && digitalRead(rel_min)==LOW && digitalRead(rel_max)==HIGH) 
  {
    digitalWrite(bomba, LOW);
    x=1;
    Wire.write(x);
  }

if (digitalRead(rap_min)==HIGH && digitalRead(rel_min)==LOW && digitalRead(rel_max)==HIGH)
  {
    digitalWrite(bomba,LOW);
    x=2;
    Wire.write(x);
  }
}

tx arduino (complete code)

#include <Wire.h>

#define bomba 9
#define rel_max 8
#define rel_min 7
#define rap_min 6

int x;

void setup() {
  pinMode(bomba, OUTPUT);
  pinMode(rel_max, INPUT);
  pinMode(rel_min, INPUT);
  pinMode(rap_min, INPUT);
  Serial.begin(9600);
  Wire.begin();
}

void loop() {
  digitalWrite(bomba, LOW); //no estado inicial a bomba esta desligada
  Wire.beginTransmission(5); // inicia transmissão entre os arduinos

  if (digitalRead(rap_min)==LOW && digitalRead(rel_min)==LOW && digitalRead(rel_max)==LOW) 
  {
    digitalWrite(bomba, LOW);
  }
  
  if (digitalRead(rap_min)==LOW && digitalRead(rel_min)==LOW && digitalRead(rel_max)==HIGH) 
  {
    digitalWrite(bomba, LOW); //se este caso acontecer um defeito ocorreu nos sensores
    x=1; //define x como 1
    Wire.write(x); //envia o defeito
  }

  if (digitalRead(rap_min)==LOW && digitalRead(rel_min)==HIGH && digitalRead(rel_max)==LOW) 
  {
    digitalWrite(bomba, LOW);
  }

  if (digitalRead(rap_min)==LOW && digitalRead(rel_min)== HIGH && digitalRead(rel_max)== HIGH) 
  {
    digitalWrite(bomba, LOW);
  }

  if (digitalRead(rap_min)==HIGH && digitalRead(rel_min)==LOW && digitalRead(rel_max)==LOW) 
  {
    digitalWrite(bomba, HIGH); //somente sob as condições acima a bomba será iniciada
  }

  if (digitalRead(rap_min)==HIGH && digitalRead(rel_min)==LOW && digitalRead(rel_max)==HIGH)
  {
    digitalWrite(bomba,LOW); //se este caso acontecer um defeito ocorreu nos sensores
    x=2; // define x como 2
    Wire.write(x); //envia o defeito
  }
  
  if (digitalRead(rap_min)==HIGH && digitalRead(rel_min)==HIGH && digitalRead(rel_max)==LOW)
  {
    digitalWrite(bomba, LOW); //depende do local de instalação
  }

  if (digitalRead(rap_min)==HIGH && digitalRead(rel_min)==HIGH && digitalRead(rel_max)==HIGH)
  {
    digitalWrite(bomba, LOW);
  }
  Wire.endTransmission();
}

rx arduino

#include <Wire.h>

int x;

void setup() 
{
  Wire.begin(5);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
}

void receiveEvent(int bytes) 
{
  x = Wire.read();
}

void loop()
{
  if (x=1) {
    Serial.print("defeito sensor -- status -- rap_min == LOW, rel_min == LOW, rel_max == HIGH");
  }
  if (x=2) {
    Serial.print("defeito sensor -- status -- rap_min == HIGH, rel_min == LOW, rel_max == HIGH");
  }
}

if (x=1) {Oops

TheMemberFormerlyKnownAsAWOL:

if (x=1) {

Oops

is it wrong ?

Yup.

And you clearly know that too. Look at any if in your tx code and spot the difference.

wildbill:
And you clearly know that too. Look at any if in your tx code and spot the difference.

i didn't get it
can you teach me how to fix it ?

is this way right ?

if (x > 0) {
    Serial.print("defeito sensor -- status -- rap_min == LOW, rel_min == LOW, rel_max == HIGH");
  }
  if (x > 1) {
    Serial.print("defeito sensor -- status -- rap_min == HIGH, rel_min == LOW, rel_max == HIGH");
  }
x = 1

Sets x to 1

x == 1

Compares x to 1

Now do you get it ?

The operator "=" assigns values.

The operator "==" compares values.

Moreover --

You have declared x as 16-bit number (int x;). I2C Bus is a byte oriented communication protocol. Wire.write(x); will queue lower part of x. What's about the upper part of x. The codes should be strictly as:

Wire.write(lowByte(x));    //assume you are sending lower byte first
Wire.write(highByte(x));   //

Likewise, at the receiver side, you should collect 2 bytes and then form the 16-bit value. You may say that that the upper byte of x is 0s as you have declared it as int x = 1. Then. why don't you declare it as byte x = 1?

What's unfortunately not documented in the Arduino Wire library reference is that the callback function you passed to Wire.onReceive() is called from an ISR. This means you should make all global variables that are modified in your receiveEvent function volatile. Otherwise, the compiler may just optimize out the variable, since it is apparently unused:
https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/volatile/

pert:
What's unfortunately not documented in the Arduino Wire library reference is that the callback function you passed to Wire.onReceive() is called from an ISR. This means you should make all global variables that are modified in your receiveEvent function volatile. Otherwise, the compiler may just optimize out the variable, since it is apparently unused:

But in many cases, the sketch works even with the variables not declared as volatile.

union
{
    flaot txTemp;
    byte txTempArray[4];
 
}data;

void sendEvent()
{
   Wire.write(data.txTempArray, sizeof(data.txTempArray));
}