Motor Control with tb6600

So i need a little help if possible. I am driving myself mad with this bit of code. I am controlling a stepper motor using a tb6600 stepper driver.
stepper driver

I am struggling with homing this and setting a pulse count. The idea is that when the 'Home' button is pressed, the following actions are taken:-
first, the motor drives in a clockwise direction until a microswitch is pressed. When the microswitch is pressed, a variable called pos is set to zero and the motor changes direction and drives until a limit switch is pressed. During this second motion, pos is incremented each time a pulse is sent to the stepper drive.
the final step is to again change the direction of the motor and for every pulse, the pos value is decremented until it reaches zero. The idea being, that I can then send the motor to a specific location using the pos variable.

however I am having a problem..... The first part where the motor drives home, and pos is set to zero works.
the second part where I change the direction of the motor and increment the pos value works. but when I try to change direction again, it does not change.
I am using digitalWrite to set the direction pin, but although it works on the first two instances, the third does not work..... What am I doing wrong please?

void Home(){
  lcd.setCursor (8,2);
  lcd.print ("Homing");
  hmSwSt = digitalRead (hmSw);
  if (hmSwSt == LOW){
    digitalWrite (ena, LOW);
    while (hmSwSt == LOW){
      digitalWrite (dir, LOW);
      digitalWrite (pul, LOW);
      delayMicroseconds (250);
      digitalWrite (dir, LOW);
      digitalWrite (pul, HIGH);
      hmSwSt = digitalRead (hmSw);
      delayMicroseconds (250);
    }
    pos = 0;
  }
  lmtSwSt = digitalRead (lmtSw);
  digitalWrite (dir, HIGH);
  while (lmtSwSt == LOW){
    digitalWrite (pul, LOW);
    pos ++;
    delayMicroseconds (250);
    digitalWrite (pul, HIGH);
    pos ++;
    lmtSwSt = digitalRead (lmtSw);
    delayMicroseconds (250);
  }
  digitalWrite (dir, LOW);
  while (pos > 0){  
  lcd.setCursor (6,3);
  lcd.print (pos);  
  digitalWrite (dir, LOW);
  digitalWrite (pul, LOW);
  
  delayMicroseconds (250); 
  digitalWrite (dir, LOW);
  digitalWrite (pul, HIGH);
  lcd.setCursor (6,3);
  lcd.print (pos);
  pos --;
  delayMicroseconds (250);
}
}

Try again, only this time, include ALL the code.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x27, 20, 4);

int pos = 0;

int lcdPwr = 13;
int ena = 10;
int dir = 11;
int pul = 12;

int hme = 44;
int fwd = 46;
int rev = 48;
int set = 50;
int clr = 52;

int hmSw = 51;
int hmSwNeg = 53;
int lmtSw = 49;
int lmtSwNeg = 47;

int hmeSt;
int fwdSt;
int revSt;
int setSt;
int clrSt;
int hmSwSt;
int lmtSwSt;

void setup(){
pinMode (lcdPwr, OUTPUT);
pinMode (ena, OUTPUT);
pinMode (dir, OUTPUT);
pinMode (pul, OUTPUT);
pinMode (hme, INPUT_PULLUP);
pinMode (fwd, INPUT_PULLUP);
pinMode (rev, INPUT_PULLUP);
pinMode (set, INPUT_PULLUP);
pinMode (clr, INPUT_PULLUP);
pinMode (hmSw, INPUT_PULLUP);
pinMode (hmSwNeg, OUTPUT);
pinMode (lmtSw, INPUT_PULLUP);
pinMode (lmtSwNeg, OUTPUT);

digitalWrite (lcdPwr, HIGH);
digitalWrite (ena, HIGH);
digitalWrite (dir, LOW);
digitalWrite (pul, LOW);
digitalWrite (hmSwNeg, LOW);
digitalWrite (lmtSwNeg, LOW);


lcd.init();
lcd.backlight();
}


void loop(){
  lcd.setCursor (4,0);
  lcd.print ("Project Adept");
  lcd.setCursor (0,1);
  lcd.print ("Switch = ");
  lcd.setCursor (0,2);
  lcd.print ("State = ");
  lcd.setCursor (0,3);
  lcd.print ("Pos = ");
  hmeSt = digitalRead (hme);

  if (hmeSt == LOW){
    lcd.setCursor (9,1);
    lcd.print ("Home      ");
    Home();
  }
  
}

