#include <Arduino.h>
#include "A4988.h"
const int StepX = 2; //Step of stepper motor driver
const int DirX = 5; //Direction of stepper motor driver
int MS1 ; //GPIO7 in Arduino UNO --- MS1 for A4988
int MS2 ; //GPIO6 in Arduino UNO --- MS2 for A4988
int MS3 ; //GPIO5 in Arduino UNO --- MS3 for A4988
//Motor Specs
const int spr = 200; //Steps per revolution
int RPM = 25; //Motor Speed in revolutions per minute
int Microsteps = 1; //Stepsize (1 for full steps, 2 for half steps, 4 for quarter steps, etc)
//Providing parameters for motor control
A4988 stepper(spr, DirX, StepX, MS1, MS2, MS3);
//CNC configuration
int EN = 8;
const int start_button = A0; // the number of the pushbutton pin (abort button)
const int stop_button = A1; // the number of the pushbutton pin (hold button)
const int ledPin_start = 9; // the number of the LED pin (endstopx)
const int ledPin_stop = 10; // the number of the LED pin (endstopx)
void setup() {
Serial.begin(9600);
pinMode(EN, OUTPUT); // to enable CNC drivers
digitalWrite(EN, LOW); //Low to enable
// initialize the start LED pin as an output:
pinMode(ledPin_start, OUTPUT);
// initialize the start pushbutton pin as an input:
pinMode(start_button, INPUT_PULLUP);
// initialize the stop LED pin as an output:
pinMode(ledPin_stop, OUTPUT);
// initialize the stop pushbutton pin as an input:
pinMode(stop_button, INPUT_PULLUP);
pinMode(StepX, OUTPUT); //Step pin as output
pinMode(DirX, OUTPUT); //Direcction pin as output
digitalWrite(StepX, LOW); // Currently no stepper motor movement
digitalWrite(DirX, LOW);
delay(1000);//Wait 1000 milliseconds (1 second) proceeding
// Set target motor RPM to and microstepping setting
stepper.begin(RPM, Microsteps);
}
void loop() {
if (digitalRead(start_button)==LOW){
digitalWrite(ledPin_start, HIGH);
delay(1000);
stepper.move(-400);//Move 400 steps clockwise (angle 720)
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
stepper.move(400);//Move 400 steps counter-clockwise (angle 720)
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
stepper.move(-200);//Move 200 steps clockwise (angle 360)
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
stepper.move(100);//Move 100 steps counter-clockwise (angle 180)
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
stepper.move(100);//Move 100 steps counter-clockwise (angle 180)
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
stepper.move(20);//Move 20 steps counter-clockwise (angle 36)
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
stepper.move(-20);//Move 20 steps clockwise (angle 36)
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
stepper.move(25);//Move 25 steps counter-clockwise (angle 45)
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
stepper.move(25);//Move additional 25 steps counter-clockwise (angle 45)
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
stepper.move(-50);//Move 50 steps clockwise (angle 90)
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
digitalWrite(start_button, HIGH);
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
}
if (digitalRead(stop_button)==LOW){
digitalWrite(ledPin_stop, HIGH);
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
digitalWrite(start_button, HIGH);
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
}
if (digitalRead(start_button)==HIGH){
digitalWrite(ledPin_start, LOW);
digitalWrite(ledPin_stop, LOW);
}
}