// Motor control pins
#define STEP_PIN 18 // Servo Signal Pin (attached to the servo motor)
#define UPPER_LIMIT 21 // Upper limit switch (simulated with button)
#define LOWER_LIMIT 22 // Lower limit switch (simulated with button)
#define BUTTON_PIN 23 // Manual override push button
// Initial servo position
int servoPos = 90; // Initial servo position (in degrees)
void setup() {
// Initialize serial monitor
Serial.begin(115200);
// Set the STEP_PIN as an output
pinMode(STEP_PIN, OUTPUT);
// Set pin modes for limit switches and button
pinMode(UPPER_LIMIT, INPUT_PULLUP);
pinMode(LOWER_LIMIT, INPUT_PULLUP);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Move servo to the initial position (e.g., 90 degrees)
analogWrite(STEP_PIN, map(servoPos, 0, 180, 0, 255)); // Simulate PWM to control servo
Serial.println("Servo moved to initial position (90 degrees).");
}
void loop() {
// Read manual button state
if (digitalRead(BUTTON_PIN) == LOW) {
manualControl();
}
// Stop motor if limit switches are triggered
if (digitalRead(UPPER_LIMIT) == LOW) {
servoPos = 0; // Stop the motor (or reset to default position)
analogWrite(STEP_PIN, map(servoPos, 0, 180, 0, 255));
Serial.println("Upper limit reached!");
} else if (digitalRead(LOWER_LIMIT) == LOW) {
servoPos = 180; // Stop the motor (or reset to max position)
analogWrite(STEP_PIN, map(servoPos, 0, 180, 0, 255));
Serial.println("Lower limit reached!");
}
delay(100); // Small delay to debounce button presses
}
// Manual control function to move the servo up or down
void manualControl() {
if (servoPos < 180) {
servoPos += 10; // Increment position by 10 degrees
analogWrite(STEP_PIN, map(servoPos, 0, 180, 0, 255)); // Simulate PWM to control servo
Serial.print("Moving up to: ");
Serial.println(servoPos);
} else {
Serial.println("Maximum height reached");
}
delay(500); // Delay to simulate button press duration
}