#include <Stepper.h>
// Define the motor control pins
const int motorPin1 = 9;
const int motorPin2 = 10;
const int motorPin3 = 11;
const int motorPin4 = 12;
// Define the sequence for stepping the motor (you may need to adjust this for your motor)
const int steps[][4] = {
{1, 0, 0, 1}, // Step 1
{0, 1, 0, 1}, // Step 2
{0, 1, 1, 0}, // Step 3
{1, 0, 1, 0} // Step 4
};
// Define the delay between steps (adjust this for your motor and desired speed)
const int stepDelay = 10;
void setup() {
// Set motor control pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}
void loop() {
// Read the value from the photoresistor
int lightValue = analogRead(A0);
// Map the analog value to a suitable range for controlling the stepper motor speed or direction
int motorSpeed = map(lightValue, 0, 1023, 10, 50); // Adjust speed range as needed
// Step the motor in one direction
for (int i = 0; i < 4; ++i) {
setStep(steps[i][0], steps[i][1], steps[i][2], steps[i][3]);
delay(stepDelay);
}
// Adjust speed based on light intensity
delay(motorSpeed); // Adjust this delay to control speed
}
// Function to set the motor control pins according to the desired step
void setStep(int step1, int step2, int step3, int step4) {
digitalWrite(motorPin1, step1);
digitalWrite(motorPin2, step2);
digitalWrite(motorPin3, step3);
digitalWrite(motorPin4, step4);
}