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);
}
}