AccellStepper, Having issues, I am sure is my code

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 

These look suspicious:

I would guess that analogRead(???) does not often equal HIGH or LOW.

Also these look suspicious:

With the "int" in front, they look more like function declarations than calls.

Do the AccelStepper example codes work? You might be able to eliminate your code as the culprit if the samples show the same issue.

Humming is often a symptom of poor power.

If you used manifest constants for the pins the code would be clearer and I would be able to tell you weren't referring to the wrong pins.

analogRead(A3) == LOW)) 

// ... 
  analogRead(3) == HIGH) 

Is one of those a typo?

Most analog inputs can be used as digital inputs, so I don't know why you don't.

And LOW is 0, HIGH is 1, so those def will not be good tests.

Does the code even compile?

a7

It compiled silently in Wokwi, and then later I tried it in Arduino IDE and got warnings:

/private/var/folders/7g/2b2t5vv15cg3yrp46fm2yv7h0000gt/T/.arduinoIDE-unsaved2025210-23129-14dfvwg.1qbn/sketch_mar10a/sketch_mar10a.ino: In function 'void loop()':
/private/var/folders/7g/2b2t5vv15cg3yrp46fm2yv7h0000gt/T/.arduinoIDE-unsaved2025210-23129-14dfvwg.1qbn/sketch_mar10a/sketch_mar10a.ino:53:32: warning: expression list treated as compound expression in initializer [-fpermissive]
   int runIn(posValue, feedValue);
                                ^
/private/var/folders/7g/2b2t5vv15cg3yrp46fm2yv7h0000gt/T/.arduinoIDE-unsaved2025210-23129-14dfvwg.1qbn/sketch_mar10a/sketch_mar10a.ino:53:32: warning: left operand of comma operator has no effect [-Wunused-value]
/private/var/folders/7g/2b2t5vv15cg3yrp46fm2yv7h0000gt/T/.arduinoIDE-unsaved2025210-23129-14dfvwg.1qbn/sketch_mar10a/sketch_mar10a.ino:56:34: warning: expression list treated as compound expression in initializer [-fpermissive]
   int cutNeck(feedValue, posValue);

I think it declares a variable and uses what is in the parentheses as an a c++ "direct initializer". Exactly like AccelStepper stepper(1,2,3);, but instead of the object being some class thing that gets constructed, the object is instead a simple int that gets "constructed".

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.