Accelstepper code

Hello ive been working on some code to for a simple powerfeed and got most of it working only things i now run in to is that when no switch is made the enable should be made high ie 5v at the moment it goes low to ground easily fixed in the wiring but is it also possible in the code ?
Second when a switch is selected and my 10k pot is all the way to 0 it still turns and i want it to then be still/stopped is there a way to fix that?

#include <AccelStepper.h>
#include <timeObj.h>
#include <mechButton.h>
#include <mapper.h>
#include <autoPOT.h>

#define DIR_PIN     2
#define STEP_PIN    3
#define CW_PIN      10
#define CCW_PIN     11
#define RAPID_PIN   12
#define SPEED_PIN   A0
#define ENABLE_PIN  4

#define PRINT_MS    1000
#define MAX_SPEED   1000


AccelStepper  stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
timeObj       printTimer(PRINT_MS);
mechButton    CWBtn(CW_PIN);
mechButton    CCWBtn(CCW_PIN);
mechButton    RapidBtn(RAPID_PIN);
autoPOT       speedPOT(SPEED_PIN);
mapper        speedMap(0,1023,0,MAX_SPEED);
enum states   { waiting, moveCW, rapidCW, moveCCW, rapidCCW };
states        ourState;
float         moveSpeed; 

void setup() {

  Serial.begin(115200);
  CWBtn.setCallback(CWClick);
  CCWBtn.setCallback(CCWClick);
  RapidBtn.setCallback(RapidClk);
  speedPOT.setCallback(newSpeed);
  Serial.println("The sketch has started");
  stepper.setMaxSpeed(MAX_SPEED); 
  stepper.setSpeed(0);
  ourState = waiting;
}


void CWClick(void) {

  switch(ourState) {
    case waiting  : 
      if (!CWBtn.getState()) {          // Switch grounded..
        if (CCWBtn.getState()) {        // CCW is NOT grounded..
          comMoveCW();
        }
      } else if (!CCWBtn.getState()) {  // CCW is grounded..
        comMoveCCW();                   // We go the other way!
      }
    break;
    case moveCW   : // Any of these, means stop. 
    case rapidCW  : 
    case moveCCW  :
    case rapidCCW : comStop(); break;
  }
}


void CCWClick(void) {

  switch(ourState) {
    case waiting  : 
      if (!CCWBtn.getState()) {       // Switch grounded..
        if (CWBtn.getState()) {       // CW is NOT grounded..
          comMoveCCW();
        }
      } else if (!CWBtn.getState()) { // CW is grounded..
        comMoveCW();                  // We go the other way!
      }
    break;
    case moveCW   :  // Any of these, means stop.  
    case rapidCW  :
    case moveCCW  :
    case rapidCCW : comStop(); break;
  }
}


void RapidClk(void) {
  
  switch(ourState) {
    case waiting  : break;
    case moveCW   : 
      if (!RapidBtn.getState()) {  // Switch grounded.
        comRapidCW();                 // Run up to rapid speed.
      }
    break;
    case rapidCW  :
      if (RapidBtn.getState()) {   // Switch released.
        comMoveCW();                  // Slow down to move speed.
      }
    break;
    case moveCCW  : 
      if (!RapidBtn.getState()) {  // Switch grounded.
        comRapidCCW();                // Run up to rapid speed.      
      }
    break;
    case rapidCCW :
      if (RapidBtn.getState()) {   // Switch released.
        comMoveCCW();                 // Slow down to move speed.
      }
    break;
  }
}


void newSpeed(int newValue) {
  

  moveSpeed = speedMap.map(newValue);

  switch(ourState) {
    case waiting  : break;
    case moveCW   : stepper.setSpeed(moveSpeed); break;
    case moveCCW  : stepper.setSpeed(-moveSpeed); break;
    case rapidCW  : // These two do nothing.
    case rapidCCW : break;
  }
}


