Control a Stepper motor using a Keypad (4 digit) help

Im trying to add a 4 digit to the code, it works but somthing strange happen if i go over 999.

The stepper moves as it needs to all the way up to 1600 mm, if i add exemple 1650mm the stepper moves the outher way
Do i need to add somthing to use 4digit to the code to make it work like it does from 0 to 1600?

[[code]
`#include // AccelStepper Library
#include // 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;

// 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

// parte pin reset carrello

void setup(void) {

// Light up the LCD backlight LEDS
analogWrite(backlight_pin, 50); // Set the Backlight intensity (0=Bright, 255=Dim)

// AccelStepper speed and acceleration setup
stepper.setMaxSpeed(1400); // 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(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”);
}
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 3rd number entered
thirdnumber=x;
String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
drawnokiascreen(displayvalue);

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

}
}
}
}

void deletenumber() { // Used to backspace entered numbers
if (fourthnumber !=99) {
String displayvalue = (String(firstnumber) + String(secondnumber) + String(fourthnumber));
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 (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
}

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;
}

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

}

/code]

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;

Why are these volatile?

The convention for variable names is camelCase. Your camel needs a drink of water.

You need to seriously consider arrays.

String currentposition = ""; // Used for display on Nokia LCD

Why the hell would you store position information in a String?

Your switch statement is ridiculous. An if/else if statement would require a LOT less code.

You absolutely do not need to abuse the String class that way. Determine the 4 digit number to be displayed, and then use itoa() to convert it to a string. If the method that draws text on the screen does not accept a string, find a better library.

thanks for your fast answer.This code work perfect with 3 numbers.I have the problem with 4 numbers.
If you have better solution i am open to all ideas but i dont now how to modify this code.
best regards
kostas

} else { // It must be the 3rd number entered

Really? After you've already dealt with the first, second, and third number?

What do your Serial.printt()s tell you happening? Why not?

The code does SOMETHING. You expect it to do SOMETHING. All we know at this point is that the two things are not the same thing. Some useful information needs to be forthcoming if you expect help.

if it helps i take the code from here https://brainy-bits.com/tutorials/diy-stepper-miter-box/ and put another number to entered more distance

Your title "Control a Stepper motor using a Keypad (4 digit) help" refers to two completely separate things.

The first is controlling a stepper motor. The second is getting data from a keypad.

They should be two quite separate functions in your program. Then you can test the two parts of the code separately.

...R

1)I test this code with 3 numbers and work perfectly.

2)this:} else { // It must be the 3rd number entered
has been :else { // It must be the 4rd number entered

You still have not answered the question. What DOES the code do? Why are there no Serial.print() statements to tell you what is happening. The ONLY person that can debug your code is YOU, unless you want to send me all of your hardware.

my hardware:
arduino Mega 2560
nokia 5110
keypand 4x4
easy driver v4
stepper motor
i don't use serial.print because i have the nokia screen
The code move a measurament to position that entering from keypad in mm

PROFILCA:
1)I test this code with 3 numbers and work perfectly.

2)this:} else { // It must be the 3rd number entered
has been :else { // It must be the 4rd number entered

You are missing my point.

Your keypad code should put a number in a variable - and then you can print the variable to see if the keypad code is working. This has nothing to do with a stepper motor. The exact same code could be used as part of a calculator program or a keycode program.

Separately you have stepper code that uses the value in that variable to do stuff. You can test the stepper code by just putting a value in the variable with code such as myKeyVar = 12345;

...R

Robin2:
You are missing my point.

Your keypad code should put a number in a variable - and then you can print the variable to see if the keypad code is working. This has nothing to do with a stepper motor. The exact same code could be used as part of a calculator program or a keycode program.

Separately you have stepper code that uses the value in that variable to do stuff. You can test the stepper code by just putting a value in the variable with code such as myKeyVar = 12345;

...R

in wich point of code i must put myKeyVar = 12345;

There is no need for a String or even a char array. Arithmetic will suffice.

PROFILCA:
in wich point of code i must put myKeyVar = 12345;

If you have to ask that question then DO NOT put it anywhere in your program. It will only make matters worse.

Just write your keypad code so that when you enter a number it puts the value into a varaible - I suggested the name myKeyVar but you can call it anything you like.

...R

Just guessing, but do you suspect the problem might be here:

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

long calculatedmove=(z*1600)/80; // Calculate number of steps needed in mm

Like I said, just guessing. But the coincidence of the number 1600 stands out.

You need to take this program apart, so that you understand exactly what it is doing in each statement. Likely, nobody here has compiled and run this example (in fact, as posted, it won't compile), so an instant solution is unlikely. The only way is to reconstruct a program (based on this example) that does what you want.

Yeah, I know, it's a lot of work.

ChrisTenone:
Just guessing, but do you suspect the problem might be here:

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

long calculatedmove=(z*1600)/80; // Calculate number of steps needed in mm




Like I said, just guessing. But the coincidence of the number 1600 stands out.

dear friend the problem I do not think this is.
I got information from here: Easy Driver Examples

(Since we are not pulling either MS1 or MS2 low on the Easy Driver low, the Easy Driver will default to 1 / 8th microstep mode. That means that each time the "digitalWrite (9, HIGH);" call is executed, the stepper motor will move 1 / 8th of a full step. So if your motor is 1.8 degrees per step, there will be 200 full steps per revolution, or 1600 microsteps per revolution.)

Robin2:
You are missing my point.

Your keypad code should put a number in a variable - and then you can print the variable to see if the keypad code is working. This has nothing to do with a stepper motor. The exact same code could be used as part of a calculator program or a keycode program.

Separately you have stepper code that uses the value in that variable to do stuff. You can test the stepper code by just putting a value in the variable with code such as myKeyVar = 12345;

...R

Dear friend thank you for your interest but as I wrote before I do not have so many knowledge. For any HELP can someone giveto me thanks or otherwise does not matter Searching the internet I hope to get HELP.
Sorry if there are some mistakes in my writing but did not know English very well
Thank you again

PROFILCA:
Dear friend thank you for your interest but as I wrote before I do not have so many knowledge.

If you explain what you don't understand I will try to help.

Try writing a short program to read the keypad and save the number in a variable and then display the variable on the Serial Monitor.

...R

i make this and work ok

[code]
/*  Keypadtest.pde
 *
 *  Demonstrate the simplest use of the  keypad library.
 *
 *  The first step is to connect your keypad to the
 *  Arduino  using the pin numbers listed below in
 *  rowPins[] and colPins[]. If you want to use different
 *  pins then  you  can  change  the  numbers below to
 *  match your setup.
 *
 */
#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13

void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  Serial.begin(9600);
}

void loop()
{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
      case '*':
        digitalWrite(ledpin, LOW);
        break;
      case '#':
        digitalWrite(ledpin, HIGH);
        break;
      default:
        Serial.println(key);
    }
  }
}

[/code]

with this code

That seems like it just detects a single key and I think you want to build up a larger number with 3 or 4 key presses (such as 1600).

Try extending that example code to do that.

Alternatively take a copy of the keypad code out of your other program and make it into a short test program.

...R