void Home(){
  lcd.setCursor (8,2);
  lcd.print ("Homing");
  hmSwSt = digitalRead (hmSw);
  if (hmSwSt == LOW){
    digitalWrite (ena, LOW);
    while (hmSwSt == LOW){
      digitalWrite (dir, LOW);
      digitalWrite (pul, LOW);
      delayMicroseconds (250);
      digitalWrite (dir, LOW);
      digitalWrite (pul, HIGH);
      hmSwSt = digitalRead (hmSw);
      delayMicroseconds (250);
    }
    pos = 0;
    digitalWrite (ena, HIGH);
  }
  lmtSwSt = digitalRead (lmtSw);
  digitalWrite (ena,LOW);
  digitalWrite (dir, HIGH);
  while (lmtSwSt == LOW){
    digitalWrite (pul, LOW);
    pos ++;
    delayMicroseconds (250);
    digitalWrite (pul, HIGH);
    pos ++;
    lmtSwSt = digitalRead (lmtSw);
    delayMicroseconds (250);
    digitalWrite (ena, HIGH);
  }
  digitalWrite (ena, LOW);
  digitalWrite (dir, LOW);
  while (pos > 0){  
  lcd.setCursor (6,3);
  lcd.print (pos);  
  digitalWrite (dir, LOW);
  digitalWrite (pul, LOW);
  
  delayMicroseconds (250); 
  digitalWrite (dir, LOW);
  digitalWrite (pul, HIGH);
  lcd.setCursor (6,3);
  lcd.print (pos);
  pos --;
  delayMicroseconds (250);
  digitalWrite (ena, LOW);
}
}

You don't need to constantly be setting your enable and direction pins. Just set them once and then do your steps. Here is your code, but with more descriptive variable names

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x27, 20, 4);

long int pos = 0;

const int lcdPowerPin = 13;
const int enablePin = 10;
const int dirPin = 11;
const int pulsePin = 12;

// direction pin
#define CLOCKWISE LOW
#define COUNTERCLOCKWISE HIGH

const int homeButtonPin = 44;
const int fwdButtonPin = 46;
const int revButtonPin = 48;
const int setButtonPin = 50;
const int clearButtonPin = 52;

const int homeSwitchPin = 51;
const int homeSwitchNegPin = 53;
const int limitSwitchPin = 49;
const int limitSwitchNegPin = 47;

int homeButtonState;
int fwdButtonState;
int revButtonState;
int setButtonState;
int clearButtonState;
int homeSwitchState;
int limitSwitchState;

void setup() {
  pinMode (lcdPowerPin, OUTPUT);
  pinMode (enablePin, OUTPUT);
  pinMode (dirPin, OUTPUT);
  pinMode (pulsePin, OUTPUT);
  pinMode (homeButtonPin, INPUT_PULLUP);
  pinMode (fwdButtonPin, INPUT_PULLUP);
  pinMode (revButtonPin, INPUT_PULLUP);
  pinMode (setButtonPin, INPUT_PULLUP);
  pinMode (clearButtonPin, INPUT_PULLUP);
  pinMode (homeSwitchPin, INPUT_PULLUP);
  pinMode (homeSwitchNegPin, OUTPUT);
  pinMode (limitSwitchPin, INPUT_PULLUP);
  pinMode (limitSwitchNegPin, OUTPUT);

  digitalWrite (lcdPowerPin, HIGH);
  digitalWrite (enablePin, HIGH);
  digitalWrite (dirPin, LOW);
  digitalWrite (pulsePin, LOW);
  digitalWrite (homeSwitchNegPin, LOW);
  digitalWrite (limitSwitchNegPin, LOW);

  lcd.init();
  lcd.backlight();
}


void loop() {
  lcd.setCursor (4, 0);
  lcd.print ("Project Adept");
  lcd.setCursor (0, 1);
  lcd.print ("Switch = ");
  lcd.setCursor (0, 2);
  lcd.print ("State = ");
  lcd.setCursor (0, 3);
  lcd.print ("Pos = ");
  homeButtonState = digitalRead(homeButtonPin);

  if (homeButtonState == LOW) {
    lcd.setCursor (9, 1);
    lcd.print ("Home      ");
    Home();
  }

}

