End switch

Hi,

I have a setup with an arduino mega and its own keypad with multiple keys, a 20x4 lcd + i2C, a nema 17 stepper motor with 2 limit switches. And an easy driver. The first limit switch responds when you start up the Arduino. The motor will move to its "0" position. And move back a little so the switch is no longer activated, and sets its "0" position here.

What I want the end switch to do is stop the motor when too much has been entered on the keypad, forcing the stepper motor to stop. But that it stays in that place. And when you press the '' Home '' button it goes back to the "0" position.

Everything works except the end switch. What happens now is that when the end switch is activated, the stepper motor keeps running until it is done and only then responds to the switch.

My code is in the attached file. I have marked the end switch piece of code with "///////"

BAva_werkend.ino (8.74 KB)

#include "AccelStepper.h" // AccelStepper Library
#include <Keypad.h>  // Keypad Library
#include <LiquidCrystal_I2C.h>

#define home_switch 22 // Pin 22 connected to Home Switch (MicroSwitch)
#define end_switch 23 // Switch einde van tigerstop baan

int direction;    // Variable to set Rotation (CW-CCW) of the motor
int steps;        // Used to set HOME position after Homing is completed

// Variables to hold entered number on Keypad
volatile int firstnumber = 99; // used to tell how many numbers were entered on keypad
volatile int secondnumber = 99;
volatile int thirdnumber = 99;
volatile int fourthnumber = 99;

// Variables to hold Distance and CurrentPosition
int keyfullnumber = 0; // used to store the final calculated distance value
String currentposition = "";  // Used for display on Nokia LCD


const byte ROWS = 6; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char keys[ROWS][COLS] = {
  {'P', 'I', 'O'},
  {'7', '8', '9', '-'},
  {'4', '5', '6', '+'},
  {'1', '2', '3', '*'},
  {'0', '.', '=', '/'},
  {'S', 'E', 'C'}
};
byte rowPins[ROWS] = {3, 2, 9, 8, 10, 11}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  // Keypad Library definition

//SETUP THE LCD
LiquidCrystal_I2C lcd(0x3F, 20, 4);

// AccelStepper Setup
AccelStepper stepper(1, A0, A1);  // 1 = Easy Driver interface
// Arduino A0 connected to STEP pin of Easy Driver
// Arduino A1 connected to DIR pin of Easy Driver



