I am trying to control 4 motors using 2 joysticks.
I want to send continuous analog values of 2 joysticks (x & y both).
I am using bluetooth HC-05. I am using the basic logic that is sending a character before each analog value and thus sending all 4 values.
My problem here is at the reception end, data is not received correctly. Error rate is very high.
Is their any other way or improved program to do that?
//this is my part of the code at the transmitter side
#include <SoftwareSerial.h>
int a,b,c,d;
int ry = A0; // forward backward
int rx = A1;//rotate
int lx = A3; //right left
int ly = A4;
SoftwareSerial blu(10, 11); // TX,RX
void setup() {
Serial.begin(9600);
blu.begin(9600);
}
void loop()
{
a = analogRead(ry)/4;
d = analogRead(rx)/4;
c= analogRead(ly)/4
b = analogRead(lx)/4;
blu.write("w");
blu.write(a);
blu.write("x");
blu.write(b);
blu.write("y");
blu.write(c);
blu.write("z");
blu.write(d);
}
}
// Code at the reciever side
#include <SoftwareSerial.h>
SoftwareSerial mySerial(12,13); // tx,rx
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
digitalWrite(gnd1 , LOW);
digitalWrite(gnd2 , LOW);
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(dir1, OUTPUT);
pinMode(dir2, OUTPUT);
pinMode(dir_1, OUTPUT);
pinMode(dir_2, OUTPUT);
}
void loop()
{
if (mySerial.available())
{
a=mySerial.read();
if(a=='w' || z=='x' || a=='y' || a=='z')
{
if(a=='w')
c_b=mySerial.read();
b=map(c_b,0,255,-255,255);
motorspeed1(b);
}
if(a=='x'){
int c_c=mySerial.read();
b=map(c_c,0,255,-255,255);
motorspeed2(c);
}
if(a=='y'){
int c_d=mySerial.read();
d=map(c_d,0,255,-255,255);
motorspeed3(d);
}
if(a=='z'){
int c_e=mySerial.read();
e=map(c_e,0,255,-255,255);
motorspeed3(e);
}
}
//motor speed function takes care of the direction and the pwm. my problem is at the receiving end the values are not proper . error is very high mySerial.read() gives false values