void Home() {
  lcd.setCursor (8, 2);
  lcd.print ("Homing");

  // Drive the motor CLOCKWISE until home switch is detected
  digitalWrite (dirPin, CLOCKWISE);
  digitalWrite (enablePin, LOW);

  while (digitalRead (homeSwitchPin) == LOW) {
    digitalWrite (pulsePin, LOW);
    delayMicroseconds (250);
    digitalWrite (pulsePin, HIGH);
    delayMicroseconds (250);
  }

  // at HOME position so call this zero
  pos = 0;
  digitalWrite (enablePin, HIGH);

  // drive the motor in reverse until limit switch is detected
  digitalWrite (dirPin, COUNTERCLOCKWISE);
  digitalWrite (enablePin, LOW);

  while (digitalRead (limitSwitchPin) == LOW) {
    digitalWrite (pulsePin, LOW);
    delayMicroseconds (250);
    digitalWrite (pulsePin, HIGH);
    delayMicroseconds (250);
    pos ++;
  }

  // at LIMIT postiion
  digitalWrite (enablePin, HIGH);

  // Drive the motor back home
  digitalWrite (dirPin, CLOCKWISE);
  digitalWrite (enablePin, LOW);

  while (pos > 0) {
    lcd.setCursor (6, 3);
    lcd.print (pos);
    digitalWrite (pulsePin, LOW);
    delayMicroseconds (250);
    digitalWrite (pulsePin, HIGH);
    delayMicroseconds (250);
    lcd.setCursor (6, 3);
    lcd.print (pos);
    lcd.print (" ");  // need to blank out last postion when transitioning from 10000 to 9999, 1000 to 999, etc.
    pos --;
  }

  // all done
  digitalWrite (enablePin, HIGH);
}

Thank you so much for this......

I really appreciate your help.

Once again blh64, ## Faraday, I appreciate your help. The code you helped me with sorted all my problems, but I do have one last little issue if you or anyone can help. The purpose of this project is to move a leadscrew to a position. Then when the 'set button' is pressed to reverse a specified number of millimetres. I have the function to convert the Gap number to millimetres depending on the microstep setting on the stepper driver.

When I look at the serial monitor, I can see that the value called pulseCount is correct.

but when I press the set button, the leadscrew simply reverses all the way back to the home switch. I know I am doing something so stupid that I will be embarrassed when I see the solution. But any help would be gratefully received. The area of problem is in:-
void setSet()

thanking you all in advance :grinning: :pray:

[code]
// setup libraries
#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>



// Initialise LCD
LiquidCrystal_I2C lcd (0x27, 20, 4);
int lcdPowerPin = 13;
byte newChar[8] = {
  B01001,
  B01001,
  B01001,
  B01001,
  B01110,
  B01000,
  B01000,
  B10000
};


// setup Keypad
const byte numRows = 5; //number of rows on the keypad
const byte numCols = 4; //number of columns on the keypad

//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols] =
{
  {'A', 'B', '#', '*'},
  {'1', '2', '3', 'U'},
  {'4', '5', '6', 'D'},
  {'7', '8', '9', 'C'},
  {'L', '0', 'R', 'E'}
};

int csr = 0;

byte rowPins[numRows] = {26, 28, 30, 32, 34};
byte colPins[numCols] = {42, 40, 38, 36};

//initializes an instance of the Keypad class
Keypad keypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);


// setup Motor control parameters
String gap = "";
int gapNum = 0;
int gapFlag = 0;
int mm = 0;
int gapSteps = 0;
String steps = "";
int stepNum = 0;
int stepFlag = 0;
int pos = 0;
int posMax = 0;
int pulseCount;


// Set up Stepper Control Pins
const int enablePin = 10;
const int dirPin = 11;
const int pulsePin = 12;

#define CLOCKWISE LOW
#define COUNTERCLOCKWISE HIGH


