Stepper motor rotates abnormally

Hi guys I really tried working with the code and editing it, I got the code from youtube and from brainybits. I modified it to accept 5 digit inputs. The weird thing is when inputing different values it does not match up with the rotation. For example.

Stepper motor at 0 steps
Input on Keypad is [99] 1.5 Revolutions (Correct)
The code for this is int calculatedmove=((z1600)/80);
((99
1600)/80)
((158,400)/80)
1,980 steps

Stepper motor at 0 steps
Input on Keypad is [999] 12 Revolutions (Correct)
The code for this is int calculatedmove=((z1600)/80);
((999
1600)/80)
((1,598,400)/80)
19,980 steps

Stepper motor at 0 steps
Input on Keypad is [9999] 2 Revolutions (Not correct, why does this happen?)
The code for this is int calculatedmove=((z1600)/80);
((9999
1600)/80)
((15,998,400)/80)
199,980 steps

Stepper motor at 0 steps
Input on Keypaid is [99999] 19 Revolutions (Not correct, I expected more turns)
The code for this is int calculatedmove=((z1600)/80);
((99999
1600)/80)
((159,998,400)/80)
1,999,980 steps

My stepper driver is configured at 1600 per revolution
Stepper driver is DM542T
Stepper Motor is Nema 23

Can anyone help me on how to fix this? I just learned slowly and started arduino 2 weeks ago.. sorry.

Also I edited the code for hours and debugging, I can't seem to find the problem.

