#include <Stepper.h>
#include <Servo.h>
// Define the number of steps per revolution for your stepper motors
const int stepsPerRevolution = 200;
// Define the connections for stepper motors
const int stepper1StepPin = 3;
const int stepper1DirPin = 4;
const int stepper2StepPin = 5;
const int stepper2DirPin = 6;
const int stepper3StepPin = 11;
const int stepper3DirPin = 12;
// Define connections for other components
const int ldrPin = A0;
const int ledPin = 2;
const int servoPin = 7;
// Create stepper motor objects
Stepper stepper1(stepsPerRevolution, stepper1StepPin, stepper1DirPin);
Stepper stepper2(stepsPerRevolution, stepper2StepPin, stepper2DirPin);
Stepper stepper3(stepsPerRevolution, stepper3StepPin, stepper3DirPin);
// Create servo motor object
Servo servoMotor;
void setup() {
// Set up the stepper motor pins
pinMode(stepper1StepPin, OUTPUT);
pinMode(stepper1DirPin, OUTPUT);
pinMode(stepper2StepPin, OUTPUT);
pinMode(stepper2DirPin, OUTPUT);
pinMode(stepper3StepPin, OUTPUT);
pinMode(stepper3DirPin, OUTPUT);
// Set up other component pins
pinMode(ldrPin, INPUT);
pinMode(ledPin, OUTPUT);
// Attach servo to its pin
servoMotor.attach(servoPin);
}
void loop() {
// Read LDR value
int ldrValue = analogRead(ldrPin);
// If LDR value indicates low light intensity
if (ldrValue < 500) {
// Turn on LED
digitalWrite(ledPin, HIGH);
// Rotate stepper1 continuously
stepper1.setSpeed(20); // Set the speed of stepper1 rotation
stepper1.step(1000); // Rotate stepper1 continuously
} else {
// Turn off LED
digitalWrite(ledPin, LOW);
// Stop stepper1
stepper1.setSpeed(0);
stepper1.step(0);
// Rotate stepper2 10 times clockwise
rotateStepper(stepper2, 10, true);
// Rotate servo clockwise 1 time
servoMotor.write(90); // Set servo to its initial position
delay(1000);
servoMotor.write(180); // Rotate servo clockwise
delay(1000);
// Rotate stepper3 90 degrees clockwise
rotateStepper(stepper3, 90, true);
// Rotate stepper2 10 times anticlockwise
rotateStepper(stepper2, 10, false);
// Start rotating stepper1 again
digitalWrite(ledPin, HIGH); // Turn on LED
stepper1.setSpeed(20); // Set the speed of stepper1 rotation
stepper1.step(1000); // Rotate stepper1 continuously
// Rotate servo anticlockwise
delay(1000);
servoMotor.write(90); // Rotate servo anticlockwise
delay(1000);
// Rotate stepper2 10 times clockwise
rotateStepper(stepper2, 10, true);
// Rotate stepper3 90 degrees anticlockwise
rotateStepper(stepper3, 90, false);
}
}
// Function to rotate stepper motor
void rotateStepper(Stepper stepper, int steps, bool clockwise) {
if (clockwise) {
stepper.setSpeed(20); // Set the speed of rotation
stepper.step(steps * stepsPerRevolution);
} else {
stepper.setSpeed(-20); // Set the speed of rotation
stepper.step(-steps * stepsPerRevolution);
}
}