#include <AccelStepper.h>
const int joyXPin = A0;
const int yButtonPin = 2;
const int relayButtonPin = 3;
const int relayPin = 4;
const int xStepPin = 9;
const int xDirPin = 10;
const int yStepPin = 5;
const int yDirPin = 6;
bool relayState = false;
const float stepPerInch = 200; // Adjust based on your motor and setup
AccelStepper xAxisMotor(AccelStepper::DRIVER, xStepPin, xDirPin);
AccelStepper yAxisMotor(AccelStepper::DRIVER, yStepPin, yDirPin);
void setup() {
Serial.begin(115200);
pinMode(yButtonPin, INPUT_PULLUP);
pinMode(relayButtonPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
xAxisMotor.setMaxSpeed(1000);
xAxisMotor.setAcceleration(500);
yAxisMotor.setMaxSpeed(1000);
yAxisMotor.setAcceleration(500);
}
void loop() {
int joyX = analogRead(joyXPin);
int xSpeed = map(joyX, 0, 1023, -1000, 1000); // Map joystick to speed range
xAxisMotor.setSpeed(xSpeed);
xAxisMotor.runSpeed();
int yButtonState = digitalRead(yButtonPin);
int relayButtonState = digitalRead(relayButtonPin);
// Move Y-axis motor 0.5 inches on button press
if (yButtonState == LOW) {
moveYMotor(0.5);
delay(300); // Debounce delay
}
// Toggle relay on button press
if (relayButtonState == LOW) {
delay(50); // Debounce delay
if (digitalRead(relayButtonState) == LOW) {
relayState = !relayState;
digitalWrite(relayPin, relayState ? HIGH : LOW);
Serial.print("Relay is now: ");
Serial.println(relayState ? "ON" : "OFF");
while (digitalRead(relayButtonPin) == LOW); // Wait until the button is released
}
}
delay(100);
}
void moveYMotor(float inches) {
int steps = inches * stepPerInch;
yAxisMotor.move(steps);
yAxisMotor.runToPosition();
}