#include <AccelStepper.h>
#include <IRremote.h>
// Define stepper motor connections and motor interface type
#define dirPinHorizontal 2
#define stepPinHorizontal 3
#define dirPinVertical 7
#define stepPinVertical 8
#define motorInterfaceType 1
// Define IR remote control pin
#define IR_RECEIVE_PIN 4
// Define proximity sensor pins
#define leftProximitySensor 5
#define rightProximitySensor 6
// Define the length of the board
#define BOARD_LENGTH 600
// Initialize stepper motors
AccelStepper stepperHorizontal(motorInterfaceType, stepPinHorizontal, dirPinHorizontal);
AccelStepper stepperVertical(motorInterfaceType, stepPinVertical, dirPinVertical);
// Initialize IR receiver
IRrecv irrecv(IR_RECEIVE_PIN);
decode_results results;
// Remote control command definitions
#define FORWARD 0xFF906F // Replace with your remote's forward button code
#define REVERSE 0xFFC23D // Replace with your remote's reverse button code
#define STOP 0xFFA857 // Replace with your remote's stop button code
bool isPaused = false;
bool isMoving = false;
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Initialize stepper motors
stepperHorizontal.setMaxSpeed(1000); // Set maximum speed
stepperHorizontal.setAcceleration(500); // Set acceleration
stepperVertical.setMaxSpeed(500); // Set maximum speed for vertical motion
stepperVertical.setAcceleration(250); // Set acceleration for vertical motion
stepperVertical.moveTo(BOARD_LENGTH); // Initial target position for vertical motion
// Initialize IR receiver
irrecv.enableIRIn();
// Initialize proximity sensors
pinMode(leftProximitySensor, INPUT);
pinMode(rightProximitySensor, INPUT);
}
void loop() {
if (irrecv.decode(&results)) {
// Check which button was pressed
switch (results.value) {
case FORWARD:
Serial.println("Forward");
if (!isPaused) {
stepperHorizontal.moveTo(BOARD_LENGTH);
isMoving = true;
}
break;
case REVERSE:
Serial.println("Reverse");
if (!isPaused) {
stepperHorizontal.moveTo(0);
isMoving = true;
}
break;
case STOP:
Serial.println("Stop/Pause");
if (isMoving) {
isPaused = !isPaused; // Toggle pause/play state
if (isPaused) {
stepperHorizontal.stop();
stepperVertical.stop();
isMoving = false;
} else {
stepperHorizontal.runToPosition();
stepperVertical.runToPosition();
isMoving = true;
}
}
break;
}
irrecv.resume(); // Receive the next value
}
// Read proximity sensors
if (digitalRead(leftProximitySensor) == LOW) {
Serial.println("Left edge detected, stopping motor");
stepperHorizontal.stop();
isMoving = false;
}
if (digitalRead(rightProximitySensor) == LOW) {
Serial.println("Right edge detected, stopping motor");
stepperHorizontal.stop();
isMoving = false;
}
// Run the motors if not paused and not at edges
if (isMoving && !isPaused) {
stepperHorizontal.run();
if (!stepperVertical.isRunning()) {
stepperVertical.moveTo(-stepperVertical.currentPosition()); // Move the vertical motor in a back and forth motion
}
stepperVertical.run();
}
}