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);
}
}
}