how to make this program work with two potenciometers

i've been working on this program and now i'm struggling to make it work with two potentiometers.
does anyone have idea how to do it?

program is made to control stepper motor.. i need it to control two stepper motors,

i'm using arduino UNO,

potentiometer is actually ps2 Joystick
but now i only have X-axis without Y-axis,

here's the code:

const int stepPin = 9;
const int dirPin = 8;
int limitS1 = 2;
int limitS2 = 3;
int customDelay,customDelayMapped;
void setup() {
   Serial.begin(9600);
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  pinMode(limitS1,INPUT);
  pinMode(limitS2,INPUT);
}
void loop() {
  {
  if (digitalRead(limitS1) == HIGH)
    return;
  if (digitalRead(limitS2) == HIGH)
    return;
  }
  int val = analogRead(A5);
  if((val > 490) && (val < 530))
  {
    digitalWrite(dirPin,LOW);
    return;
  }
  if((val > -1) && (val < 511))
  {
   digitalWrite(dirPin,LOW);
  }
  else
  {
   digitalWrite(dirPin,HIGH);
  }

  customDelayMapped = speedUp(); 
     if (customDelayMapped < 0)
     { // this part changes negative values from "int speedUp()"
      (customDelayMapped = -customDelayMapped); 
     }
  Serial.println(customDelayMapped);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);  
}
int speedUp() {
  int customDelay = analogRead(A5);
  int newCustom
  = map(customDelay, 0, 511, 4000, 0); // here i get center pozition of joystick ( if it goes down is positive value if it goes up is negative value)
  return newCustom;
}

limitS1 and S2 are limit stwiches, no need to change..

second potenciometer outputs i want to control with second potenciometer on arduino:

const int stepPin = 6;
const int dirPin = 7;

each iteration thru loop, use analogRead() to read the value of each pot and process both values.

separate reading input from processing it. I don't understand why you "return" if the value from A5 is near the center, aren't there other things to do and it doesn't allow the other pot to be read

the reason for this part is that i don't want stepper motors to move when potenciometer(Joystick) is near center..

  int val = analogRead(A5);
  if((val > 490) && (val < 530))
  {
    digitalWrite(dirPin,LOW);
    return;
  }

the same it would be for second potentiometer(joystick):

  int val = analogRead(A4);
  if((val > 490) && (val < 530))
  {
    digitalWrite(dirPin,LOW);
    return;
  }

Joystick is made of two potentiometers now with the program in first post i have it perfectly working just with X-axis now i would need it to work with Y-axis(second potentiometer)

l_nem:
the reason for this part is that i don't want stepper motors to move when potenciometer(Joystick) is near center.

so later in the code where you decide how to move each stepper, don't move the stepper if the pot value read is near the center.

aborting where you do prevents any other processing such as reading the other pot and moving the other motor

l_nem:
the reason for this part is that i don't want stepper motors to move when potenciometer(Joystick) is near center..

Rather than use return to prevent things happening I suggest you move into a separate function the code that actually moves the motor and only call that function when you want the motor to move.

I believe it will make the logic of the program easier to follow.

...R
Stepper Motor Basics
Simple Stepper Code

i don't know how to move part of code that moves the motor into a separate function

i would need it to move when potentiometer value is higher than 530 and lower than 490...

so now i'm asking for help with this because this is where my knowledge ends..

best i can do is:

void loop() {
  {
  if (digitalRead(limitS1) == HIGH)
    return;
  if (digitalRead(limitS2) == HIGH)
    return;
  }
  int val = analogRead(A5);
  if((val > -1) && (val < 511))
  {
   digitalWrite(dirPin,LOW);
  }
  else
  {
   digitalWrite(dirPin,HIGH);
  }
if(val < 490) 
{
  customDelayMapped = speedUp(); 

  Serial.println(customDelayMapped);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);  
}
if(val > 530) 
{
  customDelayMapped = speedUp(); 
     if (customDelayMapped < 0)
     {
      (customDelayMapped = -customDelayMapped);
     }
  Serial.println(customDelayMapped);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);  
}
}

without using return

void loop() {

  int val = analogRead(A5);
  if((val > 490) && (val < 530))
  {
    digitalWrite(dirPin,LOW);
    return;
  }

it makes more sense to step the motor based on the value of "val". However, i don't understand what is different between the two cases.

but looking at the section above that set the direction pin suggests that you want to go in one direction when the joystick in on one side and the reverse direction on the other. And the problem you have is you now step for two cases when "val" is above or below a value instead of testing for both ranges in one condition

if (val < 490 || 530 < val)

l_nem:
i don't know how to move part of code that moves the motor into a separate function

Have a look at how the code is organised in this Simple Stepper Code
and in Planning and Implementing a Program

...R

ok thanks for help i decided to just use two separate arduinos with same code...

i decided to just use two separate arduinos with same code...

I hope that you are joking !

Use a single Arduino. Read the two pots at the start of loop(). Write a block of code to move stepper 1 (or not) based on the reading from pot 1. Follow that with a block of code to move stepper 2 (or not) based on the reading from pot 2. Do NOT return if either of the pots is in the "do not move" zone, simply do not move the associated stepper

The two blocks of code (or a single block used twice) can conveniently be put in a function but if you are not sure how to do that simply put them in line in loop()

Do you have pulldown resistors (10k) on the limit switch pins?

here is the final code i will use for my project

it requires 2 arduino platforms

if anyone makes the code for single arduino please let me know

const int stepPin = 9;
const int dirPin = 8;
int limitS1 = 2;
int limitS2 = 3;
int customDelay,customDelayMapped;
void setup() {
   Serial.begin(9600);
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  pinMode(limitS1,INPUT);
  pinMode(limitS2,INPUT);
}
void loop() {
  {
  if (digitalRead(limitS1) == HIGH)
    return;
  if (digitalRead(limitS2) == HIGH)
    return;
  }
  motorA();
}
void motorA(){
  int val = analogRead(A5);
  if((val > -1) && (val < 511))
  {
   digitalWrite(dirPin,LOW);
  }
  else
  {
   digitalWrite(dirPin,HIGH);
  }
if (val < 490 || 530 < val) 
{
  customDelayMapped = speedUp(); 
     if (customDelayMapped < 0)
     {
      (customDelayMapped = -customDelayMapped);
     }
     (customDelayMapped = 4000 - customDelayMapped);
     if (customDelayMapped < 150)
     {
      (customDelayMapped = 150);
     }
  Serial.println(customDelayMapped);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);  
}

}
int speedUp() {
  int customDelay = analogRead(A5);
  int newCustom
  = map(customDelay, 0, 511, 4000,0);
  return newCustom;
}

here is the final code i will use for my project

it requires 2 arduino platforms

I give up

UKHeliBob:
I give up

look i tried my best to make it work on one platform but i don't have knowledge how to do it
this is project i need to make for school and i need it tomorrow in class so that was best solution for me

l_nem:
look i tried my best to make it work on one platform but i don't have knowledge how to do it
this is project i need to make for school and i need it tomorrow in class so that was best solution for me

I can understand that. Trying to meet a deadline and learn new techniques can be too stressful.

But now that you have a solution for your school project may I suggest that you take time over the next week to figure out how to do it with a single Arduino. That way you will significantly improve your programming expertise.

...R