#include <Stepper.h>
int stepper_speed = 30;
// 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;
int Number_of_Revolutions = 1;
void setup()
{
Serial.begin(9600);
// Set the speed for each motor
motor1.setSpeed (stepper_speed); // 30 rpm
motor2.setSpeed (stepper_speed); // 30 rpm
Number_of_Revolutions = 1;
}
void loop()
{
// Step the first motor forward
stepOne(Number_of_Revolutions,true, 20);
delay (1000); // Wait for 1 second
// Step the first motor backward
stepOne(Number_of_Revolutions,true, 20);
delay (1000); // Wait for 1 second
}
// A function that steps one motor and starts the second motor after a delay
void stepOne(int steps1, bool dir, int delayof)
{
int onestep = -1;
if (dir == 1)
{
// forward
onestep=1;
}
// Record the start time of the first motor
startTime1 = millis ();
// the speed of the steppers relates to the timing
// eg 30 rpm = 30 / 60 for revs per second
// and 30/60 / 1000 is revs per msec
float delayrevs = stepper_speed / 6000.0;
Serial.print(stepper_speed );
Serial.print("\t");
Serial.println(delayrevs,5 );
// Loop for the number of steps
for (int i = 0; i < steps1; i++)
{
// Step the first motor
motor1.step (1);
// 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 (1);
}
// Delay between steps
delay (10);
}
}