Linear Actuator and Joystick

Hey you guys, I am having a problem when it comes to my thresholds with my joystick for my code. I am essentially trying to control the Y-movement of the linear actuator by moving the joystick and I am getting nowhere. My code is down below.

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 > 0 && val1 < 1023){

val1 = map(val1, 1, 500, 600, 0);

analogWrite(6,val1);

analogWrite(5,0);

}

if(val1 > 0 && val1 < 1023){

val1 = map(val1, 1, 600, 700, 300);

analogWrite(5,val1);

analogWrite(6,0);

}

if(val1 > 0 && val1 < 1023){

val1 = map(val1, 1, 700, 800, 0);

analogWrite(5,val1);

analogWrite(6,val1);

}

if(val2 > 0 && val2 < 1023){

val2 = map(val2, 1, 500, 600, 0);

analogWrite(10,val2);

analogWrite(9,0);

}

if(val2 > 0 && val2 < 1023){

val2 = map(val2, 1, 600, 700, 300);

analogWrite(9,val2);

analogWrite(10,0);

}

if(val2 > 0 && val2 < 1023){

val2 = map(val2, 1, 700, 800, 0);

analogWrite(9,val2);

analogWrite(10,val2);

}

Serial.println(val1);

Serial.println(val2);

}

hi.

to make the forum helps you, please edit your code post and format it with </> buttons.
Please provide a drawning of your circuitry, including arduino board pin and peripherals links and the way you power the board.

and sorry, but your code is such a mess (only a personnal opinion): for instance, this if statment
if(val1 > 0 && val1 < 1023){
is repeated all along the code. Doesn't make sense to me. Assuming the statment is true, it will successively trigger output pin with different values.

by the way: a joystick as yours give direct measurement of the axis position. Therefore, you may assume it is roughly 512 (half of 102", full scale) at rest (and not 0 for example). More: if you keep the joystick in one direction, the output value will change along the time because you hardly can keep a steady position (from the joystick point of view)

EDIT: what I propose you is to begin with smaller step. take time to deal with the values of the joystick, understand the value domains and how it reacts (at rest, max and min position...).
Then, you can transfer the joystick output in a function which drive the actuator.

int joystick1 = A1;
int val1;

void setup() {

Serial.begin(115200);
pinMode(joystick1 ,INPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {

val1 = analogRead(joystick1 );
Serial.println(val1);

if (val1< 480){
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
delay(500);
}
if (val1> 540){
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
delay(500);
}
}

the purpose of this sketch (I used your pin definition) is to make one actuator move in one direction for 0,5secondes when joystick is pressed in one direction. The actuator move in the opposite direction for 0,5 sec when joystick is pressed in another direction. The joystick value is continuously displayed in the serial monitor.

Note I used filtering values around 512 (normal rest position) to create a "dead zone". If your joystick won't give exact value of 512 at rest, my code make the actuator won't move.

Hope it will help!

What's wrong with readings of 0 or 1023?

(Also this is not an installation issue)

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project.

Thank you for your help, we are now just getting back to our project and ran your code but I am not sure it entirely works with the setup we currently have. We are incorporating an L298N Relay motor driver. Do you have a suggested setup we should use with your code?

it is time for more info.

Please take time to edit your code in first post and format it with tags </>, according to forum rules.
Can you provide datasheet and reference for your actuator? Also the model of your board, and very important: the way you power all the config (could be critical with L298N)

Will the L298N drive the actuator itself (I knwo very few about electric characteristic for an actuator, so the datasheet will be usefull to me or another helper) ?
I use it in a toy car (arduino based) to drive 2 DC motors and it works perfectly (the principle is a H-bridge, you can have many info on the net). The arduino board has 3 different pins on it:
2 are activated to select the direction of the rotation (forward or back) and the third one is like an ON/OFF switch (and can be power with PWM to make the rotor run at different speeds).
So I guess you can easily put it on your setup with very few changes in the code.

Keep in mind the L298N is known here as "dinosaur", don't be surprised if you are proposed to change for a more recent model.

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