I'm using a two BLDC motors and running it perfectly with a joystick with Forward and Backward Commands. I want to achieve forward left, forward right, reverse left and reverse right but my results were not perfect with this.
Here below is the graph that I sketched, with the help of this graph, I wrote my code.
Joystick Graph is here, i just inverted the X-Axis as well
int VRx=A0; // ANALOG READING FOR FRONT AND BACK MOVEMENT
int VRy=A1; // ANALOG READING FOR RIGHT AND LEFT MOVEMENT
int Sw=2; //
float Aval; //VALUE OF VOLTAGE AT A0 PORT
float Bval; //VALUE OF VOLTAGE AT A1 PORT
unsigned int MAXIMUM = 1023; //MAXIMUM RESOLUTION
unsigned int MID = 512;
unsigned int MAX = 1023;
void setup() {
Serial.begin(4800);
pinMode(VRx,INPUT);
pinMode(VRy,INPUT);
pinMode(Sw,INPUT);
//digitalWrite(Sw,HIGH);
}
void loop() {
unsigned long tempA=0; //motor A speed
unsigned long tempA1=0; //use for formula
Aval=analogRead(VRx);
tempA = Aval;
Serial.println (tempA);
unsigned long tempB=0; //motor B speed
unsigned long tempB1=0; //use for formula
Bval=analogRead(VRy);
tempB = Bval;
Serial.println (tempB);
//FORWARD
if ((tempA>550) && (tempB>450 && tempB<550))
{
tempA1 = (((tempA-MID)*100)/MID);
char st[20]; // Send command to the buffer Vx as a statement for motor A
char st1[20]; // Send command Vy as a statement for motor B
sprintf(st,"<A+%03lu>",tempA1);
sprintf(st1,"<B+%03lu>",tempA1);
Serial.println (st);
Serial.println (st1);
// Serial.println (tempA);
}
//BACKWARD
if ((tempA<450) && (tempB>450 && tempB<550)){
tempA1= (((MID - tempA)*100)/MID);
char st[20];
char st1[20];
sprintf(st,"<A-%03lu>",tempA1);
sprintf(st1,"<B-%03lu>",tempA1);
Serial.println (st);
Serial.println (st1);
// Serial.println (tempA);
}
// NEUTRAL
if ((tempA>450 && tempA<550) && (tempB>450 && tempB<550))
{
char st[20];
Serial.println ("<A+000>");
Serial.println ("<B+000>");
// Serial.println (tempA);
// Serial.println (tempB);
}
// FORWARD LEFT
if (tempB<450 && tempA>550)
{
tempA1= (((tempA - MID)*100)/MID);
tempB1 = (((MID - tempB)*100)/MID);
char st[20];
char st1[20];
sprintf(st,"<A+%03lu>",tempA1);
sprintf(st1,"<B+%03lu>",tempB1);
Serial.println (st);
Serial.println (st1);
// Serial.println (tempB);
}
// FORWARD RIGHT
if (tempB>550 && tempA>550)
{
tempA1 = (((tempA - MID)*100)/MID);
tempB1= 100 - (((tempB - MID)*100)/MAX);
char st[20];
char st1[20];
sprintf(st,"<A+%03lu>",tempA1);
sprintf(st1,"<B+%03lu>",tempB1);
Serial.println (st);
Serial.println (st1);
// Serial.println (tempB);
}
//REVERSE RIGHT
if (tempB>550 && tempA<450)
{
tempA1 = (((MID - tempA)*100)/MID);
tempB1= (((MAX - tempB)*100)/MAX);
char st[20];
char st1[20];
sprintf(st,"<A-%03lu>",tempA1);
sprintf(st1,"<B-%03lu>",tempB1);
Serial.println (st);
Serial.println (st1);
// Serial.println (tempB);
}
//REVERSE LEFT
if (tempB<450 && tempA<450)
{
tempA1= (((MID - tempA)*100)/MID);
tempB1= (((MID - tempB)*100)/MID);
char st[20];
char st1[20];
sprintf(st,"<A-%03lu>",tempA1);
sprintf(st1,"<B-%03lu>",tempB1);
Serial.println (st);
Serial.println (st1);
// Serial.println (tempB);
}
delay(500);
}
Thank You ![]()

