#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <AccelStepper.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define motorPin1 2 // IN1 on the ULN2003
#define motorPin2 18 // IN2 on the ULN2003
#define motorPin3 16 // IN3 on the ULN2003
#define motorPin4 23 // IN4 on the ULN2003
// Set up the stepper motor (4-wire configuration)
AccelStepper Stepper(AccelStepper::FULL4WIRE, motorPin1, motorPin2, motorPin3, motorPin4);
void setup() {
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
Stepper.setMaxSpeed(1000);
Stepper.setAcceleration(500);
Stepper.setSpeed(50);
displayMessage("Stepper Motor Ready");
}
void loop() {
displayMessage("Rotating Clockwise");
Stepper.moveTo(50);
while (Stepper.distanceToGo() != 0) {
Stepper.run();
}
delay(1000);
displayMessage("Rotating Counterclockwise");
Stepper.moveTo(-200); // Rotate back 200 steps
while (Stepper.distanceToGo() != 0) {
Stepper.run();
}
delay(1000);
}
void displayMessage(String message) {
display.clearDisplay();
display.setCursor(0, 10);
display.println(message);
display.display();
}