Thumbstick glue code for motor control

I want two of my DC motors to be adjusted according to the thumbstick feedback. They will be on top of a hoverboard.

Thumbstick forward means:
Two engines running full speed running clockwise.

Thumbstick backward means:
Two engines running full speed counter clockwise

Thumbstick left means:
Right engine running full speed clockwise
Left engine stops

Thumbstick right means:
Left engine running full speed clockwise
Right engine stops

I used “if” statement. So if vertical value of thumbstick (0-1023) exceeds ~600 then run both engines forward and if it goes below ~450 then do the exactly opposite.

If horizontal value (0-1023) exceeds ~600 then stop the right engine, run left engine at full speed, if it goes below ~450, then do the exactly opposite.

Forward and backwards alone work well, same as left & right alone. But when I add all codes together motors confuse which order to follow. Because when I move thumbstick horizontally, i change vertical value as well, which triggers forward move.

I’ve been thinking really hard to come up with a solution but I got nothing so far. I can use 2 thumbsticks but seems to me like that won’t solve my problem.

I need a code that will clearly distinguish horizontal and vertical commands. If one is triggered, the other one should stop immediately. Here’s my code so far. I’d be glad if anyone with experience could contribute and improve it. Thanks a lot guys…!

int vert;
int horz;
int back;
int forward;
int right;
int left;

void setup(){
pinMode(10, OUTPUT); //Right motor forward (Coonected to L298N controller with H-bridge)
pinMode(11, OUTPUT); //Right motor backwards (Coonected to L298N controller with H-bridge)
pinMode (12, OUTPUT); //Left motor backwards (Coonected to L298N controller with H-bridge)
pinMode (13,OUTPUT); // Left motor forward (Coonected to L298N controller with H-bridge)
pinMode(3,OUTPUT); //Adjusts speed of left motor (PWM)
pinMode(5,OUTPUT); //Adjusts speed of right motor (PWM)
pinMode(18, INPUT); //Thumbstick vertical
pinMode(19, INPUT); //Thumbstick horizontal
  
  
}
void loop() {
vert = analogRead(18);
horz = analogRead(19);

//*************
//MOVE FORWARD
//*************
if (analogRead(vert) >=600 ) {  
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite (10, HIGH);
digitalWrite (11, LOW);
forward=map(vert, 600, 1023, 0, 50); //  Begins at 600, ends at 1023
forward = constrain(forward, 0, 50); // forces the number to be within 0-50 range.
analogWrite(3, forward);
analogWrite(5, forward);


}
//***************
//MOVE BACKWARD
//***************
if (analogRead(vert) <= 500) { 
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite (10, LOW);
digitalWrite (11, HIGH);
back=map(vert, 500, 0, 0, 50); // Begins at 500, ends at 0.
back = constrain(back, 0, 50); // Forces the number to be within 0-50 range.
analogWrite(3, back);
analogWrite(5, back);
}


//***************
//TURN RIGHT
//***************
if (analogRead(horz) >= 600) { 
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite (10, HIGH);
digitalWrite (11, LOW);
right=map(horz, 600, 1023, 0, 50); // Begins at 600, ends at 1023.
right = constrain(right, 0, 50); //Forces the number to be within 0-50 range.
analogWrite(3, 0);
analogWrite(5, right);

}

//**************
//TURN LEFT
//**************
if (analogRead(horz) <= 500) { 
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite (10, LOW);
digitalWrite (11, LOW);
left=map(horz, 500, 0, 0, 50); // Begins at 500, ends at 0.
left = constrain(left, 0, 50); // Forces the number to be within 0-50 range.
analogWrite(3, left);
analogWrite(5, 0);

}

}

I posted this in the Arduino Playground not too long ago. Skid Steering

You should be able to figure out what you need to change, on your own.

HazardsMind:
I posted this in the Arduino Playground not too long ago. Skid Steering

You should be able to figure out what you need to change, on your own.

Thanks but whoa your code is really something... I just got my Arduino. It has been not long than few weeks, i'm still a huge noob in terms of electronics and c++ syntax. I hope i'll understand it.

I'm more like looking for someone who can improve my code.

vert = analogRead(18);
horz = analogRead(19);

if (analogRead(vert) >=600 )

if (analogRead(horz) <= 500)

What? You already have the values for vert and horz, and these don’t make any sense analogRead(vert), analogRead(horz)

forward=map(vert, 600, 1023, 0, 50); // Begins at 600, ends at 1023
forward = constrain(forward, 0, 50); // forces the number to be within 0-50 range.
analogWrite(3, forward);
analogWrite(5, forward);

Why 0 - 50, that going to go very slowly forward/reverse. Try 0 - 255 instead.

HazardsMind:
What? You already have the values for vert and horz, and these don't make any sense analogRead(vert), analogRead(horz)

Could you be more clear please?

HazardsMind:
Why 0 - 50, that going to go very slowly forward/reverse. Try 0 - 255 instead.

It's just for keeping motors silent. They make so much noise at full speed. I know I should use 0-255.

Instead of if (analogRead(vert) >=600 ) nonsense, just have if(vert >= 600) or if(analogRead(18) >= 600)

HazardsMind:
Instead of if (analogRead(vert) >=600 ) nonsense, just have if(vert >= 600) or if(analogRead(18) >= 600)

OK, thanks.

analogRead(18) ?

What are you running this code on?

AWOL:
analogRead(18) ?

What are you running this code on?

UNO R3. Pin 18 refers to A4.

Skid steering with a single joystick may involve some stick/motor operation conditions that may be mutually exclusive. Below is a previous discussion that might be of interest.

http://forum.arduino.cc/index.php/topic,157967.0.html