Help! guidance for proportional steering robot tank.

Greetings, I am new to this…
I have written some code for driving two motors using L293D (TANK config)
I am taking ps2 thumbstick input x and y

y forward/reverse
x left/right

My problem is the part where i try to proportionaly turn the motors m1 & m2 (taking forward value of y) w.r.t x

ym1=(int)(xval/255)*yval;			// this part is not working as it should 
      ym2=(int)((255-xval)/255)*yval;

I did the math with various values… but ym1 & ym2 remain 0
I would be very helpful if someone responded asap

THANK YOU :slight_smile:

HERE is the full code:

const int potxpin=0;
const int potypin=1;
const int dcm1=5;
const int dcm2=6;
int xval=0;
int yval=0;
int ym1=0;
int ym2=0;
int nx=0;
int ny=0;
byte xpve=0;
byte xnve=0;
byte x0=0;


void setup()
{
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(12,OUTPUT);
  while(1)
  {
    nx=analogRead(potxpin);
    ny=analogRead(potypin);
    if((nx<550 && nx>450)&&(ny<550 && ny>450)){break;}
  }
   //Serial.println(nx);
   //Serial.println(ny);
}

void loop()
{ 
  xpve=0;
  xnve=0;
  x0=0;
      
  //Serial.println(micros());
  xval=analogRead(potxpin);
  yval=analogRead(potypin);
  
  if(xval>nx+4){//runs if >=516 assuming 511 in midpoint or nx ny
    xpve=1;
    xval=map(xval,nx+4,1023,0,255);
  }
  else if(xval<nx-4){//runs if <=106
    xnve=1;
    xval=map(xval,nx-4,0,0,255);
  }
  else{//runs for 507 to 515
    xval=0;
  }
  
  //--------------------------------------
  
  if(yval>ny+4){//forward
  	yval=map(yval,ny+4,1023,0,255);
    if(xpve==1){//forward +left
      
      ym1=(int)(xval/255)*yval;			// this part is not working as it should 
      ym2=(int)((255-xval)/255)*yval;	//working theoretically...
      
      /*Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,ym1);
      analogWrite(dcm2,ym2);
      digitalWrite(4,LOW);
      digitalWrite(7,HIGH);
      digitalWrite(8,LOW);
      digitalWrite(12,HIGH);
    }
    else if(xnve==1){//forward +right
      ym1=((255-xval)/255)*yval;
      ym2=(xval/255)*yval;
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,ym1);
      analogWrite(dcm2,ym2);
      digitalWrite(4,LOW);
      digitalWrite(7,HIGH);
      digitalWrite(8,LOW);
      digitalWrite(12,HIGH);
    }
    else{//forward
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,yval);
      analogWrite(dcm2,yval);
      digitalWrite(4,LOW);
      digitalWrite(7,HIGH);
      digitalWrite(8,LOW);
      digitalWrite(12,HIGH);
    }
  }
  else if(yval<ny-4){//reverse
    yval=map(yval,ny-4,0,0,255);
    if(xpve==1){//reverse+left
      ym1=(xval/255)*yval;
      ym2=((255-xval)/255)*yval;
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,ym1);
      analogWrite(dcm2,ym2);
      digitalWrite(4,HIGH);
      digitalWrite(7,LOW);
      digitalWrite(8,HIGH);
      digitalWrite(12,LOW);
    }
    else if(xnve==1){//reverse+right
      ym1=((255-xval)/255)*yval;
      ym2=(xval/255)*yval;
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,ym1);
      analogWrite(dcm2,ym2);
      digitalWrite(4,HIGH);
      digitalWrite(7,LOW);
      digitalWrite(8,HIGH);
      digitalWrite(12,LOW);
    }
    else{//reverse
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,yval);
      analogWrite(dcm2,yval);
      digitalWrite(4,HIGH);
      digitalWrite(7,LOW);
      digitalWrite(8,HIGH);
      digitalWrite(12,LOW);
    }
  }
  else{//point turn
    yval=0;
    if(xpve==1){//turn left
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,xval);
      analogWrite(dcm2,xval);
      digitalWrite(4,LOW);
      digitalWrite(7,HIGH);
      digitalWrite(8,HIGH);
      digitalWrite(12,LOW);
    }
    else if(xnve==1){//turn right
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,xval);
      analogWrite(dcm2,xval);
      digitalWrite(4,HIGH);
      digitalWrite(7,LOW);
      digitalWrite(8,LOW);
      digitalWrite(12,HIGH);
    }
    else{//stationary
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,0);
      analogWrite(dcm2,0);
      digitalWrite(4,LOW);
      digitalWrite(7,LOW);
      digitalWrite(8,LOW);
      digitalWrite(12,LOW);
    }
  }
  
       
}

Tinkercad link:

Greetings, I am new to this…
I have written some code for driving two motors using L293D (TANK config)
I am taking ps2 thumbstick input x and y

y forward/reverse
x left/right

My problem is the part where i try to proportionaly turn the motors m1 & m2 (taking forward value of y) w.r.t x

ym1=(int)(xval/255)*yval;			// this part is not working as it should 
ym2=(int)((255-xval)/255)*yval;

I did the math with various values… but ym1 & ym2 remain 0
I would be very helpful if someone responded asap

THANK YOU :slight_smile:

HERE is the full code:

const int potxpin=0;
const int potypin=1;
const int dcm1=5;
const int dcm2=6;
int xval=0;
int yval=0;
int ym1=0;
int ym2=0;
int nx=0;
int ny=0;
byte xpve=0;
byte xnve=0;
byte x0=0;


void setup()
{
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(12,OUTPUT);
  while(1)
  {
    nx=analogRead(potxpin);
    ny=analogRead(potypin);
    if((nx<550 && nx>450)&&(ny<550 && ny>450)){break;}
  }
   //Serial.println(nx);
   //Serial.println(ny);
}

void loop()
{ 
  xpve=0;
  xnve=0;
  x0=0;
      
  //Serial.println(micros());
  xval=analogRead(potxpin);
  yval=analogRead(potypin);
  
  if(xval>nx+4){//runs if >=516 assuming 511 in midpoint or nx ny
    xpve=1;
    xval=map(xval,nx+4,1023,0,255);
  }
  else if(xval<nx-4){//runs if <=106
    xnve=1;
    xval=map(xval,nx-4,0,0,255);
  }
  else{//runs for 507 to 515
    xval=0;
  }
  
  //--------------------------------------
  
  if(yval>ny+4){//forward
  	yval=map(yval,ny+4,1023,0,255);
    if(xpve==1){//forward +left
      
      ym1=(int)(xval/255)*yval;			// this part is not working as it should 
      ym2=(int)((255-xval)/255)*yval;	//working theoretically...
      
      /*Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,ym1);
      analogWrite(dcm2,ym2);
      digitalWrite(4,LOW);
      digitalWrite(7,HIGH);
      digitalWrite(8,LOW);
      digitalWrite(12,HIGH);
    }
    else if(xnve==1){//forward +right
      ym1=((255-xval)/255)*yval;
      ym2=(xval/255)*yval;
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,ym1);
      analogWrite(dcm2,ym2);
      digitalWrite(4,LOW);
      digitalWrite(7,HIGH);
      digitalWrite(8,LOW);
      digitalWrite(12,HIGH);
    }
    else{//forward
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,yval);
      analogWrite(dcm2,yval);
      digitalWrite(4,LOW);
      digitalWrite(7,HIGH);
      digitalWrite(8,LOW);
      digitalWrite(12,HIGH);
    }
  }
  else if(yval<ny-4){//reverse
    yval=map(yval,ny-4,0,0,255);
    if(xpve==1){//reverse+left
      ym1=(xval/255)*yval;
      ym2=((255-xval)/255)*yval;
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,ym1);
      analogWrite(dcm2,ym2);
      digitalWrite(4,HIGH);
      digitalWrite(7,LOW);
      digitalWrite(8,HIGH);
      digitalWrite(12,LOW);
    }
    else if(xnve==1){//reverse+right
      ym1=((255-xval)/255)*yval;
      ym2=(xval/255)*yval;
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,ym1);
      analogWrite(dcm2,ym2);
      digitalWrite(4,HIGH);
      digitalWrite(7,LOW);
      digitalWrite(8,HIGH);
      digitalWrite(12,LOW);
    }
    else{//reverse
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,yval);
      analogWrite(dcm2,yval);
      digitalWrite(4,HIGH);
      digitalWrite(7,LOW);
      digitalWrite(8,HIGH);
      digitalWrite(12,LOW);
    }
  }
  else{//point turn
    yval=0;
    if(xpve==1){//turn left
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,xval);
      analogWrite(dcm2,xval);
      digitalWrite(4,LOW);
      digitalWrite(7,HIGH);
      digitalWrite(8,HIGH);
      digitalWrite(12,LOW);
    }
    else if(xnve==1){//turn right
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,xval);
      analogWrite(dcm2,xval);
      digitalWrite(4,HIGH);
      digitalWrite(7,LOW);
      digitalWrite(8,LOW);
      digitalWrite(12,HIGH);
    }
    else{//stationary
     /* Serial.print("xval=");
      Serial.print(xval);
      Serial.print(" yval=");
      Serial.print(yval);
      Serial.print(" ym1=");
      Serial.print(ym1);
      Serial.print(" ym2=");
      Serial.println(ym2);*/
      analogWrite(dcm1,0);
      analogWrite(dcm2,0);
      digitalWrite(4,LOW);
      digitalWrite(7,LOW);
      digitalWrite(8,LOW);
      digitalWrite(12,LOW);
    }
  }
  
       
}

Tinkercad link:

ym1=(int)(xval/255)*yval;

This could be a problem with integer math. If the value xval is <255, then this will always be 0.

Try changing the order to

ym1=(int)(xval*yval)/255;

which is the same mathematically but makes for a bigger numerator.

If that still fails, then you should start printing what the numbers are before and after the equation to tell you what is going on.

Thanks it did work !!! why, can it not process precedence of operators like brackets an all that??

The language has rules, described in meticulous detail in the standard, you have to understand the rules too...

Integer maths does not have any fractions. We use integer maths on computers because it is much faster that floating point maths (unless there is floating point hardware, which Arduino usually does not have).

x/y will always be zero until x>y (there is no 0.1, 0.2, etc as these are real numbers).

So with integer maths you should make sure that the numerator is as big as it can be before dividing or you lose a lot of precision.

OH! yah... Obviously silly question :P

DON’T CROSS POST!!!

I HAVE REPORTED THIS THREAD TO THE MODERATORS

EDIT - all covered in your other post

got my answer it was int math logical error fixed it thanks

yah very sry im new so i posted it on another thread won't happen again.

@shddeb, please do not cross-post. Threads merged.