Can't control stepper and servo with joystick

Hello there,

Just wanna open by saying I'm as new to using Arduino as one could be so I could very well be making a very obvious mistake.

I'm trying to control a servo motor and a stepper motor with a joystick, x-axis for the servo and y-axis for the stepper. I've gathered code from videos for each individually, however when I merge them, the code for the servo stops working (the stepper works just fine though). They each work when running individually.

I'm sure everything is wired properly aswell.

If someone can help me understand why this does not work I'd be very thankful.

Here's the code:

#include <Servo.h>
#include <Stepper.h>

Servo servo;

int x_axis;
int servo_val;

// define number of steps per revolution
#define STEPS 32

// define stepper motor control pins
#define IN1 10
#define IN2 11
#define IN3 12
#define IN4 13

// initialize stepper library
Stepper stepper(STEPS, IN4, IN2, IN3, IN1);

// joystick pot output is connected to Arduino A1
#define joystick A1

void setup(){
pinMode(A0,INPUT);
servo.attach(3);
}

void loop(){

x_axis=analogRead(A0);

servo_val=map(x_axis,0,1023,0,180);

servo.write(servo_val);

// read analog value from the potentiometer
int val = analogRead(joystick);

// if the joystic is in the middle ===> stop the motor
if( (val > 500) && (val < 523) )
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}

else
{
// move the motor in the first direction
if (val >= 523)
{
// map the speed between 5 and 500 rpm
int speed_ = map(val, 523, 1023, 5, 1000);
// set motor speed
stepper.setSpeed(speed_);

  // move the motor (1 step)
  stepper.step(1);

  val = analogRead(joystick);
}

// move the motor in the other direction
if (val <= 500)
{
  // map the speed between 5 and 500 rpm
  int speed_ = map(val, 500, 0, 5, 1000);
  // set motor speed
  stepper.setSpeed(speed_);

  // move the motor (1 step)
  stepper.step(-1);

  val = analogRead(joystick);
}

}

}

Did you mean "A0" here?
I think you want A0 as X and A1 as Y... so you would need to #define both, pinMode() both and analogRead() both

Thanks for helping first of all.
Alright so you mean I would have my code something like this? (The rest of the code staying the same)

#define joystickY  A1
#define joystickX  A0

void setup(){
  pinMode(A0,INPUT);
  servo.attach(3);
  pinMode(A1,INPUT);
}

void loop(){

  x_axis=analogRead(joystickX);

  servo_val=map(x_axis,0,1023,0,180);

  servo.write(servo_val);

The servo still doesn't move if I run this program, only the stepper motor.

Are both motors Is the servo powered externally?

Oh, I thought just connecting the arduino to my PC would be enough to power the two things. Guess not.

I have 9V Elegoo battery but it doesn't seem to be working. From what I have seen people have said a 9V battery isn't a very good power source because it handles very low current, so I guess that's why.
I'm going to get a 9V adapter and see if it works.
Thank you very much.

A 5vdc power converter (telephone charger) might be able to power your motors (check their specifications).

Alright, thank you so much for the help, really grateful.
Have a good one.

: )

So I can do this now? :: ))

#include <Servo.h>
#include <Stepper.h>

Servo servo;

int x_axis;
int servo_val;

// define number of steps per revolution
#define STEPS 32

// define stepper motor control pins
#define IN1 10
#define IN2 11
#define IN3 12
#define IN4 13

// initialize stepper library
Stepper stepper(STEPS, IN4, IN2, IN3, IN1);

// joystick pot output is connected to Arduino A1
#define joystick A1

void setup() {
  pinMode(A0, INPUT);
  servo.attach(3);
}

void loop() {
  x_axis = analogRead(A0);

  servo_val = map(x_axis, 0, 1023, 0, 180);

  servo.write(servo_val);

  // read analog value from the potentiometer
  int val = analogRead(joystick);

  // if the joystic is in the middle ===> stop the motor
  if ((val > 500) && (val < 523)) {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
  }

  else {
    // move the motor in the first direction
    if (val >= 523) {
      // map the speed between 5 and 500 rpm
      int speed_ = map(val, 523, 1023, 5, 1000);
      // set motor speed
      stepper.setSpeed(speed_);

      // move the motor (1 step)
      stepper.step(1);

      val = analogRead(joystick);
    }

    // move the motor in the other direction
    if (val <= 500) {
      // map the speed between 5 and 500 rpm
      int speed_ = map(val, 500, 0, 5, 1000);
      // set motor speed
      stepper.setSpeed(speed_);

      // move the motor (1 step)
      stepper.step(-1);

      val = analogRead(joystick);
    }
  }
}

I'm guessing so, but once I get the adapter I can tell you for sure if you want.

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