#include <Stepper.h>
// Define the number of steps per revolution for your stepper motor
const int stepsPerRevolution = 200;
// Define pin numbers for buttons and limit switches
const int upButtonPin = 3;
const int downButtonPin = 2;
const int upLimitPin = 5;
const int downLimitPin = 4;
// Define pins for stepper motor driver
const int dirPin = 8;
const int stepPin = 9;
// Create a Stepper object
Stepper myStepper(stepsPerRevolution, dirPin, stepPin);
void setup() {
// Set button pins as inputs
pinMode(upButtonPin, INPUT);
pinMode(downButtonPin, INPUT);
// Set limit switch pins as inputs with pull-up resistors
pinMode(upLimitPin, INPUT_PULLUP);
pinMode(downLimitPin, INPUT_PULLUP);
// Set direction and step pins as outputs
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
}
void loop() {
// Check if the up button is pressed
if (digitalRead(upButtonPin) == HIGH) {
// Check if the up limit switch is not pressed
if (digitalRead(upLimitPin) == HIGH) {
// Rotate the motor clockwise
digitalWrite(dirPin, HIGH); // Set direction to clockwise
while (digitalRead(upLimitPin) == HIGH) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500); // Adjust delay for motor speed
digitalWrite(stepPin, LOW);
delayMicroseconds(500); // Adjust delay for motor speed
}
}
}
// Check if the down button is pressed
if (digitalRead(downButtonPin) == HIGH) {
// Check if the down limit switch is not pressed
if (digitalRead(downLimitPin) == HIGH) {
// Rotate the motor counter-clockwise
digitalWrite(dirPin, LOW); // Set direction to counter-clockwise
while (digitalRead(downLimitPin) == HIGH) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500); // Adjust delay for motor speed
digitalWrite(stepPin, LOW);
delayMicroseconds(500); // Adjust delay for motor speed
}
}
}
}