1

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

  • 1
    Can you post the parts of the code that send and parse the messages? how about examples of what is being received? Can you tell if the error rate is due to the protocol or interference? – BrettAM Dec 11 '14 at 19:20
  • The problem could be baud rate, not correctly taking data or anything. As @BrettM said, we need more information. Also you say 4 motors and 2 joysticks. Do you have X1&Y1 and X2&Y2? You could send 6 bytes: Start byte,4 data bytes(0..255) and stop byte – Martynas Dec 12 '14 at 8:43
  • guys i have included the code. thanks for the reply – Kaustubh Mhatre Dec 12 '14 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.