How to incorporate a potentiometer into code for 2 separate devices?

Hello,

I am driving a stepper motor and an X-Slide(also a stepper motor) separately with the code I have here. Right now, I also have two potentiometers hooked up on breadboards to control the speed of each.
This is working out great, however, I need to get rid of controlling the potentiometer from the breadboard.

My question is if it is possible to control the Potentiometer in my code alone, and for both devices at the same time. Any ideas of what code I should add/delete/alter?

Any help is appreciated. Thanks a lot.

#include <Stepper.h>


int forward = 10;
int reverse = 7;
int dir_cha = 12;
int dir_chb = 13;
int pwm_cha = 3;
int pwm_chb = 11;
int brake_cha = 9;
int brake_chb = 8;

Stepper motor(200, dir_cha, dir_chb);            


void setup() {
  pinMode(forward,INPUT);
  pinMode(reverse,INPUT);
  pinMode(pwm_cha, OUTPUT);
  pinMode(pwm_chb, OUTPUT);
  pinMode(brake_cha, OUTPUT);
  pinMode(brake_chb, OUTPUT);
  digitalWrite(pwm_cha, HIGH);
  digitalWrite(pwm_chb, HIGH);
  digitalWrite(brake_cha, LOW);
  digitalWrite(brake_chb, LOW);
   Serial.begin(9600);
}


void loop() {
  int Speed = analogRead(A0);
  int RPM = map(Speed, 0, 1023, 0, 100);
  int f = digitalRead(forward);
  int r = digitalRead(reverse);
  if(f == 1 && r == 0 && RPM > 1){
  motor.step(1);
    motor.setSpeed(RPM);
    delay(1);
  }
  if(r == 1 && f== 0  && RPM > 1){
   motor.step(-1);
   motor.setSpeed(RPM);
      delay(1);
  }
  delay(5);
    Serial.println(RPM);
}

tesan1: if it is possible to control the Potentiometer in my code alone, and for both devices at the same time

The potentiometer is an input - it is not something you can control. In the sketch below you seem to have just one potentiometer and one stepper. Do you mean you want to have two physical Arduinos electrically connected to the same potentiometer? There's no reason why that wouldn't work, as far as I can see, as long as both Arduinos are running at the same voltage and the grounds are connected.

No I mean that I would like to have two stepper motors, with their speeds controlled by one potentiometer connected to a breadboard. But instead of physically turning the potentiometer, I would like to control its speed (0-1023) by changing numbers in my code only. Is this possible to do?

How are you going to change the numbers in your code ? If you mean by editing it, then look for the variable that is set by reading the potentiometer, stop it being read and set the value yourself in the code. Is that what you mean ?

Please post your current program if you want more exact advice

Yes, that is what I mean. I want to physically type in a number between 0-1023 which will put the speed of my steppers wherever I please.

The program that I posted is my current one.

I dont know how to do it, but im pretty sure you can use the serial monitor to type the value in, and have the code save that to an array, that array can then be used as a variable,

something like that anyway, maybe it will serve you as a pointer until someone who knows more what they are doing sorts you out with an answer,

tesan1: No I mean that I would like to have two stepper motors, with their speeds controlled by one potentiometer connected to a breadboard. But instead of physically turning the potentiometer, I would like to control its speed (0-1023) by changing numbers in my code only. Is this possible to do?

If the stepper motors are being controlled by the Arduino then you would just remove the code to read the speed from the potentiometer and replace it with a constant value, or a dynamic value that can be updated via the serial port.

If the stepper motors are controlled by some other device with the speed set by a potentiometer and you want to disconnect the potentiometer and replace it by the Arduino then you can do that, but you will need to provide an analog output. The Due has true analog output so you could use that without any extra hardware, but for any of the other Arduinos you'd need to provide some extra hardware to generate the analog output voltage. For example you could use a digital potentiometer, or make up your own equivalent from a resistor ladder, or put a smoothing circuit onto a pwm analog output.