/*Arduino Sketch Controlling NEMA 17 Stepper Motor with A9488 driver
This code will help us control the stepper motor using the A9488 driver’s DIR and STEP pins.
*/
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
#include <Wire.h>
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
const byte stepPin = 7;
const byte dirPin = 6;
int home_switch = 9;
long TravelX ; // Used to store the X value entered in the Serial Monitor
int move_finished=1; // Used to check if move is completed
long initial_homing=-1; // Used to Home Stepper at startup
// Define a stepper and the pins it will use
AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin);
// Define I2C LCD
//the first parameter is the I2C address
//the second parameter is how many rows are on your screen
//the third parameter is how many columns are on your screen
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int steps_per_rev = 200; // define the steps per revolution. This is the number of steps our motor requires to move one complete revolution.
void setup() {
Serial.begin(115200); // establish the serial connection between the development board at a baud rate of 115200.
pinMode(home_switch, INPUT_PULLUP);
delay(5); // Wait for EasyDriver wake up
//initialize lcd screen
lcd.init();
// turn on the backlight
lcd.backlight();
lcd.setCursor(0,0); // tell the screen to write “hello, from” on the top row
lcd.print("Init");
delay(1000);
lcd.clear();
lcd.print("Stepper is Homing");
lcd.print(" ");
// Set Max Speed and Acceleration of each Steppers at startup for homing
myStepper.setMaxSpeed(10.0); // Set Max Speed of Stepper (Slower to get better accuracy)
myStepper.setAcceleration(10.0); // Set Acceleration of Stepper
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
myStepper.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
myStepper.run(); // Start moving the stepper
delay(5);
}
myStepper.setCurrentPosition(0); // Set the current position as zero for now
myStepper.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
myStepper.setAcceleration(100.0); // Set Acceleration of Stepper
initial_homing=1;
while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
myStepper.moveTo(initial_homing);
myStepper.run();
initial_homing++;
delay(5);
}
myStepper.setCurrentPosition(0);
lcd.clear();
lcd.println("Homing Completed");
lcd.println("");
delay(2000);
// configure the digital pins connected with STEP and DIR as output pins.
myStepper.setMaxSpeed(1000); // this limits the value of setSpeed(). Raise it if you like.
myStepper.setSpeed(200); // runSpeed() will run the motor at this speed - set it to whatever you like.
}
void loop() {
// To control the direction of the motor we will use the digitalWrite() function and pass the DIR pin as the first parameter and the state of the pin as the second parameter. To move the motor in the clockwise direction a high signal is passed to the DIR pin.
if (digitalRead(home_switch)) {
riseShow();
}
if (digitalRead(home_switch)){
recedeShow();
}
else {
lcd.clear();
lcd.blink();
lcd.println("End");
lcd.println("Kaboom");
myStepper.stop();
delay(9999999);
exit(0);
}
}
void riseShow(){
lcd.clear();
lcd.println("Spinning Clockwise.");
myStepper.moveTo(200);
myStepper.setAcceleration(800);
myStepper.runToPosition();
delay(1000); }
void recedeShow(){
lcd.clear();
lcd.println("Spinning AntiClockwise.");
myStepper.moveTo(-5000);
myStepper.setAcceleration(300);
myStepper.runToPosition();
delay(1000);
delay(1000); }