#include <Stepper.h>
// Define the pins for each motor
#define PUL1_PIN 2 // Pulse pin for motor 1
#define DIR1_PIN 3 // Direction pin for motor 1
#define PUL2_PIN 4 // Pulse pin for motor 2
#define DIR2_PIN 5 // Direction pin for motor 2
// Define the steps per revolution for each motor
const int stepsPerRevolution1 = 200;
const int stepsPerRevolution2 = 200;
// Create stepper objects for each motor
Stepper motor1 (stepsPerRevolution1, PUL1_PIN, DIR1_PIN);
Stepper motor2 (stepsPerRevolution2, PUL2_PIN, DIR2_PIN);
// Define the delay for the second motor in milliseconds
const int delay2 = 20000; // 20 seconds
// Define a variable to store the start time of the first motor
unsigned long startTime1;
void setup()
{
// Set the speed for each motor
motor1.setSpeed (30); // 30 rpm
motor2.setSpeed (30); // 30 rpm
}
void loop()
{
// Step the first motor forward
void stepOne(stepsPerRevolution1,PUL1_PIN,HIGH);
delay (1000); // Wait for 1 second
// Step the first motor backward
void stepOne(stepsPerRevolution1,DIR1_PIN,LOW);
delay (1000); // Wait for 1 second
}
// A function that steps one motor and starts the second motor after a delay
void stepOne(int steps1, int dir1)
{
// Record the start time of the first motor
startTime1 = millis ();
// Loop for the number of steps
for (int i = 0; i < steps1; i++)
{
// Step the first motor
motor1.step (dir1);
// Check if the delay for the second motor has passed
if (millis () - startTime1 >= delay2)
{
// Step the second motor in the same direction as the first motor
motor2.step (dir1);
}
// Delay between steps
delay (10);
}
}