#include <Stepper.h>
#include <Servo.h>
// Define the number of steps per revolution for your stepper motor
const int stepsPerRevolution = 200;
// Create a Stepper object
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
// Define servo and LED pin
const int servoPin = 17; // Change to your servo pin number
const int trayPresentLED = 2; // Change to your LED pin number
const int ldrPin = A1; // Connect the AO pin of the 4-pin LDR sensor to this analog pin
Servo myServo;
void setup() {
// Set up the stepper motor
myStepper.setSpeed(50); // Set the motor speed (adjust as needed)
// Set up servo motor
myServo.attach(servoPin);
// Set pin modes for LED and LDR
pinMode(trayPresentLED, OUTPUT);
}
void loop() {
// Read the LDR sensor value
int ldrValue = analogRead(ldrPin);
// Check if a tray is present using the LDR sensor
bool trayPresent = ldrValue < 500; // Adjust the threshold value as needed
// Control the LED based on tray presence
digitalWrite(trayPresentLED, trayPresent ? HIGH : LOW);
// Move the stepper motor to the next floor if a tray is inserted
if (trayPresent) {
moveStepper(); // Move to the next floor
delay(2000); // Adjust delay as needed for the stepper motor to reach the next floor
} else {
returnServoHome(); // Return the servo to the home position if no trays are present
}
}
// Function to move the stepper motor to the next floor
void moveStepper() {
myStepper.step(stepsPerRevolution); // Move to the next floor (1 revolution)
delay(500); // Adjust delay as needed for the stepper motor movement
}
// Function to return the servo motor to the home position
void returnServoHome() {
myServo.write(0); // Adjust the angle (0 degrees) to your home position
delay(1000); // Adjust delay as needed for the servo to reach the home position
}