// setup switches
const int homeButtonPin = 52;
const int fwdButtonPin = 50;
const int revButtonPin = 48;
const int setButtonPin = 46;
const int clearButtonPin = 44;

const int homeSwitchPin = 33;
const int homeSwitchNegPin = 31;
const int limitSwitchPin = 29;
const int limitSwitchNegPin = 27;


// setup LEDs
const int homeLed = 53;
const int homeLedGnd = 51;
const int forwardLed = 49;
const int forwardLedGnd = 47;
const int reverseLed = 41;
const int reverseLedGnd = 45;
const int setLed = 37;
const int setLedGnd = 43;
const int clearLed = 39;
const int clearLedGnd = 35;


// Set up Switch Registry
int homeButtonState;
int fwdButtonState;
int revButtonState;
int setButtonState;
int clearButtonState;
int homeSwitchState;
int limitSwitchState;
int homeFlag = 0;
int dly = 0;
int flagTotal = 0;
int leadScrewConstant = 50;

int dely (400);

void setup() {


  // setup pin functions
  pinMode (lcdPowerPin, OUTPUT);
  pinMode (enablePin, OUTPUT);
  pinMode (dirPin, OUTPUT);
  pinMode (pulsePin, OUTPUT);
  pinMode (homeButtonPin, INPUT_PULLUP);
  pinMode (fwdButtonPin, INPUT_PULLUP);
  pinMode (revButtonPin, INPUT_PULLUP);
  pinMode (setButtonPin, INPUT_PULLUP);
  pinMode (clearButtonPin, INPUT_PULLUP);
  pinMode (homeSwitchPin, INPUT_PULLUP);
  pinMode (homeSwitchNegPin, OUTPUT);
  pinMode (limitSwitchPin, INPUT_PULLUP);
  pinMode (limitSwitchNegPin, OUTPUT);

  pinMode (homeLed, OUTPUT);
  pinMode (homeLedGnd, OUTPUT);
  pinMode (forwardLed, OUTPUT);
  pinMode (forwardLedGnd, OUTPUT);
  pinMode (reverseLed, OUTPUT);
  pinMode (reverseLedGnd, OUTPUT);
  pinMode (setLed, OUTPUT);
  pinMode (setLedGnd, OUTPUT);
  pinMode (clearLed, OUTPUT);
  pinMode (clearLedGnd, OUTPUT);


  // setup common grounds

  digitalWrite (dirPin, LOW);
  digitalWrite (pulsePin, LOW);
  digitalWrite (homeSwitchNegPin, LOW);
  digitalWrite (limitSwitchNegPin, LOW);

  // setup stepper initial conditions
  digitalWrite (enablePin, HIGH);
  digitalWrite (dirPin, LOW);
  digitalWrite (pulsePin, LOW);

  // setup Serial Monitor
  Serial.begin (115200);


  //setup LCD Display
  digitalWrite (lcdPowerPin, HIGH);
  lcd.init();
  lcd.backlight();
  lcd.createChar(0, newChar);

  gapFlag = 0;
  stepFlag = 0;


}

