// Define the step sequences
const int steps[8][4] = {
{1, 0, 0, 1}, // Step 1
{1, 0, 0, 0}, // Step 2
{1, 1, 0, 0}, // Step 3
{0, 1, 0, 0}, // Step 4
{0, 1, 1, 0}, // Step 5
{0, 0, 1, 0}, // Step 6
{0, 0, 1, 1}, // Step 7
{0, 0, 0, 1} // Step 8
};
// Define the output pins for the LEDs
const int outputPins[4] = {6, 7, 8, 9};
// Function to set the output pins according to the step sequence
void setStep(int step) {
for (int i = 0; i < 4; i++) {
digitalWrite(outputPins[i], steps[step][i]);
}
}
// Function to rotate the motor
void rotateMotor(int stepsPerRevolution, int numRevolutions, bool clockwise) {
int stepDelay = 500; // Delay between steps in microseconds
for (int rev = 0; rev < numRevolutions; rev++) {
for (int step = 0; step < stepsPerRevolution; step++) {
if (clockwise) {
setStep(step % 8);
} else {
setStep(7 - (step % 8));
}
delayMicroseconds(stepDelay);
}
}
}
void setup() {
// Initialize the output pins
for (int i = 0; i < 4; i++) {
pinMode(outputPins[i], OUTPUT);
}
// Rotate the motor 10 turns clockwise
rotateMotor(8, 10, true);
// Rotate the motor 10 turns counterclockwise
rotateMotor(8, 10, false);
}
void loop() {
// Nothing to do in the loop
}