Sending data from ardunio to arduino via bluetooth

I am currently trying to send four integers over bluetooth to control a robot but it seems that when i have the robot write what it is receiving its nothing like what it should be please help i also download gyroscope library if that would interfere

(master)
#include <MPU6050_tockn.h>//gyroscope stuff
#include <Wire.h>

int joyX = A0;
int joyY = A1;
int JY;//joystick x and y
int JX;

int MJY;//maped joystick x and y
int MJX;

int gyroX;//gyrosope x and y
int gyroY;

int MX;//maped gyroscope x and y
int MY;

MPU6050 mpu6050(Wire);
long timer = 0;

void setup() {
Serial.begin(38400);//38400 is what bluoettoh needs

Wire.begin();//gyroscope stuff
mpu6050.begin();
mpu6050.calcGyroOffsets(true);

pinMode(joyY, INPUT);//sets joystick pins as input
pinMode(joyX, INPUT);
}

void loop() {
JY = analogRead(joyY);//reads joystick values
JX = analogRead(joyX);

mpu6050.update();//updates gyroscope values
int gyroX=mpu6050.getGyroAngleX();//reads gyroscope values
int gyroY=mpu6050.getGyroAngleY();

Serial.print("X:");Serial.print(gyroX);//prints gyroscope values
Serial.print(" Y:");Serial.print(gyroY);

Serial.print(" JX:");Serial.print(JX);//prints joystick values
Serial.print(" Jy:");Serial.print(JY);

Serial.print(" MJY:");Serial.print(MJY);//prints maped joystick values
Serial.print(" MJX:");Serial.print(MJX);

Serial.print(" MY:");Serial.print(MY);//prints maped gyroscope values
Serial.print(" MX:");Serial.println(MX);

MJY = map(JY, 0, 630, 0, 2);//maped to 1,2 or 3
MJX = map(JX, 0, 630, 0, 2);
MY = map(gyroY, -100, 100, 0, 544);//maped to servo
MX = map(gyroX, -100, 100, 0, 544);

if(Serial.available() > 0){//this is my attempt at sending data
Serial.write(MJX);
Serial.write(MJY);
Serial.write(MX);
Serial.write(MY);
}

}

(slave)
#include <Servo.h>
int MR = 4;//MR meaning right and left motor
int ML = 5;
Servo servoy;
Servo servox;
int sX;//servo x and y
int sY;

float intpy = 50;//servo intital values
float intpx = 35;

int MJY;//maped joystick values
int MJX;

int MX;//maped gyroscope values
int MY;

void setup() {
// put your setup code here, to run once:
pinMode(MR,OUTPUT);//motor right and left set as output
pinMode(ML,OUTPUT);

digitalWrite(MR,LOW);//turning off both motors
digitalWrite(ML,LOW);

Serial.begin(38400);//38400 is for bluetooth

servoy.attach(3);//attaching servo x and y
servox.attach(2);
}

void loop() {
if(Serial.available() > 0){//my attempt at receving values
MJX = Serial.read();
MJY = Serial.read();
MX = Serial.read();
MY = Serial.read();
}
Serial.print(" MJY:");Serial.print(MJY);//printing all received values
Serial.print(" MJX:");Serial.print(MJX);
Serial.print(" MY:");Serial.print(MY);
Serial.print(" MX:");Serial.println(MX);

if(MJX == 2){//everthing below is for deciding which motors to turn on or off
digitalWrite(MR,HIGH);
digitalWrite(ML,HIGH);
}
if(MJY == 0){
digitalWrite(ML,HIGH);
digitalWrite(ML,LOW);
}
if(MJY == 2){
digitalWrite(ML,LOW);
digitalWrite(ML,HIGH);
}
if(MJX == 1){
digitalWrite(MR,LOW);
digitalWrite(ML,LOW);
}
}

if(Serial.available() > 0){//this is my attempt at sending data
Serial.write(MJX);
Serial.write(MJY);
Serial.write(MX);
Serial.write(MY);
}

Why does there have to be data in the receive buffer before you write data out? That is not necessary. The numbers that you write are 2 bytes, Serial.write sends one byte. Use Serial.print. It would be more robust and easier to parse the data at the receive end if you delimit the data and use start and end markers like is done in the serial input basics tutorial.

Like so:

Serial.print("<");
Serial.print(MJX);
Serial.print(",");
Serial.print(MJY);
Serial.print(",");
Serial.print(MX);
Serial.print(",");
Serial.print(MY);
Serial.print(">");

Read the how to use this forum stickies to see how to properly post code.

thanks but that didn't completely solve my problem. my robot is reviving this: MJY:248 MJX:0 MY:-1 MX:-1
when it should be something more like this: MJY:0-2 MJX:0-2 MY:0-544 MX:0-544(as i mapped them)

Did you make changes to your code? If you change the code, post the latest version so that we can stay on the same page.

i think part of it is how im reading the data

(slave)

#include <Servo.h>
int MR = 4;//MR meaning right and left motor
int ML = 5;
Servo servoy;
Servo servox;
int sX;//servo x and y
int sY;

