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);
}
}