#include <IRremote.h> // IR Remote Library
#include <AccelStepper.h> // AccelStepper Library for stepper motor
// Define Sensor Pin
const int RECV_PIN = 4;
// Define Stepper Motor Pins
const int STEP_PIN = 6;
const int DIR_PIN = 7;
const int EN_PIN = 5;
// Create a stepper object
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
// Define variables
const int STEP_INCREMENT = 2500; // STEPS TO MOVE WITH EACH BUTTON PRESS
const int MAX_POSITION = 5000; // Maximum position (adjust as needed)
const int MIN_POSITION = -5000; // Minimum position (adjust as needed)
int currentPosition = 0; // Track stepper position
// Initialize last received code variable
unsigned long lastCode;
void setup() {
// Set up the enable pin
pinMode(EN_PIN, OUTPUT);
Serial.begin(9600); // Start Serial communication
pinMode(LED_BUILTIN, OUTPUT); // Built-in LED as an indicator
// Start the IR receiver
IrReceiver.begin(RECV_PIN, ENABLE_LED_FEEDBACK);
// Disable the stepper motor
digitalWrite(EN_PIN, HIGH); // LOW = Enable, HIGH = Disable
// Configure the stepper motor
stepper.setMaxSpeed(200); // Set maximum speed (steps per second)
stepper.setAcceleration(60); // Set acceleration (steps per second^2)
Serial.println("Setup complete. Ready to receive IR signals.");
}
void loop() {
if (IrReceiver.decode()) { // Check if an IR code is received
unsigned long code = IrReceiver.decodedIRData.decodedRawData;
Serial.print("Received code: 0x");
Serial.println(code, HEX);
if (code == 0xFFFFFFFF) {
// Handle repeat codes
code = lastCode;
}
if (code == 0xB847FF00) { // Left button pressed
Serial.println("Left button pressed. Moving stepper motor clockwise.");
digitalWrite(LED_BUILTIN, HIGH); // Flash LED as feedback
delay(75);
digitalWrite(LED_BUILTIN, LOW);
if (currentPosition + STEP_INCREMENT <= MAX_POSITION) {
digitalWrite(EN_PIN, LOW); // Enable the motor
currentPosition += STEP_INCREMENT; // Increment position
stepper.moveTo(currentPosition); // Move stepper to new position
} else {
Serial.println("Maximum position reached. Cannot move further.");
}
}
if (code == 0xBA45FF00) { // Right button pressed
Serial.println("Right button pressed. Moving stepper motor counter-clockwise.");
digitalWrite(LED_BUILTIN, HIGH); // Flash LED as feedback
delay(75);
digitalWrite(LED_BUILTIN, LOW);
if (currentPosition - STEP_INCREMENT >= MIN_POSITION) {
digitalWrite(EN_PIN, LOW); // Enable the motor
currentPosition -= STEP_INCREMENT; // Decrement position
stepper.moveTo(currentPosition); // Move stepper to new position
} else {
Serial.println("Minimum position reached. Cannot move further.");
}
}
if (code == 0xB946FF00) { // Center button pressed
Serial.println("Center button pressed. Returning stepper motor to center position.");
digitalWrite(LED_BUILTIN, HIGH); // Flash LED as feedback
delay(75);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(EN_PIN, LOW); // Enable the motor
currentPosition = 0; // Reset position to center
stepper.moveTo(currentPosition); // Move stepper to center
}
lastCode = code; // Store the last received code
delay(30); // Short delay to avoid false readings
IrReceiver.resume(); // Ready to receive the next signal
}
// Continuously run the stepper motor to reach the target position
stepper.run();
// Disable the motor when the target position is reached
if (stepper.distanceToGo() == 0) {
digitalWrite(EN_PIN, HIGH);
}
}