float intpy = 50;//servo intital values
float intpx = 35;

int MJY;//maped joystick values
int MJX;

int MX;//maped gyroscope values
int MY;

void setup() {
  // put your setup code here, to run once:
pinMode(MR,OUTPUT);//motor right and left set as output
pinMode(ML,OUTPUT);

digitalWrite(MR,LOW);//turning off both motors
digitalWrite(ML,LOW);

Serial.begin(38400);//38400 is for bluetooth

servoy.attach(3);//attaching servo x and y
servox.attach(2);
}

void loop() {
if(Serial.available() > 0){//my attempt at receving values
  MJX = Serial.read();
  MJY = Serial.read();
  MX = Serial.read();
  MY = Serial.read();
}
Serial.print(" MJY:");Serial.print(MJY);//printing all received values
Serial.print(" MJX:");Serial.print(MJX);
Serial.print(" MY:");Serial.print(MY);
Serial.print(" MX:");Serial.println(MX);

if(MJX == 2){//everthing below is for deciding which motors to turn on or off
digitalWrite(MR,HIGH);
digitalWrite(ML,HIGH);
}
if(MJY == 0){
digitalWrite(ML,HIGH);
digitalWrite(ML,LOW);
}
if(MJY == 2){
digitalWrite(ML,LOW);
digitalWrite(ML,HIGH);
}
if(MJX == 1){
digitalWrite(MR,LOW);
digitalWrite(ML,LOW);
}
}

(master)

#include <MPU6050_tockn.h>//gyroscope stuff
#include <Wire.h>

int joyX = A0;
int joyY = A1;
int JY;//joystick x and y
int JX;

int MJY;//maped joystick x and y
int MJX;

int gyroX;//gyrosope x and y
int gyroY;

int MX;//maped gyroscope x and y
int MY;

MPU6050 mpu6050(Wire);
long timer = 0;


void setup() {
Serial.begin(38400);//38400 is what bluoettoh needs

Wire.begin();//gyroscope stuff
mpu6050.begin();
mpu6050.calcGyroOffsets(true);

pinMode(joyY, INPUT);//sets joystick pins as input
pinMode(joyX, INPUT);
}

void loop() {
JY = analogRead(joyY);//reads joystick values
JX = analogRead(joyX);

mpu6050.update();//updates gyroscope values
int gyroX=mpu6050.getGyroAngleX();//reads gyroscope values
int gyroY=mpu6050.getGyroAngleY();

Serial.print("X:");Serial.print(gyroX);//prints gyroscope values
Serial.print(" Y:");Serial.print(gyroY);

Serial.print(" JX:");Serial.print(JX);//prints joystick values
Serial.print(" Jy:");Serial.print(JY);

Serial.print(" MJY:");Serial.print(MJY);//prints maped joystick values
Serial.print(" MJX:");Serial.print(MJX);

Serial.print(" MY:");Serial.print(MY);//prints maped gyroscope values
Serial.print(" MX:");Serial.println(MX);

MJY = map(JY, 0, 630, 0, 2);//maped to 1,2 or 3
MJX = map(JX, 0, 630, 0, 2);
MY = map(gyroY, -100, 100, 0, 544);//maped to servo
MX = map(gyroX, -100, 100, 0, 544);

if(Serial.available() > 0){//this is my attempt at sending data
Serial.print("<");
Serial.print(MJX);
Serial.print(",");
Serial.print(MJY);
Serial.print(",");
Serial.print(MX);
Serial.print(",");
Serial.print(MY);
Serial.print(">");

}

}

So you have the BT device connected to pins 0 and 1 (hardware serial) and you are using serial monitor as well? That is not going to work very well. Only one device should be connected to each serial port. You should set up a separate port for in the master for the BT device and use Serial for debug and program upload. If you are using an Uno (or other mega328 based board), set up a software serial port for the BT device. If you are using a Mega or the like use one of the other hardware ports for the BT device. Same for the slave.

if(Serial.available() > 0){//my attempt at receving values
  MJX = Serial.read();
  MJY = Serial.read();
  MX = Serial.read();
  MY = Serial.read();
}

Serial.read reads one byte at a time. The data that you are sending is of the int data type (2 bytes). I would modify the example #5 of the serial input basics to receive and parse the data.

