Powerfeed help needed

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?
or would it be better to use ContinuousStepper lib?

#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();
  }
}
 


The safest way is that the switch being LOW indicates "Clear to run". Use INPUT_PULLUP in the controller. In case of a broken wire, lost contact, it will be handled as "Stop".
In the code reading a LOW is clear to go. if clear, go, else Stop.

Read the pot and use an interval. If above 100, use the value and if below 900, use the value. If outside that, Stop, illegal value etc.

oke will have to investigate that interval pot stuff tnx for the info

Suppose a 10k pot. Putting a 1k resistor between both the high and the low side of the pot gives a range in analog.read of some 100 to 950 ca. If a failure occurs, high or low side connection lost the reading will be 0 or 1023, both outside range, and a reason for emergency stop.
Of course a calibration reading the allowed max and min is needed.
This is used in industrial machines world wide.

2 Likes

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