Pulse steppers while looking for endstop switches

Hello All,
Well I’ve been working on this for quite a while now and I am SO close, with much help from you all.

/*
  Uses DM542T or similar Stepper Driver Unit
  Nextion Intelligent 7.0 inch NX8048P070-011C Capacitive Touch Screen 
  Muggs - 20220314
*/

#include <EasyNextionLibrary.h>

EasyNex Nex(Serial1);

// Define Pins & Conatants ******************************
const int W1_Home = 2;     // Set W1_Home limit switch to pin 2
const int W2_Home = 3;     // Set W2_Home limit switch to pin 3
const int H1_Home = 4;     // Set H1_Home limit switch to pin 4
const int H2_Home = 5;     // Set H2_Home limit switch to pin 5
const int WidthDIR = 10;   // Width DIRECTION- pin 10
const int WidthPUL = 11;   // Width PULSE- pin 11
const int HeightDIR = 12;  // Height DIRECTION- pin 12
const int HeightPUL = 13;  // Height PULSE- pin 13
const int pd = 200;        // Pulse delay in Microseconds

// Define Variables ******************************
int i;                           // Integer for Counter
int SPI = 1018;                  // StepsPerInch was caculated to be 4072 @ 1600 micro stepping
int Plus_dir = HIGH;             // Plus Direction (away from Moter) is set to HIGH
int Minus_dir = LOW;             // Minus Direction (Toward from Moter) is set to LOW
int W1_Disable = 6;              // Set H-Motor Disable to pin 6
int W2_Disable = 7;              // Set H-Motor Disable to pin 7
int H1_Disable = 8;              // Set H-Home Disable to pin 8
int H2_Disable = 9;              // Set H-Motor Disable to pin 9
String strWfromNex;              // Width Distance as String from Nextion
String strHfromNex;              // Height Distance as String from Nextion
String bufferWidth;              // Buffer to remove "." in Width Field
String bufferHeight;             // Buffer to remove "." in Height Field
String Ver = "JFD V0.2.8";       // Version #
float Wsteps;                    // Wsteps eqal the Width distance from Display * SPI
float Hsteps;                    // Hsteps eqal the Height distance from Display * SPI
boolean setdirW = LOW;           // Set Width Direction
boolean setdirH = LOW;           // Set Height Direction
boolean hasRun = false;          // Set hasRun for Homming to False
unsigned long previousTime = 0;  //

/*  Trigger designations ******************************
    trigger0  = TEMP *** Reverse Direction
    trigger1  = 1.000" - TEMP W move test setup
    trigger2  = 0.500" - TEMP W move test setup
    trigger3  = 0.250" - TEMP W move test setup
    trigger4  = 2.000" - TEMP W move test setup
    trigger5  = ENTER
    trigger6  = 1.000" - TEMP H move test setup
    trigger7  = 0.500" - TEMP H move test setup
    trigger13 = Initial HOME
*/