void loop() {

  lcd.setCursor (4, 0);
  lcd.print ("Project Adept");
  lcd.setCursor (0, 1);
  lcd.print ("Gap= ");
  lcd.setCursor (9, 1);
  lcd.write (0);
  lcd.print ("Steps=");
  lcd.setCursor (0, 2);
  lcd.print ("State = ");
  lcd.setCursor (0, 3);
  lcd.print ("Pos = ");
  lcd.print (pos);
  lcd.print (" / ");
  lcd.print (posMax);



  flagTotal = gapFlag + stepFlag + homeFlag;

  Serial.print ("Home Button = ");
  Serial.print (homeButtonState);
  Serial.print (" Forward Button = ");
  Serial.print (fwdButtonState);
  Serial.print (" Gap (mm) = ");
  Serial.print (gapNum);
  Serial.print (" Microsteps = ");
  Serial.print (stepNum);
  Serial.print (" mm = ");
  Serial.print (mm);
  Serial.print (" Set Pulse Count = ");
  Serial.print (pulseCount);
  Serial.print (" Flag Total = ");
  Serial.println (flagTotal);

  //Sets the number of pulses to reverse when set button pressed

  mm = leadScrewConstant * stepNum;;
  pulseCount = (gapNum * mm);

  homeButtonState = digitalRead(homeButtonPin);
  fwdButtonState = digitalRead(fwdButtonPin);

  //display values
  if (gapFlag == 0) {
    lcd.setCursor (5, 1);
    lcd.print ("F1 ");
  }
  if (gapFlag == 1) {
    lcd.setCursor (5, 1);
    lcd.print (gapNum);
  }


  if (stepFlag == 0) {
    lcd.setCursor (17, 1);
    lcd.print ("F2 ");
  }
  if (stepFlag == 1) {
    lcd.setCursor (17, 1);
    lcd.print (stepNum);
  }

  if (homeFlag != 1) {
    lcd.setCursor (8, 2);
    lcd.print ("Set Home  ");
  }

  if (homeFlag == 1) {
    lcd.setCursor (8, 2);
    lcd.print ("Ready     ");
  }

  if ((homeButtonState == LOW) && (homeFlag == 0)) {
    digitalWrite (homeLed, HIGH);
    Home();
    digitalWrite (homeLed, LOW);
  }
  if ((homeButtonState == LOW) && (homeFlag == 1)) {
    lcd.setCursor (8, 2);
    lcd.print("Home Set  ");
    delay (1500);
  }


  // Set Forward Motion

  fwdButtonState = digitalRead (fwdButtonPin);
  if ((fwdButtonState == LOW) && (flagTotal != 3)) {
    lcd.setCursor (8, 2);
    lcd.print ("Set Values  ");
    delay (1500);
  }

  if ((fwdButtonState == LOW) && (flagTotal == 3)) {
    digitalWrite (forwardLed, HIGH);
    forward();
    digitalWrite (forwardLed, LOW);
  }

  if ((fwdButtonState == LOW) && (limitSwitchState == HIGH)) {
    lcd.setCursor (8, 2);
    lcd.print ("FWD LIMIT    ");
  }

  //Set Reverse Motion

  revButtonState = digitalRead (revButtonPin);
  if ((revButtonState == LOW) && (flagTotal != 3)) {
    lcd.setCursor (8, 2);
    lcd.print ("Set Values  ");
    delay (1500);
  }

  if ((revButtonState == LOW) && (flagTotal == 3)) {
    digitalWrite (reverseLed, HIGH);
    reverse();
    digitalWrite (reverseLed, LOW);
  }

  if ((revButtonState == LOW) && (homeSwitchState == HIGH)) {
    lcd.setCursor (8, 2);
    lcd.print ("REV LIMIT    ");
  }

  clearButtonState = digitalRead (clearButtonPin);
  if (clearButtonState == LOW) {
    digitalWrite (clearLed, HIGH);
    gap = "";
    gapNum = 0;
    gapFlag = 0;
    steps = "";
    stepNum = 0;
    stepFlag = 0;
    homeFlag = 0;
    delay (1500);
    digitalWrite (clearLed, LOW);
  }

  //Set Clearance
  setButtonState = digitalRead (setButtonPin);
  if ((setButtonState == LOW) && (flagTotal == 3)) {
    digitalWrite (setLed, HIGH);
    setSet();
    delay (500);
    digitalWrite (setLed, LOW);
  }





  //read keypad
  char keypressed = keypad.getKey();
  if (keypressed != NO_KEY) {                           //curly bracket 1
    if ((keypressed != 'A') && (keypressed != 'B')) {
      lcd.clear();
      lcd.setCursor (3, 1);
      lcd.print ("Invalid Input");
      delay (2000);
      lcd.clear();
      loop();
    }
    switch (keypressed) {
        break;

      case 'A': //When pressing F1
        gapSet();
        break;

      case 'B': //when pressing F2
        stepSet();
        break;






    }
  }
}
[/code]

