Can you please guide me about analog joystick ? Is it possible to control the directions and speed of two motors just like an RC Car movements with one joystick (VRx, VRy )

please tell me about

Please follow the advice given in the link below when posting code. Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

As well as CODE TAGS, look into auto-formatting your code…

You really need to explore (functions), the switch() statement, millis() timing and a few other basic C concepts.

Your code is about three times as long as it needs to be, and the delay()s will make it quite unresponsive.

Please see again

See what again ?

There are still no code tags around your code

And I don't even see code anymore.

@mubashirazmat, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the IDE 1.x category.

Sure. I've posted this a few times but I guess you didn't find it in your search.

const byte XPotPin = A0;
const byte YPotPin = A1;

const int PWM_LIMIT = 255;
const int DEAD_ZONE = 10;

void loop()
{
  int speedInput = analogRead(YPotPin); // Forward/Reverse
  int yawInput = analogRead(XPotPin); // Left/Right turn

  // map 'speed' to the range -PWM_LIMIT (backward), +PWM_LIMIT (forward)
  speedInput = map(speedInput, 0, 1023, -PWM_LIMIT, PWM_LIMIT);
  yawInput = map(yawInput, 0, 1023, -PWM_LIMIT, PWM_LIMIT);

  // Put in dead zones
  if (speedInput > -DEAD_ZONE && speedInput < DEAD_ZONE)
    speedInput = 0;
  if (yawInput > -DEAD_ZONE && yawInput < DEAD_ZONE)
    yawInput = 0;

  int leftSpeed = speedInput + (YAW_SENSITIVITY * yawInput);
  int rightSpeed = speedInput - (YAW_SENSITIVITY * yawInput);

  // neither motor can go faster than maximum speed
  leftSpeed = constrain(leftSpeed, -255, 255);
  rightSpeed = constrain(rightSpeed, -255, 255);

  // For positive speeds, motor goes forward at speed
  // For negative speeds, motor goes backward at -speed
  // For zero speeds, turn off motor
}
1 Like

This uses one byte of memory. Why not

#define XPotPin A0

? Any saving?

Are you sure? I don't know if it uses an extra byte of memory or not. Perhaps you could try it and find out for sure. (I suspect that the compiler optimization is good enough that they both compile to the same code.)

One advantage of using "const" instead of "define": If you use the same identifier in two places, "const" generates better warning/error messages.

Well. Optimisers do quite a lot but this looks like too much to expect. I'll give it a try and come back!

About warnings, You surely know more than I do.

Const vs #define – When do you use them and why?

Fresh report. The amount of pgm memory as well as global variable memory turned out the same!
I'm impressed by the optimiser!
I changed 8 "const int..." to #define...

Using #define keep in mind that all chars will be substituted at compile time. In one example the define ended with a ";"..... No good.

No define can use the comment sign //.... Comments in a #define need to be /comments/...

Don't worry. Something does that for you. If you use:
#define name expression // comment
it gets changed to:
'#define name expression /* comment */`

I'm not sure which process does that. I expect it to be the preprocessor.

I only get more and more impressed! Whow!
But..... Using unknown compilers and optimisers.... don't expect that much I would say.

thank you for your response, Sir. I m using two EZRUN MAX 8 BLDC Motors .. I think i need to make some changes in your code .... :slight_smile:

Sir, Can you please also check this link here is the details along with the code :slight_smile:
https://forum.arduino.cc/t/arduino-programming-ezrunmax-8-bldc-joystick/910633

Thank you

Why did you create a new topic ?

its same; sorry i m new here so yesterday I did not know how to use tags, today i learned about how to upload images, coding etc :slight_smile:

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 :slight_smile: