Linear actuator, motor driver, and joystick

Hello, does anyone have a setup and code that can control a linear actuator with a joystick through a motor driver (L298N).

int joystrick1 = A1;
int joystrick2 = A2;
int val1;
int val2;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
int val1 = analogRead(joystrick1);
int val2 = analogRead(joystrick2);

if(val1< 480){
val1 = map(val1, 1, 480, 255, 0);

analogWrite(6,val1);
analogWrite(5,0);
}

if(val1> 540){
val1 = map(val1, 530, 1023, 0, 255);

analogWrite(5,val1);
analogWrite(6,0);
}

if(val1 > 491 && val1 < 529){
val1 = map(val1, 491, 529, 0, 0);

analogWrite(5,val1);
analogWrite(6,val1);
}

if(val1< 480){
val2 = map(val2, 1, 490, 255, 0);

analogWrite(10,val2);
analogWrite(9,0);
}

if(val1> 540){
val2 = map(val2, 530, 1023, 0, 255);

analogWrite(9,val2);
analogWrite(10,0);
}


if(val2 > 491 && val2 < 529){
val2 = map(val2, 491, 529, 0, 0);

analogWrite(9,val2);
analogWrite(10,val2);
}

Serial.println(val1);
Serial.println(val2);
}

This is what we have but it is very finicky with our setup and I am looking for better suggestions.

We don't write code for you, we help you to write code. So I suggest that you start looking at code for the components that you want to use, understand them and merge them.

Hello 1964968484

Welcome to the worldbest Arduino forum.

This is a nice project to get started.

Keep it simple and stupid firstly.
Run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.

Have a nice day and enjoy coding in C++.

In general, you should not use magic numbers. The I/O pins love to have a functional name.

Please explain more

There is example code for a number of different items in the IDE. Use the tag "files/example". Always make code in small chunks like Arduino + joystick (potetntiometer twice), Arduino + motor controll. When they work separately You can use the pot value to affect how the motor runs.

Like You did analogReading the pot! analogWrite(motorPinA, val );

Find the autoformat in thr IDE, ot use Ctr + T, to make the code even more readable.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.