// Libraries for OLED and Stepper
#include <Wire.h>//Used for I2C communication, which is needed to control the OLED display
#include <Adafruit_GFX.h>//These are the libraries for controlling the OLED screen.
#include <Adafruit_SSD1306.h>//They help display text and graphics
// Define A4988 Pins
#define DIR_PIN 25//This pin controls the direction of the stepper motor (clockwise or counterclockwise).
#define STEP_PIN 26//This pin is used to generate step signals to the stepper motor driver, controlling its movement.
// OLED Display Setup
#define SCREEN_WIDTH 128//Define the size of the OLED screen
#define SCREEN_HEIGHT 64//Define the size of the OLED screen
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);//Initializes the OLED display using I2C communication.
// Motor Configuration
const int STEPS_PER_REV = 200; //The number of steps the motor takes to complete one full revolution. // Steps per revolution
const int MOTOR_SPEED = 500; //The delay between each step of the motor, controlling how fast the motor moves. // Step delay in microseconds
void setup() {
// Setup Motor Pins
pinMode(DIR_PIN, OUTPUT);//Set the motor pins as outputs.
pinMode(STEP_PIN, OUTPUT);//Set the motor pins as outputs.
// Initialize Serial Monitor
Serial.begin(115200);//Starts the serial communication to output debugging or status messages to the Serial Monitor.
// Initialize OLED Display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Directly use the I2C address 0x3C//Initializes the OLED display with the I2C address 0x3C.
Serial.println("OLED initialization failed");
while (true); // Stop here if initialization fails
}
display.clearDisplay();//.Clears the OLED screen.
display.setTextSize(1);//Sets the text size for the display.
display.setTextColor(SSD1306_WHITE);//Sets the text color to white.
display.setCursor(0, 0);// Sets the cursor position on the display to the top-left corner.
display.println("DOOR TO STORE THE BAGGAGE");//Displays a startup message on the OLED screen
display.display();
delay(2000);
}
void loop() {
// Open the Door
displayMessage("Door is Opening...");
digitalWrite(DIR_PIN, HIGH); // Clockwise//It moves the motor forward to open the door
moveStepper(STEPS_PER_REV * 5); // Rotate the motor forward
delay(5000); // Wait 2 seconds
// Close the Door
displayMessage("Door is Closed");
digitalWrite(DIR_PIN, LOW); // Counterclockwise//It moves the motor backward to close it
moveStepper(STEPS_PER_REV * 5); // Rotate the motor backward
delay(2000); // Pause before next cycle
}
// Function to Move Stepper Motor
void moveStepper(int steps) {
for (int i = 0; i < steps; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(MOTOR_SPEED);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(MOTOR_SPEED);
}
}
// Function to Display Messages
void displayMessage(const char* message) {
display.clearDisplay();
display.setCursor(0, 0);
display.println(message);
display.display();
Serial.println(message);
}