/*Example sketch to control a stepper motor with DRV8825 stepper motor driver, AccelStepper library and Arduino: number of steps or revolutions. More info: https://www.makerguides.com */
// Include the AccelStepper library:
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 5
#define motorInterfaceType 1
//CNC configuration
int EN = 8;
const int start_button = A0; // the number of the pushbutton pin (abort button)
const int stop_button = 18; // the number of the pushbutton pin (hold button)
const int reset_button = 19; // 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
const int ledPin_reset = 11; // the number of the LED pin
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
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);
// initialize the stop LED pin as an output:
pinMode(ledPin_reset, OUTPUT);
// initialize the stop pushbutton pin as an input:
pinMode(reset_button, INPUT_PULLUP);
// set up interrupt function
attachInterrupt ( digitalPinToInterrupt ( stop_button ), stop, LOW );
// Set the maximum speed and acceleration:
stepper.setMaxSpeed(300);
stepper.setAcceleration(60);
// initialisation of LCD
lcd.init();
lcd.backlight();
lcd.setCursor(1, 0);
lcd.print("S1");
}
void(* resetFunc) (void) = 0;//declare reset function at address 0
void loop() {
if (digitalRead(start_button)==LOW){
digitalWrite(ledPin_start, HIGH);
delay(1000);
lcd.setCursor(1, 1);
lcd.print(stepper.currentPosition());
lcd.print(" ");
delay(2000);
lcd.clear();
// Set the target position:
stepper.move(200);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();
lcd.setCursor(1, 0);
lcd.print("S1");
lcd.setCursor(1, 1);
lcd.print(stepper.currentPosition());
lcd.print(" ");
delay(2000);
lcd.clear();
// Move to 100 steps clockwise
stepper.move(-100);
stepper.runToPosition();
lcd.setCursor(1, 0);
lcd.print("S1");
lcd.setCursor(1, 1);
lcd.print(stepper.currentPosition());
lcd.print(" ");
delay(2000);
lcd.clear();
// Move back to zero:
stepper.move(-100);
stepper.runToPosition();
lcd.setCursor(1, 0);
lcd.print("S1");
lcd.setCursor(1, 1);
lcd.print(stepper.currentPosition());
lcd.print(" ");
delay(2000);
lcd.clear();
//Move 50 steps counter-clockwise
stepper.move(50);
stepper.runToPosition();
lcd.setCursor(1, 0);
lcd.print("S1");
lcd.setCursor(1, 1);
lcd.print(stepper.currentPosition());
lcd.print(" ");
delay(2000);
lcd.clear();
delay(1000);
//Move 25 steps clockwise
stepper.move(-25);
stepper.runToPosition();
lcd.setCursor(1, 0);
lcd.print("S1");
lcd.setCursor(1, 1);
lcd.print(stepper.currentPosition());
lcd.print(" ");
delay(2000);
lcd.clear();
// Move back to zero:
stepper.moveTo(0);
stepper.runToPosition();
lcd.setCursor(1, 0);
lcd.print("S1");
lcd.setCursor(1, 1);
lcd.print(stepper.currentPosition());
lcd.print(" ");
delay(2000);
lcd.clear();
digitalWrite(ledPin_start, LOW);
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
}
}
void stop ( )
{ digitalWrite(ledPin_start, LOW);
delay(1000);//Wait 1000 milliseconds (1 second) before moving again
digitalWrite(ledPin_stop, HIGH);
delay(3000);//Wait 2000 milliseconds (1 second) before moving again
digitalWrite(ledPin_stop, LOW);
delay(1000);//Wait 2000 milliseconds (1 second) before moving again
resetFunc(); //call reset
lcd.clear();
}