void setup(void) {

  pinMode(home_switch, INPUT_PULLUP);
  pinMode(end_switch, INPUT_PULLUP);
  
  //  AccelStepper speed and acceleration setup
  stepper.setMaxSpeed(1500);  // Not to fast or you will have missed steps
  stepper.setAcceleration(400);  //  Same here
  

  //lcd text setup
  lcd.init();                      // initialize the lcd
  lcd.backlight();
  lcd.print(" ENTER LENGTH IN mm");
  lcd.setCursor(0, 1);
  lcd.print("===>       mm");
  lcd.setCursor(0, 2);
  lcd.print("===>       X"); // X= amount
  lcd.setCursor(0, 3);
  lcd.print("STOP POSITION= 0");
  lcd.setCursor(5, 1);

  // HOMING OF STEPPER MOTOR
  while (digitalRead(home_switch)) {  // Do this until the switch is activated
    digitalWrite(A1, LOW);
    digitalWrite(A0, HIGH);
    delay(1);                       // More delay to slow even more while moving away from switch
    digitalWrite(A0, LOW);
    delay(1);
  }

  while (!digitalRead(home_switch)) { // Do this until the switch is not activated

    digitalWrite(A1, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(A0, HIGH);
    delay(10);                       // Delay to slow down speed of Stepper
    digitalWrite(A0, LOW);
    delay(10);
  }

  steps = 0; // Reset position variable to zero
  stepper.setCurrentPosition(0);

}


void loop() {
///
 while (digitalRead(end_switch)) {
///
 
  char keypressed = keypad.getKey();  // Get value of keypad button if pressed
  if (keypressed != NO_KEY) { // If keypad button pressed check which key it was
    switch (keypressed) {

      case '1':
        checknumber(1);
        break;

      case '2':
        checknumber(2);
        break;

      case '3':
        checknumber(3);
        break;

      case '4':
        checknumber(4);
        break;

      case '5':
        checknumber(5);
        break;

      case '6':
        checknumber(6);
        break;

      case '7':
        checknumber(7);
        break;

      case '8':
        checknumber(8);
        break;

      case '9':
        checknumber(9);
        break;

      case '0':
        checknumber(0);
        break;

      case '*':
        deletenumber();
        break;

      case 'S': // Startknop tigerstop keypad
        calculatedistance();
        break;

      /*   case 'I':

           break;

         /*  case 'A':

           break;

           case 'B':

           break;*/

      case 'C': /// Clear knop tigerstop keypad
        resetnumbers();
        break;

      case 'E': // space-Calib knop tigerstop keypad
        homing();
        break;
    }
  }
////
while (!digitalRead(end_switch)) {
    digitalWrite(A1, LOW);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(A0, HIGH);
    delay(10);                       // Delay to slow down speed of Stepper
    digitalWrite(A0, LOW);
    delay(10);
  }
////
}
}

void homing() {
  while (digitalRead(home_switch)) {  // Do this until the switch is activated
    digitalWrite(A1, LOW);
    digitalWrite(A0, HIGH);
    delay(1);                       // More delay to slow even more while moving away from switch
    digitalWrite(A0, LOW);
    delay(1);
  }

  while (!digitalRead(home_switch)) { // Do this until the switch is not activated

    digitalWrite(A1, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(A0, HIGH);
    delay(10);                       // Delay to slow down speed of Stepper
    digitalWrite(A0, LOW);
    delay(10);
  }

  steps = 0; // Reset position variable to zero
  stepper.setCurrentPosition(0);
  lcd.setCursor(15, 3);
  lcd.print("     ");
  lcd.setCursor(15, 3);
  lcd.print("0");
}

void checknumber(int x) {

  if (firstnumber == 99) { // Check if this is the first number entered
    firstnumber = x;
    String displayvalue = String(firstnumber);  //  Transform int to a string for display
    drawLCD(displayvalue); // Redraw Nokia lcd

  }

  else {
    if (secondnumber == 99) {  // Check if it's the second number entered
      secondnumber = x;
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawLCD(displayvalue);

    }

    else {
      if (thirdnumber == 99) { // It must be the 3rd number entered
        thirdnumber = x;
        String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
        drawLCD(displayvalue);
      }

      else {
        fourthnumber = x;
        String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
        drawLCD(displayvalue);
      }
    }
  }

}

void deletenumber() {  // Used to backspace entered numbers
  if (fourthnumber != 99) {
    String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
    drawLCD(displayvalue);
    lcd.setCursor(8, 1);
    lcd.print(" ");
    fourthnumber = 99;
  }
  else {
    if (thirdnumber != 99) {
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawLCD(displayvalue);
      lcd.setCursor(7, 1);
      lcd.print(" ");
      thirdnumber = 99;
    }

    else {
      if (secondnumber != 99) {
        String displayvalue = String(firstnumber);
        drawLCD(displayvalue);
        lcd.setCursor(6, 1);
        lcd.print(" ");
        secondnumber = 99;
      }

      else {
        if (firstnumber != 99) {
          String displayvalue = "";
          drawLCD(displayvalue);
          lcd.setCursor(5, 1);
          lcd.print(" ");
          firstnumber = 99;
        }
      }
    }
  }
}

void calculatedistance() {  // Used to create a full number from entered numbers

  if (fourthnumber == 99 && thirdnumber == 99 && secondnumber == 99 && firstnumber != 99) {
    keyfullnumber = firstnumber;
    movestepper(keyfullnumber);
  }

  if (secondnumber != 99 && thirdnumber == 99 && fourthnumber == 99) {
    keyfullnumber = (firstnumber * 10) + secondnumber;
    movestepper(keyfullnumber);
  }

  if (thirdnumber != 99 && fourthnumber == 99) {
    keyfullnumber = (firstnumber * 100) + (secondnumber * 10) + thirdnumber;
    movestepper(keyfullnumber);
  }

  if (fourthnumber != 99) {
    keyfullnumber = (firstnumber * 1000) + (secondnumber * 100) + (thirdnumber * 10) + fourthnumber;
    movestepper(keyfullnumber);
  }



  resetnumbers(); // Reset numbers to get ready for new entry
  stepper.setCurrentPosition(0);
  lcd.setCursor(15, 3);
  lcd.print(currentposition);
  lcd.print("     ");
}


void movestepper(int z) {  //  Move the stepper

  int calculatedmove = ((z * 1600) / 80); //  Calculate number of steps needed in mm
  stepper.runToNewPosition(calculatedmove);
  currentposition = String(z);
}

void resetnumbers() {  // Reset numbers for next entry
  firstnumber = 99;
  secondnumber = 99;
  thirdnumber = 99;
  fourthnumber = 99;
  lcd.setCursor(5, 1);
  lcd.print("     ");
}


void drawLCD(String y) {
  lcd.setCursor(5, 1);
  lcd.print(y);
}

The function runToNewPosition() blocks the Arduino until it completes.

You should use run() or runSpeed() but be aware that those functions must be called very frequently - perhaps 3 or 4 time for every step.

The AccelStepper library has examples.

...R

Thanks for replying,
But what do you mean with:

"You should use run() or runSpeed() but be aware that those functions must be called very frequently - perhaps 3 or 4 time for every step"

I have tried a couple of things but the stepper motor does not move at all.

[code]
void movestepper(int z) {  //  Move the stepper

  int calculatedmove = ((z * 1600) / 80); //  Calculate number of steps needed in mm
  stepper.moveTo(calculatedmove);
  stepper.run();
  currentposition = String(z);
}

[/code]

302369432:
I have tried a couple of things but the stepper motor does not move at all.

You need to post a complete program.

Have you studied the examples from the AccelStepper library?

Almost certainly stepper.run() should be in loop() rather than in your movestepper() function. A better name for the moveStepper() function might be setStepperDestination()

...R

[code]
#include "AccelStepper.h" // AccelStepper Library
#include <Keypad.h>  // Keypad Library
#include <LiquidCrystal_I2C.h>

#define home_switch 22 // Pin 22 connected to Home Switch (MicroSwitch)
#define end_switch 23 // Switch einde van tigerstop baan

int direction;    // Variable to set Rotation (CW-CCW) of the motor
int steps;        // Used to set HOME position after Homing is completed

// Variables to hold entered number on Keypad
volatile int firstnumber = 99; // used to tell how many numbers were entered on keypad
volatile int secondnumber = 99;
volatile int thirdnumber = 99;
volatile int fourthnumber = 99;

// Variables to hold Distance and CurrentPosition
int keyfullnumber = 0; // used to store the final calculated distance value
String currentposition = "";  // Used for display on Nokia LCD

const byte ROWS = 6; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char keys[ROWS][COLS] = {
  {'P', 'I', 'O'},
  {'7', '8', '9', '-'},
  {'4', '5', '6', '+'},
  {'1', '2', '3', '*'},
  {'0', '.', '=', '/'},
  {'S', 'E', 'C'}
};
byte rowPins[ROWS] = {3, 2, 9, 8, 10, 11}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  // Keypad Library definition

//SETUP THE LCD
LiquidCrystal_I2C lcd(0x3F, 20, 4);

// AccelStepper Setup
AccelStepper stepper(1, A0, A1);  // 1 = Easy Driver interface
// Arduino A0 connected to STEP pin of Easy Driver
// Arduino A1 connected to DIR pin of Easy Driver

void setup(void) {

  pinMode(home_switch, INPUT_PULLUP);
  pinMode(end_switch, INPUT_PULLUP);

  //  AccelStepper speed and acceleration setup
  stepper.setMaxSpeed(1500);  // Not to fast or you will have missed steps
  stepper.setAcceleration(400);  //  Same here

  //lcd text setup
  lcd.init();                      // initialize the lcd
  lcd.backlight();
  lcd.print(" ENTER LENGTH IN mm");
  lcd.setCursor(0, 1);
  lcd.print("===>       mm");
  lcd.setCursor(0, 2);
  lcd.print("===>       X"); // X= amount
  lcd.setCursor(0, 3);
  lcd.print("STOP POSITION= 0");
  lcd.setCursor(5, 1);

  // HOMING OF STEPPER MOTOR
  while (digitalRead(home_switch)) {  // Do this until the switch is activated
    digitalWrite(A1, LOW);
    digitalWrite(A0, HIGH);
    delay(1);                       // More delay to slow even more while moving away from switch
    digitalWrite(A0, LOW);
    delay(1);
  }

  while (!digitalRead(home_switch)) { // Do this until the switch is not activated
    digitalWrite(A1, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(A0, HIGH);
    delay(10);                       // Delay to slow down speed of Stepper
    digitalWrite(A0, LOW);
    delay(10);
  }
  steps = 0; // Reset position variable to zero
  stepper.setCurrentPosition(0);

}

void loop() {
  while (digitalRead(end_switch)) {
    char keypressed = keypad.getKey();  // Get value of keypad button if pressed
    if (keypressed != NO_KEY) { // If keypad button pressed check which key it was
      switch (keypressed) {

        case '1':
          checknumber(1);
          break;

        case '2':
          checknumber(2);
          break;

        case '3':
          checknumber(3);
          break;

        case '4':
          checknumber(4);
          break;

        case '5':
          checknumber(5);
          break;

        case '6':
          checknumber(6);
          break;

        case '7':
          checknumber(7);
          break;

        case '8':
          checknumber(8);
          break;

        case '9':
          checknumber(9);
          break;

        case '0':
          checknumber(0);
          break;

        case '*':
          deletenumber();
          break;

        case 'S': // Startknop tigerstop keypad
          calculatedistance();
          break;

        /*   case 'I':
             break;

           /*  case 'A':
             break;

             case 'B':
             break;*/

        case 'C': /// Clear knop tigerstop keypad
          resetnumbers();
          break;

        case 'E': // space-Calib knop tigerstop keypad
          homing();
          break;
      }
    }

    while (!digitalRead(end_switch)) {
      digitalWrite(A1, LOW);      // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(A0, HIGH);
      delay(10);                       // Delay to slow down speed of Stepper
      digitalWrite(A0, LOW);
      delay(10);
    }
  }
}

void homing() {
  while (digitalRead(home_switch)) {  // Do this until the switch is activated
    digitalWrite(A1, LOW);
    digitalWrite(A0, HIGH);
    delay(1);                       // More delay to slow even more while moving away from switch
    digitalWrite(A0, LOW);
    delay(1);
  }

  while (!digitalRead(home_switch)) { // Do this until the switch is not activated

    digitalWrite(A1, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(A0, HIGH);
    delay(10);                       // Delay to slow down speed of Stepper
    digitalWrite(A0, LOW);
    delay(10);
  }

  steps = 0; // Reset position variable to zero
  stepper.setCurrentPosition(0);
  lcd.setCursor(15, 3);
  lcd.print("     ");
  lcd.setCursor(15, 3);
  lcd.print("0");
}

void checknumber(int x) {

  if (firstnumber == 99) { // Check if this is the first number entered
    firstnumber = x;
    String displayvalue = String(firstnumber);  //  Transform int to a string for display
    drawLCD(displayvalue); // Redraw Nokia lcd

  }

  else {
    if (secondnumber == 99) {  // Check if it's the second number entered
      secondnumber = x;
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawLCD(displayvalue);
    }

    else {
      if (thirdnumber == 99) { // It must be the 3rd number entered
        thirdnumber = x;
        String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
        drawLCD(displayvalue);
      }

      else {
        fourthnumber = x;
        String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
        drawLCD(displayvalue);
      }
    }
  }
}

void deletenumber() {  // Used to backspace entered numbers
  if (fourthnumber != 99) {
    String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
    drawLCD(displayvalue);
    lcd.setCursor(8, 1);
    lcd.print(" ");
    fourthnumber = 99;
  }
  else {
    if (thirdnumber != 99) {
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawLCD(displayvalue);
      lcd.setCursor(7, 1);
      lcd.print(" ");
      thirdnumber = 99;
    }

    else {
      if (secondnumber != 99) {
        String displayvalue = String(firstnumber);
        drawLCD(displayvalue);
        lcd.setCursor(6, 1);
        lcd.print(" ");
        secondnumber = 99;
      }

      else {
        if (firstnumber != 99) {
          String displayvalue = "";
          drawLCD(displayvalue);
          lcd.setCursor(5, 1);
          lcd.print(" ");
          firstnumber = 99;
        }
      }
    }
  }
}

void calculatedistance() {  // Used to create a full number from entered numbers

  if (fourthnumber == 99 && thirdnumber == 99 && secondnumber == 99 && firstnumber != 99) {
    keyfullnumber = firstnumber;
    setStepperDestination(keyfullnumber);
  }

  if (secondnumber != 99 && thirdnumber == 99 && fourthnumber == 99) {
    keyfullnumber = (firstnumber * 10) + secondnumber;
    setStepperDestination(keyfullnumber);
  }
  if (thirdnumber != 99 && fourthnumber == 99) {
    keyfullnumber = (firstnumber * 100) + (secondnumber * 10) + thirdnumber;
    setStepperDestination(keyfullnumber);
  }

  if (fourthnumber != 99) {
    keyfullnumber = (firstnumber * 1000) + (secondnumber * 100) + (thirdnumber * 10) + fourthnumber;
    setStepperDestination(keyfullnumber);
  }
  resetnumbers(); // Reset numbers to get ready for new entry
  stepper.setCurrentPosition(0);
  lcd.setCursor(15, 3);
  lcd.print(currentposition);
  lcd.print("     ");
}
void setStepperDestination(int z) {  //  Move the stepper

  int calculatedmove = ((z * 1600) / 80); //  Calculate number of steps needed in mm
  stepper.moveTo(calculatedmove);
  stepper.run();
  currentposition = String(z);
}

void resetnumbers() {  // Reset numbers for next entry
  firstnumber = 99;
  secondnumber = 99;
  thirdnumber = 99;
  fourthnumber = 99;
  lcd.setCursor(5, 1);
  lcd.print("     ");
}
void drawLCD(String y) {
  lcd.setCursor(5, 1);
  lcd.print(y);
}

[/code]

And i forgot, yes i studied the Accelstepper library. But i do not understand why it is not working.

In this code

void setStepperDestination(int z) {  //  Move the stepper

  int calculatedmove = ((z * 1600) / 80); //  Calculate number of steps needed in mm
  stepper.moveTo(calculatedmove);
  stepper.run();
  currentposition = String(z);
}

You did not take my advice to remove the line stepper.run() and put it in your loop() function

...R

I tried and it did not work, i still have no idea where to place it.

[code]
#include "AccelStepper.h" 
#include <Keypad.h> 
#include <LiquidCrystal_I2C.h>

#define home_switch 22 
#define end_switch 23 

int direction;    
int steps;       

volatile int firstnumber = 99; // used to tell how many numbers were entered on keypad
volatile int secondnumber = 99;
volatile int thirdnumber = 99;
volatile int fourthnumber = 99;

// Variables to hold Distance and CurrentPosition
int keyfullnumber = 0; // used to store the final calculated distance value
String currentposition = "";  // Used for display on Nokia LCD

const byte ROWS = 6; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'P', 'I', 'O'},
  {'7', '8', '9', '-'},
  {'4', '5', '6', '+'},
  {'1', '2', '3', '*'},
  {'0', '.', '=', '/'},
  {'S', 'E', 'C'}
};
byte rowPins[ROWS] = {3, 2, 9, 8, 10, 11}; 
byte colPins[COLS] = {7, 6, 5, 4};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); 

//SETUP THE LCD
LiquidCrystal_I2C lcd(0x3F, 20, 4);

// AccelStepper Setup
AccelStepper stepper(1, A0, A1);  // 1 = Easy Driver interface
// Arduino A0 connected to STEP pin of Easy Driver
// Arduino A1 connected to DIR pin of Easy Driver

void setup(void) {

  pinMode(home_switch, INPUT_PULLUP);
  pinMode(end_switch, INPUT_PULLUP);

  //  AccelStepper speed and acceleration setup
  stepper.setMaxSpeed(1500); 
  stepper.setAcceleration(400); 

  //lcd text setup
  lcd.init();                      
  lcd.backlight();
  lcd.print(" ENTER LENGTH IN mm");
  lcd.setCursor(0, 1);
  lcd.print("===>       mm");
  lcd.setCursor(0, 2);
  lcd.print("===>       X"); // X= amount
  lcd.setCursor(0, 3);
  lcd.print("STOP POSITION= 0");
  lcd.setCursor(5, 1);

  // HOMING OF STEPPER MOTOR
  while (digitalRead(home_switch)) {  // Do this until the switch is activated
    digitalWrite(A1, LOW);
    digitalWrite(A0, HIGH);
    delay(1);                       // More delay to slow even more while moving away from switch
    digitalWrite(A0, LOW);
    delay(1);
  }

  while (!digitalRead(home_switch)) { // Do this until the switch is not activated
    digitalWrite(A1, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(A0, HIGH);
    delay(10);                       // Delay to slow down speed of Stepper
    digitalWrite(A0, LOW);
    delay(10);
  }
  steps = 0; // Reset position variable to zero
  stepper.setCurrentPosition(0);

}

void loop() {
  while (digitalRead(end_switch)) {
    stepper.run();
    char keypressed = keypad.getKey();  // Get value of keypad button if pressed
    if (keypressed != NO_KEY) { // If keypad button pressed check which key it was
      switch (keypressed) {

        case '1':
          checknumber(1);
          break;

        case '2':
          checknumber(2);
          break;

        case '3':
          checknumber(3);
          break;

        case '4':
          checknumber(4);
          break;

        case '5':
          checknumber(5);
          break;

        case '6':
          checknumber(6);
          break;

        case '7':
          checknumber(7);
          break;

        case '8':
          checknumber(8);
          break;

        case '9':
          checknumber(9);
          break;

        case '0':
          checknumber(0);
          break;

        case '*':
          deletenumber();
          break;

        case 'S': // Startknop tigerstop keypad
          calculatedistance();
          stepper.run();
          break;

        /*   case 'I':
             break;

           /*  case 'A':
             break;

             case 'B':
             break;*/

        case 'C': /// Clear knop tigerstop keypad
          resetnumbers();
          break;

        case 'E': // space-Calib knop tigerstop keypad
          homing();
          break;
      }
    }

    while (!digitalRead(end_switch)) {
      digitalWrite(A1, LOW);      // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(A0, HIGH);
      delay(10);                       // Delay to slow down speed of Stepper
      digitalWrite(A0, LOW);
      delay(10);
    }
  }
}

void homing() {
  while (digitalRead(home_switch)) {  // Do this until the switch is activated
    digitalWrite(A1, LOW);
    digitalWrite(A0, HIGH);
    delay(1);                       // More delay to slow even more while moving away from switch
    digitalWrite(A0, LOW);
    delay(1);
  }

  while (!digitalRead(home_switch)) { // Do this until the switch is not activated
    digitalWrite(A1, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(A0, HIGH);
    delay(10);                       // Delay to slow down speed of Stepper
    digitalWrite(A0, LOW);
    delay(10);
  }

  steps = 0; // Reset position variable to zero
  stepper.setCurrentPosition(0);
  lcd.setCursor(15, 3);
  lcd.print("     ");
  lcd.setCursor(15, 3);
  lcd.print("0");
}

void checknumber(int x) {
  if (firstnumber == 99) { // Check if this is the first number entered
    firstnumber = x;
    String displayvalue = String(firstnumber);  //  Transform int to a string for display
    drawLCD(displayvalue); // Redraw Nokia lcd
  }
  else {
    if (secondnumber == 99) {  // Check if it's the second number entered
      secondnumber = x;
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawLCD(displayvalue);
    }
    else {
      if (thirdnumber == 99) { // It must be the 3rd number entered
        thirdnumber = x;
        String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
        drawLCD(displayvalue);
      }
      else {
        fourthnumber = x;
        String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
        drawLCD(displayvalue);
      }
    }
  }
}

void deletenumber() {  // Used to backspace entered numbers
  if (fourthnumber != 99) {
    String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
    drawLCD(displayvalue);
    lcd.setCursor(8, 1);
    lcd.print(" ");
    fourthnumber = 99;
  }
  else {
    if (thirdnumber != 99) {
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawLCD(displayvalue);
      lcd.setCursor(7, 1);
      lcd.print(" ");
      thirdnumber = 99;
    }
    else {
      if (secondnumber != 99) {
        String displayvalue = String(firstnumber);
        drawLCD(displayvalue);
        lcd.setCursor(6, 1);
        lcd.print(" ");
        secondnumber = 99;
      }
      else {
        if (firstnumber != 99) {
          String displayvalue = "";
          drawLCD(displayvalue);
          lcd.setCursor(5, 1);
          lcd.print(" ");
          firstnumber = 99;
        }
      }
    }
  }
}

void calculatedistance() {  // Used to create a full number from entered numbers

  if (fourthnumber == 99 && thirdnumber == 99 && secondnumber == 99 && firstnumber != 99) {
    keyfullnumber = firstnumber;
    setStepperDestination(keyfullnumber);
  }

  if (secondnumber != 99 && thirdnumber == 99 && fourthnumber == 99) {
    keyfullnumber = (firstnumber * 10) + secondnumber;
    setStepperDestination(keyfullnumber);
  }

  if (thirdnumber != 99 && fourthnumber == 99) {
    keyfullnumber = (firstnumber * 100) + (secondnumber * 10) + thirdnumber;
    setStepperDestination(keyfullnumber);
  }

  if (fourthnumber != 99) {
    keyfullnumber = (firstnumber * 1000) + (secondnumber * 100) + (thirdnumber * 10) + fourthnumber;
    setStepperDestination(keyfullnumber);
  }
  resetnumbers(); // Reset numbers to get ready for new entry
  stepper.setCurrentPosition(0);
  lcd.setCursor(15, 3);
  lcd.print(currentposition);
  lcd.print("     ");
}
void setStepperDestination(int z) {  //  Move the stepper

  int calculatedmove = ((z * 1600) / 80); //  Calculate number of steps needed in mm
  stepper.moveTo(calculatedmove);
  currentposition = String(z);
}
void resetnumbers() {  // Reset numbers for next entry
  firstnumber = 99;
  secondnumber = 99;
  thirdnumber = 99;
  fourthnumber = 99;
  lcd.setCursor(5, 1);
  lcd.print("     ");
}
void drawLCD(String y) {
  lcd.setCursor(5, 1);
  lcd.print(y);
}

[/code]

You have the call to stepper.run() inside a WHILE loop. just put it as the last line in loop() - not inside anything else. Like this

    while (!digitalRead(end_switch)) {
      digitalWrite(A1, LOW);      // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(A0, HIGH);
      delay(10);                       // Delay to slow down speed of Stepper
      digitalWrite(A0, LOW);
      delay(10);
    }
  }
stepper.run();
}

I would not be at all surprised if you discover that those WHILE loops are preventing loop() from repeating fast enough. I suggest you replace all the WHILEs with IF and allow loop() to do the repetition.

You need to get rid of all the delay()s also. Have a look at how millis() is used to manage timing without blocking in Several Things at a Time. And see Using millis() for timing. A beginners guide if you need more explanation.

...R

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