void setup() {

  Nex.begin(9600);
  Serial.begin(9600);

  pinMode(WidthPUL, OUTPUT);
  pinMode(WidthDIR, OUTPUT);
  pinMode(HeightPUL, OUTPUT);
  pinMode(HeightDIR, OUTPUT);
  pinMode(W1_Home, INPUT_PULLUP);
  pinMode(W2_Home, INPUT_PULLUP);
  pinMode(W1_Disable, OUTPUT);
  pinMode(W2_Disable, OUTPUT);
  pinMode(H1_Home, INPUT_PULLUP);
  pinMode(H2_Home, INPUT_PULLUP);
  pinMode(H1_Disable, OUTPUT);
  pinMode(H2_Disable, OUTPUT);
  digitalWrite(W1_Disable, HIGH);
  digitalWrite(W2_Disable, HIGH);
}
void loop() {

  Nex.NextionListen();
  digitalWrite(WidthDIR, setdirW);
  digitalWrite(HeightDIR, setdirH);
}
String parseData(String data, char delimiter) {

  int8_t index = 0, indexPast = 0;
  String resultData;
  index = data.indexOf(delimiter);
  data.remove(index, 1);
  resultData = data;
  return resultData;
}
void trigger0() {  // TEMP Trigger to set Direction

  setdirW = !setdirW;
  setdirH = !setdirH;
}
void trigger1() {  // Trigger for 1" Width Button

  for (i = 1; i <= 01.000 * SPI; i++) {
    digitalWrite(WidthPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(WidthPUL, LOW);
    delayMicroseconds(pd);
  }
}
void trigger2() {  // Trigger for .5" Width Button

  for (i = 1; i <= 00.500 * SPI; i++) {
    digitalWrite(WidthPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(WidthPUL, LOW);
    delayMicroseconds(pd);
  }
}
void trigger3() {  // Trigger for .25" Width Button

  for (i = 1; i <= 00.250 * SPI; i++) {
    digitalWrite(WidthPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(WidthPUL, LOW);
    delayMicroseconds(pd);
  }
}
void trigger4() {  // Trigger for 2" Width Button

  for (i = 1; i <= 02.000 * SPI; i++) {
    digitalWrite(WidthPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(WidthPUL, LOW);
    delayMicroseconds(pd);
  }
}
void trigger5() {  // Trigger for ENTER Button
  strWfromNex = Nex.readStr("t1.txt");
  strHfromNex = Nex.readStr("t2.txt");
  bufferWidth = parseData(strWfromNex, '.');
  bufferHeight = parseData(strHfromNex, '.');
  Wsteps = bufferWidth.toFloat() / 1000 * SPI;
  Hsteps = bufferHeight.toFloat() / 1000 * SPI;

  for (i = 1; i <= Wsteps; i++) {
    digitalWrite(WidthPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(WidthPUL, LOW);
    delayMicroseconds(pd);
  }
  for (i = 1; i <= Hsteps; i++) {
    digitalWrite(HeightPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(HeightPUL, LOW);
    delayMicroseconds(pd);
  }
}
void trigger6() {  // Trigger for 1" Height Button

  for (i = 1; i <= 01.000 * SPI; i++) {
    digitalWrite(HeightPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(HeightPUL, LOW);
    delayMicroseconds(pd);
  }
}
void trigger7() {  // Trigger for TEMP HOME Button

  Nex.writeStr("t0.txt", Ver);
  hasRun = false;
  Home();
}
void trigger13() {  // Trigger for HOME Button on Home/Warning Screen

  Nex.writeStr("t0.txt", Ver);
  hasRun = false;
  Home();
}
void Home() {

  // Move until you hit the switch

  while ((digitalRead(W1_Home) != LOW) && (digitalRead(W2_Home) != LOW)) {
    CheckPins();
    StepTowards_W_Switch();
  }



  hasRun = true;
  setdirW = Plus_dir;
  // setdirH = Plus_dir;
}

// void StepTowards_H_Switch() {

//   digitalWrite(HeightDIR, Minus_dir);
//   digitalWrite(HeightPUL, HIGH);
//   delayMicroseconds(pd);
//   digitalWrite(HeightPUL, LOW);
//   delayMicroseconds(pd);
// }

void StepTowards_W_Switch() {

  digitalWrite(WidthDIR, Minus_dir);
  digitalWrite(WidthPUL, HIGH);
  delayMicroseconds(pd);
  digitalWrite(WidthPUL, LOW);
  delayMicroseconds(pd);
}

// void StepAwayFrom_H_Switch() {

//   digitalWrite(HeightDIR, Plus_dir);
//   digitalWrite(HeightPUL, HIGH);
//   delayMicroseconds(pd);
//   digitalWrite(HeightPUL, LOW);
//   delayMicroseconds(pd);
// }

// void StepAwayFrom_W_Switch() {

//   digitalWrite(WidthDIR, Plus_dir);
//   digitalWrite(WidthPUL, HIGH);
//   delayMicroseconds(pd);
//   digitalWrite(WidthPUL, LOW);
//   delayMicroseconds(pd);
// }

void CheckPins() {

  if (digitalRead(W1_Home) == LOW) {
    digitalWrite(W1_Disable, LOW);
  } else {
    digitalWrite(W1_Disable, HIGH);
  }

  if (digitalRead(W2_Home) == LOW) {
    digitalWrite(W2_Disable, LOW);
  } else {
    digitalWrite(W2_Disable, HIGH);
  }
}

I am driving two NEMA 23 steppers and DM542T controllers from ONE pulse pin on the Arduino Nano Every. I’ve worked out (again with a lot of help form you) how to disable either one of the steppers using

void CheckPins() {

  if (digitalRead(W1_Home) == LOW) {
    digitalWrite(W1_Disable, LOW);
  } else {
    digitalWrite(W1_Disable, HIGH);
  }

  if (digitalRead(W2_Home) == LOW) {
    digitalWrite(W2_Disable, LOW);
  } else {
    digitalWrite(W2_Disable, HIGH);
  }
}

If I put the CheckPins() in the void loop and close the W1_Home (endstop) switch with my finger the stepper is disabled, and consequently, if I release the W1_Home switch the stepper is enabled again. Great!
The problem (my limitations) is that when hitting the Home Button, both steppers move to the first (W1 or W2) home switch then both steppers stop.
I’m driving the steppers home using a while loop and checking to see if W1_Home AND W2_Home are not LOW using:

 while ((digitalRead(W1_Home) != LOW) && (digitalRead(W2_Home) != LOW)) {
    CheckPins();
    StepTowards_W_Switch();
  }

However, both steppers stop when either of the two endstops switches are reached, and neither stepper gets disabled.
Any help would again be greatly appreciated!
Muggs

ALWAYS check the switch BEFORE stepping the motor. If the switch is already pushed when you power up the Arduino, and move before checking, you will crash into the switch.

while ((digitalRead(W1_Home) != LOW) && (digitalRead(W2_Home) != LOW)) {

You should be using the logical "or" || not the logical "and" && to run the block until both are reading low.

while ((digitalRead(W1_Home) != LOW)  or (digitalRead(W2_Home) != LOW)) {
    CheckPins();
    StepTowards_W_Switch();
  }

https://www.cplusplus.com/doc/tutorial/operators/

@kolaha
Thank you. However I get when trying to compile:

@Paul_KD7HB
Thanks Paul, yes I plan on stepping off of the endstops as soon as both steppers are "homed".

yes

if (digitalRead(W1_Home) == LOW) || (digitalRead(W2_Home) == LOW)isOK = false;

@cattledog You sir, are a genius! Thank you! A virtual cold one on me.

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