#include <Stepper.h>
#define STEPS_PER_REV 2048
#define MIN_ANGLE 10
// Pin configuration for stepper motors
const int motor1Pin1 = 2;
const int motor1Pin2 = 3;
const int motor1Pin3 = 4;
const int motor1Pin4 = 5;
const int motor2Pin1 = 6;
const int motor2Pin2 = 7;
const int motor2Pin3 = 8;
const int motor2Pin4 = 9;
const int motor3Pin1 = 10;
const int motor3Pin2 = 11;
const int motor3Pin3 = 12;
const int motor3Pin4 = 13;
// Stepper motor objects and angles
Stepper motor1(STEPS_PER_REV, motor1Pin1, motor1Pin3, motor1Pin2, motor1Pin4);
Stepper motor2(STEPS_PER_REV, motor2Pin1, motor2Pin3, motor2Pin2, motor2Pin4);
Stepper motor3(STEPS_PER_REV, motor3Pin1, motor3Pin3, motor3Pin2, motor3Pin4);
int angle1 = 0;
int angle2 = 0;
// Pin configuration for LDR
const int ldrPin = A0;
const int ldrThreshold = 0;
void setup() {
// Set the speed and configure initial positions of the stepper motors
motor1.setSpeed(5);
motor2.setSpeed(5);
motor3.setSpeed(5);
motor1.step(STEPS_PER_REV / 36 * MIN_ANGLE);
angle1 += MIN_ANGLE;
motor2.step(-STEPS_PER_REV / 36 * MIN_ANGLE);
angle2 -= MIN_ANGLE;
}
void loop() {
// Rotate motor3 until minimal light value is achieved
int ldrValue = analogRead(ldrPin);
while (ldrValue > ldrThreshold) {
motor3.step(1);
delay(10);
ldrValue = analogRead(ldrPin);
}
// Rotate motor1 and motor2 until minimal light value is achieved
while (angle1 >= 0 && analogRead(ldrPin) > ldrThreshold) {
motor1.step(-STEPS_PER_REV / 36);
angle1--;
delay(10);
}
while (angle2 <= 360 && analogRead(ldrPin) > ldrThreshold) {
motor2.step(STEPS_PER_REV / 36);
angle2++;
delay(10);
}
// Rotate motor2 to achieve minimal light value of 0 in LDR
while (analogRead(ldrPin) > ldrThreshold) {
motor2.step(STEPS_PER_REV / 36);
angle2++;
delay(10);
}
// Print final angles
Serial.print("Motor1 angle: ");
Serial.println(angle1);
Serial.print("Motor2 angle: ");
Serial.println(angle2);
}
Make sure to connect your stepper motors and LDR sensor correctly to the Arduino and adjust the pin numbers in the code accordingly. Also, don't forget to set up the serial communication to see the output of the final angles.