void comMoveCW(void) {

  stepper.setSpeed(moveSpeed);
  digitalWrite(ENABLE_PIN, LOW);
  ourState = moveCW;
}


void comMoveCCW(void) {

  stepper.setSpeed(-moveSpeed);
  digitalWrite(ENABLE_PIN, LOW);
  ourState = moveCCW;
}


void comRapidCW(void) {

  stepper.setSpeed(MAX_SPEED);
  ourState = rapidCW;
}


void comRapidCCW(void) {

  stepper.setSpeed(-MAX_SPEED);
  ourState = rapidCCW;
}


void comStop(void) {

  stepper.setSpeed(0);
  digitalWrite(ENABLE_PIN, HIGH);
  ourState = waiting;
}


void loop() {

  idle();
  stepper.runSpeed();
  if (printTimer.ding()) {
    switch(ourState) {
      case waiting  : Serial.print("waiting   ");  break;
      case moveCW   : Serial.print("Move CW   ");  break;
      case moveCCW  : Serial.print("Move CCW  ");  break;
      case rapidCW  : Serial.print("Rapid CW  ");  break;
      case rapidCCW : Serial.print("Rapid CCW ");  break;
    }
    Serial.print("Speed : ");
    Serial.print(stepper.speed());
    Serial.print("  Pos : ");
    Serial.println(stepper.currentPosition());
    printTimer.start();
  }
}
 


Have you considered a relay for continuity (making a connection or breaking a connection)?

i got a relay for making and breaking the connection to the stepper but seeing its still under power that doesnt seem right and when the enable gets power the drive disconnects the stepper so it can nearly free spin and i could than safely disconnect the stepper.

or do you mean in a different way?

I was referring to your request to set "enable" high... usually you would connect enable to a pin, and set the pin high or low.. but it seemed to me that you wanted an electrical connection to change from high to low. Using the relay you could physically change N.O. and N.C. pins to connect to COM.

no i am looking to setting a pin high or low but it seems that low is no connection end high is connection to gnd i thought high would mean 5v

It's unclear what you have going on.

With a pin set to output, digitalWrite(pin,HIGH) will set that pin to 5v and digitalWrite(pin,LOW) will connect the pin to ground.

here is what im working with , the 2 switches is actually 1-3position switch left is ccw made and right cw when in the middle nothing is made and enable should be high .
but thats not the case it seems.
i do have a different drive but that shouldnt matter as it accepts same as 4988

maybe ill just start over but with ContinuousStepper lib seems more fitted to my purpose.

Does this mean "erratic results?" If so, have you tried a pulldown resistor?

/**************************************************************

(off topic, but if you want your stepper to start at 0 rather than -2, cross the wire pairs...

Before (-2 at start)

After (0 at start)

tbh no as in new to this programming and arduino stuff and dont really know what i program or what connections it uses/needs .
got my code sofar by looking at other examples and using that knowledge and proofing/testing it in wokwi

Your simulation runs perfectly. Can you point out the issue?

#define ENABLE_PIN  4

void setup() {

  Serial.begin(115200);
  CWBtn.setCallback(CWClick);
  CCWBtn.setCallback(CCWClick);
  RapidBtn.setCallback(RapidClk);
  speedPOT.setCallback(newSpeed);
  Serial.println("The sketch has started");
  stepper.setMaxSpeed(MAX_SPEED); 
  stepper.setSpeed(0);
  ourState = waiting;
}

You do not set the pinMode for the ENABLE_PIN to output in setup.

The issue is that the enable isnt high when both switches are in rest

  • that it is stepping when the pot was at 0 but i think that that could have been that -2 you pointed out

your right guess that explains the enable not working , didnt notice that.

Have you seen the MobaTools stepper library? It is easier to learn and use than AccelStepper, i think, but still supports acceleration if higher speeds are required.

no havnt seen that but if im gonna start over i think ill use continuousstepper lib as that has all i need asfar as i can see .

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