/* Arduino Control Stepper with Keypad and LCD

Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include "AccelStepper.h" // AccelStepper Library
#include <Keypad.h>  // Keypad Library
#include <U8glib.h>  // U8glib for Nokia LCD

// 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;
volatile int fifthnumber=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


// Keypad Setup
const byte ROWS = 4; // Four Rows
const byte COLS = 4; // Four Columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {22, 24, 26, 28}; // Arduino pins connected to the row pins of the keypad
byte colPins[COLS] = {31, 33, 35, 37}; // Arduino pins connected to the column pins of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  // Keypad Library definition


// U8glib Setup for Nokia LCD
#define backlight_pin 11
U8GLIB_PCD8544 u8g(3, 4, 6, 5, 7);  // Arduino pins connected to Nokia pins:
                                    // CLK=3, DIN=4, CE=6, DC=5, RST=7
                                    
                                    
// 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) {
  
  //  Light up the LCD backlight LEDS
  analogWrite(backlight_pin, 0);  // Set the Backlight intensity (0=Bright, 255=Dim)
    
  //  AccelStepper speed and acceleration setup
  stepper.setMaxSpeed(1500);  // Not to fast or you will have missed steps
  stepper.setAcceleration(400);  //  Same here
  
  // Draw starting screen on Nokia LCD
  u8g.firstPage();
  do {
  u8g.drawHLine(0, 15, 84);
  u8g.drawVLine(40, 16, 38);
  u8g.drawHLine(0, 35, 84); 
  u8g.setFont(u8g_font_profont11);
  u8g.drawStr(0, 10, "ENTER DISTANCE");
  u8g.drawStr(62, 29, "MM");
  u8g.drawStr(4, 46, "curpos");
  }
  while( u8g.nextPage() );
  
}


void loop(){
  
  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 '#':
        calculatedistance();
      break;
    }
  }

}

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
    drawnokiascreen(displayvalue); // Redraw Nokia lcd
    
  } else {
    if (secondnumber == 99) {  // Check if it's the second number entered
      secondnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawnokiascreen(displayvalue);

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

    } else { 
     if (fourthnumber == 99) {
      fourthnumber=x;   
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
      drawnokiascreen(displayvalue);
    
     } else { 
      fifthnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber) + String(fifthnumber));
      drawnokiascreen(displayvalue);
     }
     }
    }
    }
    }
  
void deletenumber() {  // Used to backspace entered numbers

 if (fifthnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
      drawnokiascreen(displayvalue);

    fifthnumber=99;
  } 


 else { 
 if (fourthnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
      drawnokiascreen(displayvalue);

    fourthnumber=99;
  } 


 else { 
  if (thirdnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawnokiascreen(displayvalue);

    thirdnumber=99;
  } 
  else {
    if (secondnumber !=99) {
      String displayvalue = String(firstnumber);
      drawnokiascreen(displayvalue);

      secondnumber=99;
   } 
   else {
     if (firstnumber !=99) {
       String displayvalue = "";
       drawnokiascreen(displayvalue);

       firstnumber=99;
        }
      }
    }
  }
}
}  
void calculatedistance() {  // Used to create a full number from entered numbers

    if (fifthnumber == 99 && fourthnumber == 99 && thirdnumber == 99 && secondnumber == 99 && firstnumber != 99) {
      keyfullnumber=firstnumber;
      movestepper(keyfullnumber);
    }
    // this code is for the second number 
    if (secondnumber != 99 && thirdnumber == 99 && fourthnumber == 99 && fifthnumber == 99) {
      keyfullnumber=(firstnumber*10)+secondnumber;
      movestepper(keyfullnumber);
    }
    
    if (thirdnumber != 99 && fourthnumber == 99 && fifthnumber == 99) {
      keyfullnumber=(firstnumber*100)+(secondnumber*10)+thirdnumber;
      movestepper(keyfullnumber);
    }

    if (fourthnumber != 99 && fifthnumber == 99) {
      keyfullnumber=(firstnumber*1000)+(secondnumber*100)+(thirdnumber*10)+fourthnumber;
      movestepper(keyfullnumber);
    }
    
    if (fifthnumber != 99) {
      keyfullnumber=(firstnumber*10000)+(secondnumber*1000)+(thirdnumber*100)+(fourthnumber*10)+fifthnumber;
      movestepper(keyfullnumber);
    }
    resetnumbers(); // Reset numbers to get ready for new entry
  } 















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);
  u8g.firstPage();
  do {
    u8g.drawHLine(0, 15, 84);
    u8g.drawVLine(50, 16, 38);
    u8g.drawHLine(0, 35, 84); 
    u8g.setFont(u8g_font_profont11);
    u8g.drawStr(0, 10, "ENTER DISTANCE");
    u8g.drawStr(62, 29, "MM");
    u8g.drawStr(4, 46, "cur-pos");
    u8g.setPrintPos(57,47);
    u8g.print(currentposition);       
  }
  while( u8g.nextPage() ); 
}
                
void resetnumbers() {  // Reset numbers for next entry
  firstnumber=99;
  secondnumber=99;
  thirdnumber=99;
  fourthnumber=99;
  fifthnumber=99;
} 
  

void drawnokiascreen(String y) {

    u8g.firstPage();
    do {
      u8g.drawHLine(0, 15, 84);
      u8g.drawVLine(50, 16, 38);
      u8g.drawHLine(0, 35, 84); 
      u8g.setFont(u8g_font_profont11);
      u8g.drawStr(0, 10, "ENTER DISTANCE");
      u8g.setPrintPos(0,29);
      u8g.print(y);  // Put entered number on Nokia lcd    
      u8g.drawStr(62, 29, "MM");
      u8g.drawStr(4, 46, "cur-pos");
      u8g.setPrintPos(57,47); 
      u8g.print(currentposition);  //  Display current position of stepper
    }
      while( u8g.nextPage() );

}

@xplitz, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project :wink: See About the Installation & Troubleshooting category.

Assuming that you're using an 8-bit micro (e.g. Uno, Mega), 99999 does not fit in an 16-bit integer; change the type of keyfullnumber to long

long keyfullnumber = 0; // used to store the final calculated distance value

I'm not familiar with AccelStepper (and steppers in general), but above is a possible cause of your problem.

Thank you for the advice, I'm also sorry about not posting in the right category, I will read more on that. Thank you, I will try it now.

Same result
Input 9999 on keyboard returns 2 Revolutions

/* Arduino Control Stepper with Keypad and LCD

Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include "AccelStepper.h" // AccelStepper Library
#include <Keypad.h>  // Keypad Library
#include <U8glib.h>  // U8glib for Nokia LCD

// 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;
volatile int fifthnumber=99;

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


// Keypad Setup
const byte ROWS = 4; // Four Rows
const byte COLS = 4; // Four Columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {22, 24, 26, 28}; // Arduino pins connected to the row pins of the keypad
byte colPins[COLS] = {31, 33, 35, 37}; // Arduino pins connected to the column pins of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  // Keypad Library definition


// U8glib Setup for Nokia LCD
#define backlight_pin 11
U8GLIB_PCD8544 u8g(3, 4, 6, 5, 7);  // Arduino pins connected to Nokia pins:
                                    // CLK=3, DIN=4, CE=6, DC=5, RST=7
                                    
                                    
// 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) {
  
  //  Light up the LCD backlight LEDS
  analogWrite(backlight_pin, 20);  // Set the Backlight intensity (0=Bright, 255=Dim)
    
  //  AccelStepper speed and acceleration setup
  stepper.setMaxSpeed(1500);  // Not to fast or you will have missed steps
  stepper.setAcceleration(400);  //  Same here
  
  // Draw starting screen on Nokia LCD
  u8g.firstPage();
  do {
  u8g.drawHLine(0, 15, 84);
  u8g.drawVLine(40, 16, 38);
  u8g.drawHLine(0, 35, 84); 
  u8g.setFont(u8g_font_profont11);
  u8g.drawStr(0, 10, "ENTER DISTANCE");
  u8g.drawStr(62, 29, "MM");
  u8g.drawStr(4, 46, "curpos");
  }
  while( u8g.nextPage() );
  
}


void loop(){
  
  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 '#':
        calculatedistance();
      break;
    }
  }

}

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
    drawnokiascreen(displayvalue); // Redraw Nokia lcd
    
  } else {
    if (secondnumber == 99) {  // Check if it's the second number entered
      secondnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawnokiascreen(displayvalue);

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

    } else { 
     if (fourthnumber == 99) {
      fourthnumber=x;   
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
      drawnokiascreen(displayvalue);
    
     } else { 
      fifthnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber) + String(fifthnumber));
      drawnokiascreen(displayvalue);
     }
     }
    }
    }
    }
  
void deletenumber() {  // Used to backspace entered numbers

 if (fifthnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
      drawnokiascreen(displayvalue);

    fifthnumber=99;
  } 


 else { 
 if (fourthnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
      drawnokiascreen(displayvalue);

    fourthnumber=99;
  } 


 else { 
  if (thirdnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawnokiascreen(displayvalue);

    thirdnumber=99;
  } 
  else {
    if (secondnumber !=99) {
      String displayvalue = String(firstnumber);
      drawnokiascreen(displayvalue);

      secondnumber=99;
   } 
   else {
     if (firstnumber !=99) {
       String displayvalue = "";
       drawnokiascreen(displayvalue);

       firstnumber=99;
        }
      }
    }
  }
}
}  
void calculatedistance() {  // Used to create a full number from entered numbers

    if (fifthnumber == 99 && fourthnumber == 99 && thirdnumber == 99 && secondnumber == 99 && firstnumber != 99) {
      keyfullnumber=firstnumber;
      movestepper(keyfullnumber);
    }
    // this code is for the second number 
    if (secondnumber != 99 && thirdnumber == 99 && fourthnumber == 99 && fifthnumber == 99) {
      keyfullnumber=(firstnumber*10)+secondnumber;
      movestepper(keyfullnumber);
    }
    
    if (thirdnumber != 99 && fourthnumber == 99 && fifthnumber == 99) {
      keyfullnumber=(firstnumber*100)+(secondnumber*10)+thirdnumber;
      movestepper(keyfullnumber);
    }

    if (fourthnumber != 99 && fifthnumber == 99) {
      keyfullnumber=(firstnumber*1000)+(secondnumber*100)+(thirdnumber*10)+fourthnumber;
      movestepper(keyfullnumber);
    }
    
    if (fifthnumber != 99) {
      keyfullnumber=(firstnumber*10000)+(secondnumber*1000)+(thirdnumber*100)+(fourthnumber*10)+fifthnumber;
      movestepper(keyfullnumber);
    }
    resetnumbers(); // Reset numbers to get ready for new entry
  } 















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

  long calculatedmove=((z*1600)/80);  //  Calculate number of steps needed in mm
  stepper.runToNewPosition(calculatedmove);
  currentposition = String(z);
  u8g.firstPage();
  do {
    u8g.drawHLine(0, 15, 84);
    u8g.drawVLine(50, 16, 38);
    u8g.drawHLine(0, 35, 84); 
    u8g.setFont(u8g_font_profont11);
    u8g.drawStr(0, 10, "ENTER DISTANCE");
    u8g.drawStr(62, 29, "MM");
    u8g.drawStr(4, 46, "cur-pos");
    u8g.setPrintPos(57,47);
    u8g.print(currentposition);       
  }
  while( u8g.nextPage() ); 
}
                
void resetnumbers() {  // Reset numbers for next entry
  firstnumber=99;
  secondnumber=99;
  thirdnumber=99;
  fourthnumber=99;
  fifthnumber=99;
} 
  

void drawnokiascreen(String y) {

    u8g.firstPage();
    do {
      u8g.drawHLine(0, 15, 84);
      u8g.drawVLine(50, 16, 38);
      u8g.drawHLine(0, 35, 84); 
      u8g.setFont(u8g_font_profont11);
      u8g.drawStr(0, 10, "ENTER DISTANCE");
      u8g.setPrintPos(0,29);
      u8g.print(y);  // Put entered number on Nokia lcd    
      u8g.drawStr(62, 29, "MM");
      u8g.drawStr(4, 46, "cur-pos");
      u8g.setPrintPos(57,47); 
      u8g.print(currentposition);  //  Display current position of stepper
    }
      while( u8g.nextPage() );

}

If you change the code, post the new version so we can keep up.

1 Like

The other problem is that calculations are by default done based on integers unless one of the parts of the calculation is a long. In below, the L in 10L forces the calculation to be done with longs.

    keyfullnumber = (firstnumber * 10L) + secondnumber;
                                     ^
                                     |
                                     +-- force calculation with long

Repeat for the other digits.

Below demo code demonstrates; note that pins used for the keypad are different from yours and that I use the serial output for debugging.

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char keyMap[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = {4, 5, 6, 7};  //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 9, 10};    //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad keypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);

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;
volatile int fifthnumber = 99;

long keyfullnumber = 0; // used to store the final calculated distance value


void setup()
{
  Serial.begin(57600);
  while (!Serial);
  Serial.println("keypad demo");
}

void loop() {

  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 '0'...'9':
        checknumber(keypressed - '0');
        break;
      case '*':
        deletenumber();
        break;
      case '#':
        calculatedistance();
        break;
    }
  }
}

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

  }
  else
  {
    if (secondnumber == 99)
    { // Check if it's the second number entered
      secondnumber = x;
      String displayvalue = (String(firstnumber) + String(secondnumber));
      Serial.println(displayvalue); // Redraw Nokia lcd

    }
    else
    {
      if (thirdnumber == 99)
      { // Check if it's the third number entered
        thirdnumber = x;
        String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
        Serial.println(displayvalue); // Redraw Nokia lcd

      }
      else
      {
        if (fourthnumber == 99)
        {
          fourthnumber = x;
          String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
          Serial.println(displayvalue); // Redraw Nokia lcd

        }
        else
        {
          fifthnumber = x;
          String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber) + String(fifthnumber));
          Serial.println(displayvalue); // Redraw Nokia lcd
        }
      }
    }
  }
}

void deletenumber()
{  // Used to backspace entered numbers
  Serial.println(__FUNCTION__);

  if (fifthnumber != 99) {
    String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
    Serial.println(displayvalue); // Redraw Nokia lcd
    fifthnumber = 99;
  }
  else
  {
    if (fourthnumber != 99)
    {
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
      Serial.println(displayvalue); // Redraw Nokia lcd
      fourthnumber = 99;
    }
    else
    {
      if (thirdnumber != 99)
      {
        String displayvalue = (String(firstnumber) + String(secondnumber));
        Serial.println(displayvalue); // Redraw Nokia lcd
        thirdnumber = 99;
      }
      else
      {
        if (secondnumber != 99)
        {
          String displayvalue = String(firstnumber);
          Serial.println(displayvalue); // Redraw Nokia lcd

          secondnumber = 99;
        }
        else
        {
          if (firstnumber != 99)
          {
            String displayvalue = "";
            Serial.println(displayvalue); // Redraw Nokia lcd
            firstnumber = 99;
          }
        }
      }
    }
  }
}

void calculatedistance()
{ // Used to create a full number from entered numbers
  Serial.println(__FUNCTION__);
  if (fifthnumber == 99 && fourthnumber == 99 && thirdnumber == 99 && secondnumber == 99 && firstnumber != 99) {
    keyfullnumber = firstnumber;
    Serial.println(keyfullnumber);
  }
  // this code is for the second number
  if (secondnumber != 99 && thirdnumber == 99 && fourthnumber == 99 && fifthnumber == 99) {
    keyfullnumber = (firstnumber * 10L) + secondnumber;
    Serial.println(keyfullnumber);
  }

  if (thirdnumber != 99 && fourthnumber == 99 && fifthnumber == 99) {
    keyfullnumber = (firstnumber * 100L) + (secondnumber * 10) + thirdnumber;
    Serial.println(keyfullnumber);
  }

  if (fourthnumber != 99 && fifthnumber == 99) {
    keyfullnumber = (firstnumber * 1000L) + (secondnumber * 100) + (thirdnumber * 10) + fourthnumber;
    Serial.println(keyfullnumber);
  }

  if (fifthnumber != 99) {
    keyfullnumber = (firstnumber * 10000L) + (secondnumber * 1000) + (thirdnumber * 100) + (fourthnumber * 10) + fifthnumber;
    Serial.println(keyfullnumber);
  }
  resetnumbers(); // Reset numbers to get ready for new entry
}

void resetnumbers() {  // Reset numbers for next entry
  firstnumber = 99;
  secondnumber = 99;
  thirdnumber = 99;
  fourthnumber = 99;
  fifthnumber = 99;
}

I've streamlined your loop() function.

The main trick is the conversion from text to integer by subtracting '0'. E.g. ascii character '1' is decimal value 49; subtracting '0' (decimal value 48) will give you the number 1.

The second trick is to use a range in the case.

1 Like

Thank you, it looks so much cleaner now, but the problem still persist, is it a bug on the code library, or the arduino mega can't handle it? Thank you sterretje I will study the loop fuction you shared to me.

/* Arduino Control Stepper with Keypad and LCD

Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include "AccelStepper.h" // AccelStepper Library
#include <Keypad.h>  // Keypad Library
#include <U8glib.h>  // U8glib for Nokia LCD

// 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;
volatile int fifthnumber=99;

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


// Keypad Setup
const byte ROWS = 4; // Four Rows
const byte COLS = 4; // Four Columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {22, 24, 26, 28}; // Arduino pins connected to the row pins of the keypad
byte colPins[COLS] = {31, 33, 35, 37}; // Arduino pins connected to the column pins of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  // Keypad Library definition


// U8glib Setup for Nokia LCD
#define backlight_pin 11
U8GLIB_PCD8544 u8g(3, 4, 6, 5, 7);  // Arduino pins connected to Nokia pins:
                                    // CLK=3, DIN=4, CE=6, DC=5, RST=7
                                    
                                    
// 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) {
  
  //  Light up the LCD backlight LEDS
  analogWrite(backlight_pin, 20);  // Set the Backlight intensity (0=Bright, 255=Dim)
    
  //  AccelStepper speed and acceleration setup
  stepper.setMaxSpeed(1500);  // Not to fast or you will have missed steps
  stepper.setAcceleration(400);  //  Same here
  
  // Draw starting screen on Nokia LCD
  u8g.firstPage();
  do {
  u8g.drawHLine(0, 15, 84);
  u8g.drawVLine(40, 16, 38);
  u8g.drawHLine(0, 35, 84); 
  u8g.setFont(u8g_font_profont11);
  u8g.drawStr(0, 10, "ENTER DISTANCE");
  u8g.drawStr(62, 29, "MM");
  u8g.drawStr(4, 46, "curpos");
  }
  while( u8g.nextPage() );
  
}


void loop() {

  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 '0'...'9':
        checknumber(keypressed - '0');
        break;
      case '*':
        deletenumber();
        break;
      case '#':
        calculatedistance();
        break;
    }
  }

}

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
    drawnokiascreen(displayvalue); // Redraw Nokia lcd
    
  } else {
    if (secondnumber == 99) {  // Check if it's the second number entered
      secondnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawnokiascreen(displayvalue);

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

    } else { 
     if (fourthnumber == 99) {
      fourthnumber=x;   
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
      drawnokiascreen(displayvalue);
    
     } else { 
      fifthnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber) + String(fifthnumber));
      drawnokiascreen(displayvalue);
     }
     }
    }
    }
    }
  
void deletenumber() {  // Used to backspace entered numbers

 if (fifthnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
      drawnokiascreen(displayvalue);

    fifthnumber=99;
  } 


 else { 
 if (fourthnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
      drawnokiascreen(displayvalue);

    fourthnumber=99;
  } 


 else { 
  if (thirdnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawnokiascreen(displayvalue);

    thirdnumber=99;
  } 
  else {
    if (secondnumber !=99) {
      String displayvalue = String(firstnumber);
      drawnokiascreen(displayvalue);

      secondnumber=99;
   } 
   else {
     if (firstnumber !=99) {
       String displayvalue = "";
       drawnokiascreen(displayvalue);

       firstnumber=99;
        }
      }
    }
  }
}
}  
void calculatedistance() {  // Used to create a full number from entered numbers

    if (fifthnumber == 99 && fourthnumber == 99 && thirdnumber == 99 && secondnumber == 99 && firstnumber != 99) {
      keyfullnumber=firstnumber;
      movestepper(keyfullnumber);
    }
    // this code is for the second number 
    if (secondnumber != 99 && thirdnumber == 99 && fourthnumber == 99 && fifthnumber == 99) {
      keyfullnumber=(firstnumber*10L)+secondnumber;
      movestepper(keyfullnumber);
    }
    
    if (thirdnumber != 99 && fourthnumber == 99 && fifthnumber == 99) {
      keyfullnumber=(firstnumber*100L)+(secondnumber*10L)+thirdnumber;
      movestepper(keyfullnumber);
    }

    if (fourthnumber != 99 && fifthnumber == 99) {
      keyfullnumber=(firstnumber*1000L)+(secondnumber*100L)+(thirdnumber*10L)+fourthnumber;
      movestepper(keyfullnumber);
    }
    
    if (fifthnumber != 99) {
      keyfullnumber=(firstnumber*10000L)+(secondnumber*1000L)+(thirdnumber*100L)+(fourthnumber*10L)+fifthnumber;
      movestepper(keyfullnumber);
    }
    resetnumbers(); // Reset numbers to get ready for new entry
  } 















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

  long calculatedmove=((z*1600)/80);  //  Calculate number of steps needed in mm
  stepper.runToNewPosition(calculatedmove);
  currentposition = String(z);
  u8g.firstPage();
  do {
    u8g.drawHLine(0, 15, 84);
    u8g.drawVLine(50, 16, 38);
    u8g.drawHLine(0, 35, 84); 
    u8g.setFont(u8g_font_profont11);
    u8g.drawStr(0, 10, "ENTER DISTANCE");
    u8g.drawStr(62, 29, "MM");
    u8g.drawStr(4, 46, "cur-pos");
    u8g.setPrintPos(57,47);
    u8g.print(currentposition);       
  }
  while( u8g.nextPage() ); 
}
                
void resetnumbers() {  // Reset numbers for next entry
  firstnumber=99;
  secondnumber=99;
  thirdnumber=99;
  fourthnumber=99;
  fifthnumber=99;
} 
  

void drawnokiascreen(String y) {

    u8g.firstPage();
    do {
      u8g.drawHLine(0, 15, 84);
      u8g.drawVLine(50, 16, 38);
      u8g.drawHLine(0, 35, 84); 
      u8g.setFont(u8g_font_profont11);
      u8g.drawStr(0, 10, "ENTER DISTANCE");
      u8g.setPrintPos(0,29);
      u8g.print(y);  // Put entered number on Nokia lcd    
      u8g.drawStr(62, 29, "MM");
      u8g.drawStr(4, 46, "cur-pos");
      u8g.setPrintPos(57,47); 
      u8g.print(currentposition);  //  Display current position of stepper
    }
      while( u8g.nextPage() );

}

I noticed something when I input 99999 on the keypad after the stepper motor turns, the nokia display says current position is -3107 . when its supposed to be 99999.

I did a bit of research; according to https://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html:

1 Like

I found the solution THANK YOU SO MUCH sterretje, without your guidance I couldn't have solved this I just added the L to the 1600.

The code is below if anyone wants to copy. THANK YOU!!

/* Arduino Control Stepper with Keypad and LCD

Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include "AccelStepper.h" // AccelStepper Library
#include <Keypad.h>  // Keypad Library
#include <U8glib.h>  // U8glib for Nokia LCD

// 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;
volatile int fifthnumber=99;

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


// Keypad Setup
const byte ROWS = 4; // Four Rows
const byte COLS = 4; // Four Columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {22, 24, 26, 28}; // Arduino pins connected to the row pins of the keypad
byte colPins[COLS] = {31, 33, 35, 37}; // Arduino pins connected to the column pins of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  // Keypad Library definition


// U8glib Setup for Nokia LCD
#define backlight_pin 11
U8GLIB_PCD8544 u8g(3, 4, 6, 5, 7);  // Arduino pins connected to Nokia pins:
                                    // CLK=3, DIN=4, CE=6, DC=5, RST=7
                                    
                                    
// 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) {
  
  //  Light up the LCD backlight LEDS
  analogWrite(backlight_pin, 20);  // Set the Backlight intensity (0=Bright, 255=Dim)
    
  //  AccelStepper speed and acceleration setup
  stepper.setMaxSpeed(3000);  // Not to fast or you will have missed steps
  stepper.setAcceleration(800);  //  Same here
  
  // Draw starting screen on Nokia LCD
  u8g.firstPage();
  do {
  u8g.drawHLine(0, 15, 84);
  u8g.drawVLine(40, 16, 38);
  u8g.drawHLine(0, 35, 84); 
  u8g.setFont(u8g_font_profont11);
  u8g.drawStr(0, 10, "ENTER DISTANCE");
  u8g.drawStr(62, 29, "MM");
  u8g.drawStr(4, 46, "curpos");
  }
  while( u8g.nextPage() );
  
}


void loop() {

  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 '0'...'9':
        checknumber(keypressed - '0');
        break;
      case '*':
        deletenumber();
        break;
      case '#':
        calculatedistance();
        break;
    }
  }

}

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
    drawnokiascreen(displayvalue); // Redraw Nokia lcd
    
  } else {
    if (secondnumber == 99) {  // Check if it's the second number entered
      secondnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawnokiascreen(displayvalue);

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

    } else { 
     if (fourthnumber == 99) {
      fourthnumber=x;   
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
      drawnokiascreen(displayvalue);
    
     } else { 
      fifthnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber) + String(fifthnumber));
      drawnokiascreen(displayvalue);
     }
     }
    }
    }
    }
  
void deletenumber() {  // Used to backspace entered numbers

 if (fifthnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber) + String(fourthnumber));
      drawnokiascreen(displayvalue);

    fifthnumber=99;
  } 


 else { 
 if (fourthnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
      drawnokiascreen(displayvalue);

    fourthnumber=99;
  } 


 else { 
  if (thirdnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawnokiascreen(displayvalue);

    thirdnumber=99;
  } 
  else {
    if (secondnumber !=99) {
      String displayvalue = String(firstnumber);
      drawnokiascreen(displayvalue);

      secondnumber=99;
   } 
   else {
     if (firstnumber !=99) {
       String displayvalue = "";
       drawnokiascreen(displayvalue);

       firstnumber=99;
        }
      }
    }
  }
}
}  
void calculatedistance() {  // Used to create a full number from entered numbers

    if (fifthnumber == 99 && fourthnumber == 99 && thirdnumber == 99 && secondnumber == 99 && firstnumber != 99) {
      keyfullnumber=firstnumber;
      movestepper(keyfullnumber);
    }
    // this code is for the second number 
    if (secondnumber != 99 && thirdnumber == 99 && fourthnumber == 99 && fifthnumber == 99) {
      keyfullnumber=(firstnumber*10L)+secondnumber;
      movestepper(keyfullnumber);
    }
    
    if (thirdnumber != 99 && fourthnumber == 99 && fifthnumber == 99) {
      keyfullnumber=(firstnumber*100L)+(secondnumber*10L)+thirdnumber;
      movestepper(keyfullnumber);
    }

    if (fourthnumber != 99 && fifthnumber == 99) {
      keyfullnumber=(firstnumber*1000L)+(secondnumber*100L)+(thirdnumber*10L)+fourthnumber;
      movestepper(keyfullnumber);
    }
    
    if (fifthnumber != 99) {
      keyfullnumber=(firstnumber*10000L)+(secondnumber*1000L)+(thirdnumber*100L)+(fourthnumber*10L)+fifthnumber;
      movestepper(keyfullnumber);
    }
    resetnumbers(); // Reset numbers to get ready for new entry
  } 















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

  long calculatedmove=((z*1600L)/80);  //  Calculate number of steps needed in mm
  stepper.runToNewPosition(calculatedmove);
  currentposition = String(z);
  u8g.firstPage();
  do {
    u8g.drawHLine(0, 15, 84);
    u8g.drawVLine(50, 16, 38);
    u8g.drawHLine(0, 35, 84); 
    u8g.setFont(u8g_font_profont11);
    u8g.drawStr(0, 10, "ENTER DISTANCE");
    u8g.drawStr(62, 29, "MM");
    u8g.drawStr(4, 46, "cur-pos");
    u8g.setPrintPos(57,47);
    u8g.print(currentposition);       
  }
  while( u8g.nextPage() ); 
}
                
void resetnumbers() {  // Reset numbers for next entry
  firstnumber=99;
  secondnumber=99;
  thirdnumber=99;
  fourthnumber=99;
  fifthnumber=99;
} 
  

void drawnokiascreen(String y) {

    u8g.firstPage();
    do {
      u8g.drawHLine(0, 15, 84);
      u8g.drawVLine(50, 16, 38);
      u8g.drawHLine(0, 35, 84); 
      u8g.setFont(u8g_font_profont11);
      u8g.drawStr(0, 10, "ENTER DISTANCE");
      u8g.setPrintPos(0,29);
      u8g.print(y);  // Put entered number on Nokia lcd    
      u8g.drawStr(62, 29, "MM");
      u8g.drawStr(4, 46, "cur-pos");
      u8g.setPrintPos(57,47); 
      u8g.print(currentposition);  //  Display current position of stepper
    }
      while( u8g.nextPage() );

}
1 Like

I noticed that in serial monitor before I modified the code to use longs and force longs.

Great. I was still looking at that :wink:

There is a solution checkbox under the replies; you can tick the one under the most useful reply to let others know that the issue is solved and a solution was provided.

1 Like