Hello, my stepper motor just hums with this code.
Using A4988 and a nema 17 motor wiith a custom PCB.
Can someone point me where I went wrong please?
#include <AccelStepper.h>
// Define a stepper and the pins it will use
//AccelStepper stepper(AccelStepper::FULL4WIRE, 3, 2, 7);
#define motorInterfaceType 1
const int dirPin = 2;
const int stepPin = 3;
AccelStepper stepper(motorInterfaceType, stepPin, dirPin);
//#define dir 2
//#define step 3
#define EN 7
#define MS1 6
#define MS2 5
#define MS3 4
void setup()
{
pinMode(10, INPUT); //START SWITCH INPUT GO LOW
pinMode(A1, INPUT); //RETRACT SWITCH INPUT GO LOW
pinMode(A3, INPUT); //HOME SWITCH INPUT GO LOW
pinMode(EN, OUTPUT); //D7 IS OUTPUT ENABLE TO DRIVER
// pinMode(dir, OUTPUT); //D2 OUTPUT FOR STEP ON DRIVER
//pinmode(step, OUTPUT) //D3 IS STEP TO DRIVER
pinMode(MS1, OUTPUT); //D6 TO MS1 ON DRIVER
pinMode(MS2, OUTPUT); //D5 TO MS2 ON DRIVER
pinMode(MS3, OUTPUT); //D4 TO MS3 ON DRIVER
stepper.setMaxSpeed(20000.0); //SETTING MAX SPEED
stepper.setAcceleration(10000.0); //SETTING MAX ACCELERATION
Serial.begin(9600); //SERIAL BAUD RATE
digitalWrite(EN, HIGH); //HIGH to shut down, LOW to run
Serial.print("Current position = "); //PRINTING LINE CURRENT POSITION
Serial.println(stepper.currentPosition());
digitalWrite(MS1, HIGH);
digitalWrite(MS2, LOW);
digitalWrite(MS3, LOW);
}
void loop()
{
digitalWrite(EN, HIGH); //HIGH to shut down, LOW to run
int feedValue = ((1024 - analogRead(A2)) / 5) + 50; //THIS IS THE FUCTION FOR THE POT READ FOR FEEDRATE
int posValue = (analogRead(A4) * 1.3 + 5700); //THIS IS THE FORMULA FOR THE RAPID POSITION POT
if (posValue < 300)
posValue = 300;
if ((digitalRead(10) == LOW) && (analogRead(A3) == LOW)) //Start in from home. Must be at home position to start. D3 IS HOME SW D10 IS START BUTTON
digitalWrite(EN, LOW); //HIGH to shut down, LOW to run
int runIn(posValue, feedValue);
Serial.print("analogRead A3 = ");
Serial.println(analogRead(A3));
int cutNeck(feedValue, posValue);
stepper.setCurrentPosition(0); //THIS IS SETTING THE POSITION TO 0 TO START FROM HOME ON NEXT CYCLE
digitalWrite(EN, HIGH); //Disable stepper motor
}
int runIn(int posValue, int feedValue)
{ digitalWrite(EN, LOW); //Pull enable pin low to allow motor control
stepper.setMaxSpeed(1800); //THIS IS THE RAPID FORWARD SPEED
stepper.setAcceleration(10000);
stepper.runToNewPosition(posValue); //THIS IS THE VALUE CALCULATED FROM THE RAPID POSITION FUNCTION AND GOES THIS MANY COUNTS FROM HOME
stepper.stop(); //THIS STOPS THE RAPID FUNCTION
delay(400); //THIS IS THE DWELL PAUSE AFTER THE RAPID POSITION HAS BEEN MET
digitalWrite(EN, HIGH); //Pull enable pin low to allow motor control
}
int goBack(int posValue)
{
digitalWrite(EN, LOW); //Pull enable pin low to allow motor control
while (analogRead(3) == HIGH) //Return Home//
digitalWrite(MS1, HIGH);
digitalWrite(MS2, LOW);
digitalWrite(MS3, LOW);
stepper.setAcceleration(500);
stepper.setMaxSpeed(-1600);
stepper.setSpeed(-1600);
stepper.runSpeed();
digitalWrite(EN, HIGH); //Pull enable pin low to allow motor control
}
////////////////////////////////Cut TIP/////////////////////////////////////////////
int cutNeck(int feedValue, int posValue) {
digitalWrite(EN, LOW); //Pull enable pin low to allow motor control
int curPos1 = stepper.currentPosition();
stepper.setMaxSpeed(feedValue);
digitalWrite(MS1, HIGH);
digitalWrite(MS2, HIGH);
digitalWrite(MS2, HIGH);
stepper.move(4000);
while (analogRead(A1) == HIGH && (digitalRead(10) == HIGH)) {
stepper.run();
}
int curPos2 = stepper.currentPosition();
delay(1000); //This is the dwell time AT END OF CUT
stepper.stop();
digitalWrite(MS1, HIGH);
digitalWrite(MS2, LOW);
digitalWrite(MS2, LOW);
goBack(-posValue);
digitalWrite(EN, HIGH); //Pull enable pin low to allow motor control
} /////////////////End of cutting