void forward(){
  digitalWrite (enablePin, LOW);
  digitalWrite (dirPin, COUNTERCLOCKWISE);
  limitSwitchState = digitalRead (limitSwitchPin);
  fwdButtonState = digitalRead (fwdButtonPin);
  
   while (( limitSwitchState == LOW) && (fwdButtonState == LOW)) {
    limitSwitchState = digitalRead (limitSwitchPin);
    fwdButtonState = digitalRead (fwdButtonPin);       
    digitalWrite (pulsePin, LOW);
    delayMicroseconds (dely);
    digitalWrite (pulsePin, HIGH);
    delayMicroseconds (dely);
    pos ++;
  }
  lcd.setCursor (0,3);
  lcd.print ("                   ");
}
[code]
void gapSet() {
  gapFlag = 0;
  gapNum = 0;
  gap = "";
  csr = 0;
  lcd.clear();
  lcd.setCursor (1, 0);
  lcd.print ("Set Gap Distance");
  lcd.setCursor (1, 1);
  lcd.print ("mm");
  lcd.setCursor (9, 1);
  lcd.print ("(100mm Max)");

  do {
    char gapKey = keypad.getKey();

    if (gapKey) {
      lcd.setCursor ((csr) + 1, 3); //move cursor on LCD
      lcd.print (gapKey);           //display character pressed on keypad

      if  (gapKey >= '0' && gapKey <= '9') {     // only act on numeric keys
        gap += gapKey;               // append new character to input string
        csr++; //increase cursor count to move on lcd

      } else if (gapKey == 'E') {
        if ((gap.length() > 0) && (gap.length() <= 3)) {
          gapNum = gap.toInt(); // YOU GOT AN INTEGER NUMBER
          if (gapNum < 1) {
            lcd.clear();
            lcd.setCursor (3, 1);
            lcd.print ("Invalid Input");
            delay (2000);
            lcd.clear();
            gap = "";
            gapNum = 0;
            gapFlag = 0;
            break;
          }
          if (gapNum > 100) {
            lcd.clear();
            lcd.setCursor (3, 1);
            lcd.print ("Invalid Input");
            delay (2000);
            lcd.clear();
            gap = "";
            gapNum = 0;
            gapFlag = 0;

            break;
          }
          gapFlag = 1;            // DO YOUR WORK HERE
          gap = "";               // clear input
        }
      } else if (gapKey == 'C') {
        gapFlag = 0;
        gapNum = 0;
        gap = "";              // clear input
        lcd.clear();
        break;
      }
    }
  } while (gapFlag != 1);
  lcd.clear();
   
}
[/code]

void Home() {
  lcd.setCursor (8, 2);
  lcd.print ("Homing   ");

  // Drive the motor CLOCKWISE until home switch is detected
  digitalWrite (dirPin, CLOCKWISE);
  digitalWrite (enablePin, LOW);

  while (digitalRead (homeSwitchPin) == LOW) {
    digitalWrite (pulsePin, LOW);
    delayMicroseconds (dely);
    digitalWrite (pulsePin, HIGH);
    delayMicroseconds (dely);
  }

  // at HOME position so call this zero
  pos = 0;
  digitalWrite (enablePin, HIGH);

  // drive the motor in reverse until limit switch is detected
  digitalWrite (dirPin, COUNTERCLOCKWISE);
  digitalWrite (enablePin, LOW);

  while (digitalRead (limitSwitchPin) == LOW) {
    digitalWrite (pulsePin, LOW);
    delayMicroseconds (dely);
    digitalWrite (pulsePin, HIGH);
    delayMicroseconds (dely);
    pos ++;
  }

  // at LIMIT postiion
  digitalWrite (enablePin, HIGH);
  posMax = (pos);

  // Drive the motor back home
  digitalWrite (dirPin, CLOCKWISE);
  digitalWrite (enablePin, LOW);

  while (pos > 0) {
    
    digitalWrite (pulsePin, LOW);
    delayMicroseconds (dely);
    digitalWrite (pulsePin, HIGH);
    delayMicroseconds (dely);
   
    pos --;
  } 
  lcd.setCursor (6, 3);
  lcd.print (pos);
  lcd.print (" ");  // need to blank out last postion when transitioning from 10000 to 9999, 1000 to 999, etc.

  // all done
  homeFlag = 1;
  digitalWrite (enablePin, HIGH);

 
}
[code]
void reverse(){
  digitalWrite (enablePin, LOW);
  digitalWrite (dirPin, CLOCKWISE);
  homeSwitchState = digitalRead (homeSwitchPin);
  revButtonState = digitalRead (revButtonPin);
  
   while (( homeSwitchState == LOW) && (revButtonState == LOW)) {
    homeSwitchState = digitalRead (homeSwitchPin);
    revButtonState = digitalRead (revButtonPin);       
    digitalWrite (pulsePin, LOW);
    delayMicroseconds (dely);
    digitalWrite (pulsePin, HIGH);
    delayMicroseconds (dely);
    pos --;
  }
  lcd.setCursor (0,3);
  lcd.print ("                   ");
}