if(Serial.available() > 0){//this is my attempt at sending data

Why is that still in the master code?

Use autoformat (ctrl-t or Tools, Auto Format) on the code to indent the code for readability.

i only have one arduino plugged in at once I don't need to read the values that the master is printing i only need to see the values the slave is receiving

groundFungus:
you are using serial monitor as well? That is not going to work very well.

the serial monitor lets me see what the arduino is outputting. i also dont know if yow saw but i edited my last reply and said it might be something with how i'm reading the data. thank you for helping me thus far.
im going to log off for a while so i will give you the most updated code i have.
(slave)

#include <Servo.h>
int MR = 4;//MR meaning right and left motor
int ML = 5;
Servo servoy;
Servo servox;
int sX;//servo x and y
int sY;

float intpy = 50;//servo intital values
float intpx = 35;

int MJY;//maped joystick values
int MJX;

int MX;//maped gyroscope values
int MY;

void setup() {
  // put your setup code here, to run once:
  pinMode(MR, OUTPUT); //motor right and left set as output
  pinMode(ML, OUTPUT);

  digitalWrite(MR, LOW); //turning off both motors
  digitalWrite(ML, LOW);

  Serial.begin(38400);//38400 is for bluetooth

  servoy.attach(3);//attaching servo x and y
  servox.attach(2);
}

void loop() {
  if (Serial.available() > 0) { //my attempt at receving values
    MJX = Serial.read();
    MJY = Serial.read();
    MX = Serial.read();
    MY = Serial.read();
  }
  Serial.print(" MJY:"); Serial.print(MJY); //printing all received values
  Serial.print(" MJX:"); Serial.print(MJX);
  Serial.print(" MY:"); Serial.print(MY);
  Serial.print(" MX:"); Serial.println(MX);

  if (MJX == 2) { //everthing below is for deciding which motors to turn on or off
    digitalWrite(MR, HIGH);
    digitalWrite(ML, HIGH);
  }
  if (MJY == 0) {
    digitalWrite(ML, HIGH);
    digitalWrite(ML, LOW);
  }
  if (MJY == 2) {
    digitalWrite(ML, LOW);
    digitalWrite(ML, HIGH);
  }
  if (MJX == 1) {
    digitalWrite(MR, LOW);
    digitalWrite(ML, LOW);
  }
}

(master)

#include <MPU6050_tockn.h>//gyroscope stuff
#include <Wire.h>

int joyX = A0;
int joyY = A1;
int JY;//joystick x and y
int JX;

int MJY;//maped joystick x and y
int MJX;

int gyroX;//gyrosope x and y
int gyroY;

int MX;//maped gyroscope x and y
int MY;

MPU6050 mpu6050(Wire);
long timer = 0;


void setup() {
  Serial.begin(38400);//38400 is what bluoettoh needs

  Wire.begin();//gyroscope stuff
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);

  pinMode(joyY, INPUT);//sets joystick pins as input
  pinMode(joyX, INPUT);
}

void loop() {
  JY = analogRead(joyY);//reads joystick values
  JX = analogRead(joyX);

  mpu6050.update();//updates gyroscope values
  int gyroX = mpu6050.getGyroAngleX(); //reads gyroscope values
  int gyroY = mpu6050.getGyroAngleY();

  Serial.print("X:"); Serial.print(gyroX); //prints gyroscope values
  Serial.print(" Y:"); Serial.print(gyroY);

  Serial.print(" JX:"); Serial.print(JX); //prints joystick values
  Serial.print(" Jy:"); Serial.print(JY);

  Serial.print(" MJY:"); Serial.print(MJY); //prints maped joystick values
  Serial.print(" MJX:"); Serial.print(MJX);

  Serial.print(" MY:"); Serial.print(MY); //prints maped gyroscope values
  Serial.print(" MX:"); Serial.println(MX);

  MJY = map(JY, 0, 630, 0, 2);//maped to 1,2 or 3
  MJX = map(JX, 0, 630, 0, 2);
  MY = map(gyroY, -100, 100, 0, 544);//maped to servo
  MX = map(gyroX, -100, 100, 0, 544);

    Serial.print("<");
    Serial.print(MJX);
    Serial.print(",");
    Serial.print(MJY);
    Serial.print(",");
    Serial.print(MX);
    Serial.print(",");
    Serial.print(MY);
    Serial.print(">");

  

}

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino-to-Arduino communication

You can send data in a compatible format with code like this

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

If you send all the values in the same order every time the position of the items will identify them.

...R

If you are sending data using Serial.print and you are intent on receiving a decimal as a String, you should also send it as such, after which you can decode back to an integer. The Serial monitor will output the bytes received as they are Serial.print(" MJY:"); Serial.print(MJY,DEC); will give you the desired result on the serial monitor MJX = Serial.read(); but now this will not have the desired result, unless you create a function that converts the String received back into an integer.
In other words the way the Serial monitor outputs the data to you is not the way your master & slave are communicating.

Also in your slave code:

if(Serial.available() > 0){//my attempt at receving values
  MJX = Serial.read();
  MJY = Serial.read();
  MX = Serial.read();
  MY = Serial.read();
}

you are obviously not expecting the Strings "MJX:","MJY:","MX:" & "MY:" that your master is sending.

My advice is either send data or send data as Strings, which is what i usually do, since that allows me to do some error checking. Read the data buffer until it's empty (slow your program down enough so to make sure you have full transmission. if you String does not start with "MJX:" discard the lot flush the buffer, send "ERROR" back to the master. If it does start with "MJX:" read the bit after convert it to an Integer (if possible) if not do the error thing and so on and so forth. I have code to make softwareSerial communicate with a hwSerial on an ESP but since the swSerial is slightly different in it's commands i don't think it will help you.