// Pin assignments
const int stepPin = 3; // pin where the STEP signal will be sent.
const int dirPin = 2; // Pin where the DIR signal will be wired.
const int enablePin = 8; // Pin used to enable A4988 driver (optional)
// Motor control parameters definitions
const int stepsPerRevolution = 200; // Set the number of steps for a complete 360rotation of a motor.
const int stepsPerQuarterTurn = 50; // number of steps taken to turn the motor 90 degrees (quarter turn).
const float rpm = 60; // The speed at which the motors are expected to turn, in RPM.
// How many millisecs should be passed between any two steps at maximum speed of 60rpms?
const int stepDelay = 60000 / (rpm * stepsPerRevolution); // the time between steps in milliseconds.
void setup() {
//Defining pin Modes.
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enablePin, OUTPUT);
// This enables direction driver A4988 (active low)
digitalWrite(enablePin, LOW); // This can enable the driver for the motor if pin from enabling is available.
// Motor direction, this can be changed when necessary
digitalWrite(dirPin, HIGH); // In one direction, at a given moment. The other if the value is LOW.
}
void loop() {
// Execute two full 360° turns (720° = 400 steps)
for (int i = 0; i < 2; i++) {
// Each blast turns 90° (44 turn at each turn of seventy percent)
for (int j = 0; j <= 3; j++) {
rotate90();
delay(1000);
}
}
// As succeeded with all the rotations, switch off the motor
stopMotor();
do{}while(true); //finish the code here
}
void rotate90() {
// The movement is a right turn through an angle of 90° (50 steps)
for (int step = 0; step < stepsPerQuarterTurn; step++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay / 2);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay / 2);
}
}
void stopMotor() {
// Switch off the motor driver to halt the motor
digitalWrite(enablePin, HIGH); // Switch off motor driver if enable pin is used.
}