void setSet() {
  
  // causes the lead screw to retract a preset number of pulses
  
  digitalWrite (enablePin, LOW);
  digitalWrite (dirPin, CLOCKWISE);
  for (int i = 0; i < pulseCount; i++) { // uses for loop to create a set number of pulses
    while (homeSwitchState == LOW) {
      homeSwitchState = digitalRead (homeSwitchPin); //prevents running into end stock
      digitalWrite (pulsePin, HIGH);
      delayMicroseconds (dely);
      digitalWrite (pulsePin, LOW);
      delayMicroseconds (dely);
      pos = pos - 1;

    }
  }
}
[code]
void stepSet() {
  stepFlag = 0;
  stepNum = 0;
  steps = "";
  csr = 0;
  lcd.clear();
  lcd.setCursor (1, 0);
  lcd.print ("MicroSteps");
  lcd.setCursor (1, 1);
  lcd.print ("");
  lcd.setCursor (9, 1);
  lcd.print ("(32 Max)");

  do {
    char stepKey = keypad.getKey();

    if (stepKey) {
      lcd.setCursor ((csr) + 1, 3); //move cursor on LCD
      lcd.print (stepKey);           //display character pressed on keypad

      if  (stepKey >= '0' && stepKey <= '9') {     // only act on numeric keys
        steps += stepKey;               // append new character to input string
        csr++; //increase cursor count to move on lcd

      } else if (stepKey == 'E') {
        if ((steps.length() > 0) && (steps.length() <= 2)) {
          stepNum = steps.toInt(); // YOU GOT AN INTEGER NUMBER
          if (stepNum < 1) {
            lcd.clear();
            lcd.setCursor (3, 1);
            lcd.print ("Invalid Input");
            delay (2000);
            lcd.clear();
            steps = "";
            stepNum = 0;
            stepFlag = 0;
            break;
          }
          if (stepNum > 32) {
            lcd.clear();
            lcd.setCursor (3, 1);
            lcd.print ("Invalid Input");
            delay (2000);
            lcd.clear();
            steps = "";
            stepNum = 0;
            stepFlag = 0;

            break;
          }
          stepFlag = 1;            // DO YOUR WORK HERE
          steps = "";               // clear input
        }
      } else if (stepKey == 'C') {
        stepFlag = 0;
        stepNum = 0;
        steps = "";              // clear input
        lcd.clear();
        break;
      }
    }
  } while (stepFlag != 1);
  lcd.clear();

}
[/code]

Having your code in multiple code blocks makes it much more difficult to copy & paste... 1 block is better

A vert odd way of initializing a variable, but valid

const int dely = 400;

Yikes! Don't ever call loop(). You are creating a recursive loop that will eventually crash the MCU. If you want to be done, simply call return which will exit this iteration of loop() and then start again.

Inside your setSet() function, you have a while() loop inside your for() loop. This makes the while loop run all the way back to HOME during the first iteration of your for() loop. You just want to check the home limit switch and stop each time through for() without the while() loop

void setSet() {
  // causes the lead screw to retract a preset number of pulses
  digitalWrite (enablePin, LOW);
  digitalWrite (dirPin, CLOCKWISE);
  for (int i = 0; i < pulseCount; i++) { // uses for loop to create a set number of pulses
    homeSwitchState = digitalRead (homeSwitchPin); //prevents running into end stock
    if (homeSwitchState == HIGH) {
      // we reached home so stop
      break;
    }
    digitalWrite (pulsePin, HIGH);
    delayMicroseconds (dely);
    digitalWrite (pulsePin, LOW);
    delayMicroseconds (dely);
    pos--;
  }
  digitalWrite (enablePin, HIGH);
}

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