#include <Servo.h>
// define the pins for the A4988 stepper motor driver
const int stepPin = 6;
const int dirPin = 5;
// define the pin for the servo motor
const int ServoPin = 3;
// set the maximum and minimum values of the potentiometer
const int potMin = 0;
const int potMax = 1023;
// set the maximum and minimum values of the joystick
const int joyMin = 0;
const int joyMax = 1023;
void setup()
{
// set the STEP and DIR pins as outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// attach the servo motor to the servoPin
Servo.attach(ServoPin);
}
void loop()
{
// read the value of the potentiometer
int potValue = analogRead(A0);
// map the potentiometer value to a range between -1 and 1
float dirValue = map(potValue, potMin, potMax, -1, 1);
// determine the direction of the motor rotation based on the potentiometer value
int dir = (dirValue >= 0) ? HIGH : LOW;
// set the DIR pin to the appropriate value
digitalWrite(dirPin, dir);
// toggle the STEP pin to move the motor one step
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
// read the value of the y-axis of the joystick
int joyValue = analogRead(A1);
// map the joystick value to a range between 0 and 180
int angle = map(joyValue, joyMin, joyMax, 0, 180);
// set the angle of the servo motor
Servo.write(angle);
}