lcd.print statement printing over n over filling the lcd screen

Hello

What I want is for the program to print "Number of coils?" and then receive input. Then take input and then do something with it. I have a (some what) working program that lacks the initial question "Number of Coils". It's pretty ugly but works. here is what I have so far.

/* @file MyAutoCoil
 || @version 6.0
 || @author Submicro
 || @contact submicro68@gmail.com
 ||
 || @description
 || | Asks for user input then
 || | Makes coils with stepper motors
 || | Still incomplete, needs alot of work.
 || | At least it WORKS!!
 || #
 */
 
#include <Wire.h> //==========================
#include <Keypad.h> //===== Included Libs ====
#include <Stepper.h> //=======================
#include <LiquidCrystal_I2C.h> //=============

//======initialize Variables_=======
byte numberOfCoils = 0; //==========
long totalNumberOfSteps = 0; //=====
const int stepsPerCoil = 800; //====
const byte enablePin0 = 38; //======
const byte enablePin1 = 56; //======
byte coils; //======================

//=======_setup keypad library ============
const byte ROWS = 4; // ==four row ========
const byte COLS = 3; // ==three columns ===
char keys[ROWS][COLS] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'*','0','#'}
};

byte rowPins[ROWS] = {47, 37, 39, 43}; // ==== connect to the row pinouts of the keypad ====
byte colPins[COLS] = {45, 32, 41}; // ====== connect to the column pinouts of the keypad ===

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // ==========
LiquidCrystal_I2C lcd(0x27,20,4); // ========= initialize the liquid crystal library ==
Stepper myStepper0(stepsPerCoil, 54, 55); // ==== initialize the stepper library ======
Stepper myStepper1(stepsPerCoil, 60, 61); // ==== initialize the stepper library ======

byte ledPin = 13; 

boolean blink = false; //==Set initial state==
boolean ledPin_state; //======================

void setup(){
    lcd.init(); //================ Initialize LCD ======
    lcd.backlight(); //========= Turn on backlight =====
    lcd.setCursor(0, 1); //====== Cursor Position ======
    pinMode(ledPin, OUTPUT); //==== Sets the digital pin as output.====
    pinMode(enablePin0, OUTPUT); //=========== Active low =============
    pinMode(enablePin1, OUTPUT); //=========== Active low =============
    digitalWrite(ledPin, HIGH); //========== Turn the LED on ==========
    digitalWrite(enablePin0, HIGH); //== Set high untill needed (it's active low) ==    
    digitalWrite(enablePin1, HIGH); //== Set high untill needed (it's active low) ==    
    
    ledPin_state = digitalRead(ledPin);   // Store initial LED state. HIGH when LED is on.
    keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
    
}

void loop()
    {
      char key = keypad.getKey(); //== Check the keypad for pressed keys ==
    }
    
// ======================================
// ======= Handle keypad events =========
// === This is clumsy and very sloppy ===
// ======= Needs ALOT of work!! =========
// ======================================

void keypadEvent(KeypadEvent key){
    switch (keypad.getState()){
           case PRESSED:
        if (key == '1') {
            numberOfCoils = 1;
            lcd.init();
            lcd.print("1 Coil");
        }

        if (key == '2') {
            numberOfCoils = 2;
            lcd.init();
            lcd.print("2 Coils");
        }

        if (key == '3') {
            numberOfCoils = 3;
            lcd.init();
            lcd.print("3 Coils");
        }

        if (key == '4') {
            numberOfCoils = 4;
            lcd.init();
            lcd.print("4 Coils");
        }

        if (key == '5') {
            numberOfCoils = 5;
            lcd.init();
            lcd.print("5 Coils");
        }

        if (key == '6') {
            numberOfCoils = 6;
            lcd.init();
            lcd.print("6 Coils");
        }

        if (key == '7') {
            numberOfCoils = 7;
            lcd.init();
            lcd.print("7 Coils");
        }

        if (key == '8') {
            numberOfCoils = 8;
            lcd.init();
            lcd.print("8 Coils");
        }

        if (key == '9') {
            numberOfCoils = 9;
            lcd.init();
            lcd.print("9 Coils");
        }
        break;

    case HOLD:
        if (key == '#') {
          totalNumberOfSteps = (numberOfCoils * stepsPerCoil);
          lcd.init();
          lcd.home();
          lcd.print("Total number of      "); 
          lcd.setCursor(0,1);
          lcd.print("steps is  ");
          lcd.print(totalNumberOfSteps);
          lcd.setCursor(0,2);
          lcd.print("Press * key to begin");
        }
        break;

            case RELEASED:
        if (key == '*') 
            {
                digitalWrite(enablePin0, LOW); // =======================================
                myStepper0.setSpeed(200); // ====== Set the speed to rpm needed: ========
                myStepper0.step(totalNumberOfSteps); // ===Get to steppin brother!! =====
                lcd.clear(); // ================== Clear the screen... ==================
            }
      }
}

I have a (some what) working program that lacks the initial question "Number of Coils".

You know how to output text to the LCD so why not print the message when you want to ?

Incidentally, you could replace

      if (key == '1') {
        numberOfCoils = 1;
        lcd.init();
        lcd.print("1 Coil");
      }

      if (key == '2') {
        numberOfCoils = 2;
        lcd.init();
        lcd.print("2 Coils");
      }
etc
etc

by setting numberOfCoils to key - '0' after checking that key is in the right range.

once you do this is set up

lcd.init(); //================ Initialize LCD ======

you no longer need to do it in the loop

so all the lines

lcd.init();

need to be removed

the main code you will use to control the lcd are the 3 listed below

lcd.clear(); //clear the screen this is a sledge hammer approach on a 20x4 display as it can cause screen flicker so only use it when you can not over write the data on the screen.

lcd.setCursor(0, 1);// set the place and the line you want the next text line to begin. If the text will not fit it will over flow to a different line on the 20x4. I think line zero over flows to line 3 then over flows back to line zero. So this says start at line 1 (2nd line down) at first cursor position

lcd.print("2 Coils");// basic print. once printed it will stay there until you either write data over the same blocks or until cleared

You know how to output text to the LCD so why not print the message when you want to ?

When I print in loop it prints over n over filling up the lcd. I don't know how to get it to print once and stay there until it gets user input. And then after completing task start over.

Incidentally, you could replace

by setting numberOfCoils to key - '0' after checking that key is in the right range.

I'm not sure I get it?

once you do this is set up

lcd.init(); //================ Initialize LCD ======

you no longer need to do it in the loop

so all the lines

lcd.init();

need to be removed

Thanks for the tip. And I'll keep tryin till I get it.

SubMicro:
I don't know how to get it to print once and stay there until it gets user input.

How about something like

bool haveToPrint = true;

  ...some..code...

  if (haveToPrint) {
   
     ...print...

    haveToPrint = false;
  }

I don't know how to get it to print once and stay there until it gets user input.

At least two ways to deal with that

  1. Position the LCD cursor with lcd.setCursor(column, row); before printing the message so taht it is always in the same place

  2. Once you have printed the message set a flag variable to true and do not print it again if the flag variable is true. If you need to print it again set the flag variable to false.

by setting numberOfCoils to key - '0' after checking that key is in the right range.

The key variable will hold a single character between '0' and '9'. The decimal ASCII value of key will be between 48 and 57 So, if you subtract the value of '0' (48) from any numeric value of key, say '7' (55) you will get the number of the key pressed so no need to check each of them individually.

The key variable will hold a single character between '0' and '9'. The decimal ASCII value of key will be between 48 and 57 So, if you subtract the value of '0' (48) from any numeric value of key, say '7' (55) you will get the number of the key pressed so no need to check each of them individually.

That brings me to my next question which was why is it not registering as the number pressed. in my next piece of code. Which works kind of!

the math for that has my head buzzing!

#include <Wire.h> //==========================
#include <Keypad.h> //===== Included Libs ====
#include <Stepper.h> //=======================
#include <LiquidCrystal_I2C.h> //=============

//======initialize Variables_=======
byte numberOfCoils = 0; //==========
long totalNumberOfSteps = 0; //=====
const int stepsPerCoil = 800; //====
const byte enablePin0 = 38; //======
const byte enablePin1 = 56; //======
byte coils; //======================

//=======_setup keypad library ============
const byte ROWS = 4; // ==four row ========
const byte COLS = 3; // ==three columns ===
char keys[ROWS][COLS] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'*','0','#'}
};

byte rowPins[ROWS] = {47, 37, 39, 43}; // ==== connect to the row pinouts of the keypad ====
byte colPins[COLS] = {45, 32, 41}; // ====== connect to the column pinouts of the keypad ===

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // ==========
LiquidCrystal_I2C lcd(0x27,20,4); // ========= initialize the liquid crystal library ==
Stepper myStepper0(stepsPerCoil, 54, 55); // ==== initialize the stepper library ======
Stepper myStepper1(stepsPerCoil, 60, 61); // ==== initialize the stepper library ======

byte ledPin = 13; 

boolean blink = false; //==Set initial state==
boolean ledPin_state; //======================

void setup(){
    lcd.init(); //================ Initialize LCD ======
    lcd.backlight(); //========= Turn on backlight =====
    lcd.setCursor(0, 1); //====== Cursor Position ======
    pinMode(ledPin, OUTPUT); //==== Sets the digital pin as output.====
    pinMode(enablePin0, OUTPUT); //=========== Active low =============
    pinMode(enablePin1, OUTPUT); //=========== Active low =============
    digitalWrite(ledPin, HIGH); //========== Turn the LED on ==========
    digitalWrite(enablePin0, HIGH); //== Set high untill needed (it's active low) ==    
    digitalWrite(enablePin1, HIGH); //== Set high untill needed (it's active low) ==    
    
    ledPin_state = digitalRead(ledPin);   // Store initial LED state. HIGH when LED is on.

    
}

void loop()
    {
        lcd.setCursor(0,0);
        lcd.print("Number of Coils?");
        lcd.setCursor(17,0);
      char key = keypad.getKey(); //== Check the keypad for pressed keys ==
      lcd.print(key);
      lcd.setCursor(0,2);
      totalNumberOfSteps = (key * stepsPerCoil);
      digitalWrite(enablePin0, LOW); // =======================================
      myStepper0.setSpeed(200); // ====== Set the speed to rpm needed: ========
      myStepper0.step(totalNumberOfSteps); // ===Get to steppin brother!! =====  
    }

  char key = keypad.getKey(); //== Check the keypad for pressed keys ==Does the getKey() function wait until a key is pressed ? If not then it will not return a number most of the time but your code acts as though it always does.

  totalNumberOfSteps = (key * stepsPerCoil);key is a char but here you are multiplying it by an int as if it were a number.
  totalNumberOfSteps = ( (key - '0') * stepsPerCoil);Would work if key is a single digit number held as a char.

Does the getKey() function wait until a key is pressed ? If not then it will not return a number most of the time but your code acts as though it always does.

How can I tell? So I will catch this in the future. And how can I correct this?

key is a char but here you are multiplying it by an int as if it were a number.

How can I convert it to int ?

totalNumberOfSteps = ( (key - '0') * stepsPerCoil);

Tried this, did not work even with single digits. Stepper just started on it's own and never quit. Program wasn't responding to input.

Thanks for your help

Ok still having problems getting it to work the way I want.

  1. Need to convert int char to int
  2. I would like for the user to have to press # key after entering number before the stepper starts
    i'm working on that, looking at keypad event thing again but haven't got it yet.

Here's what I got so far. I moved everything to functions.

/* @file MyAutoCoil
 || @version 6.3
 || @author Submicro
 || @contact submicro68@gmail.com
 ||
 || @description
 || | Asks for user input then
 || | Makes coils with stepper motors
 || | Still incomplete, needs alot of work.
 || | At least it WORKS!!
 || #
 */
 
#include <Wire.h>                           //==========================
#include <Keypad.h>                         //===== Included Libs ======
#include <Stepper.h>                        //==========================
#include <LiquidCrystal_I2C.h>              //==========================

// ======initialize Variables =======================
char numberOfCoils;                     // ======
long totalNumberOfSteps = 0;                // ======
const int stepsPerCoil = 800;               // ======
const byte enablePin0 = 38;                 // ======
// const byte enablePin1 = 56;              // ======
byte coils;                                 // ======

//=======_setup keypad library ===================================
const byte ROWS = 4;                        // ==four rows =======
const byte COLS = 3;                        // ==three columns ===
char keys[ROWS][COLS] = {                   // ===================
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'*','0','#'}
};

byte rowPins[ROWS] = {47, 37, 39, 43};          // ==== connect to the row pinouts of the keypad =====
byte colPins[COLS] = {45, 32, 41};              // === connect to the column pinouts of the keypad ===

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // === initialize keypad ===
LiquidCrystal_I2C lcd(0x27,20,4);               // ========= initialize the liquid crystal library ===
Stepper myStepper0(stepsPerCoil, 54, 55);       // ========= initialize the stepper library ==========
// Stepper myStepper1(stepsPerCoil, 60, 61);    // ========= initialize the stepper library ==========

byte ledPin = 13; 

void setup() {
    lcd.init();                             // ============== Initialize LCD ================
    lcd.backlight();                        // ============= Turn on backlight ==============
    lcd.setCursor(0, 1);                    // ============== Cursor Position ===============
    pinMode(ledPin, OUTPUT);                // ======= Sets the digital pin as output.=======
    pinMode(enablePin0, OUTPUT);            // ================ Active low ==================
//  pinMode(enablePin1, OUTPUT);            // ================ Active low ==================
    digitalWrite(ledPin, HIGH);             // ============= Turn the LED on ================
    digitalWrite(enablePin0, HIGH);         // == Set high untill needed (it's active low) ==    
//  digitalWrite(enablePin1, HIGH);         // == Set high untill needed (it's active low) == 
}

void loop() {
    userInput();
    moveStepper();
    }                                             

// ===================== Functions ===================== 

void userInput() {
    lcd.setCursor(0,0);
    lcd.print("Number of Coils?");
    lcd.setCursor(17,0);
    char key = keypad.getKey();               // == Check the keypad for pressed keys ==
    numberOfCoils = key;
    lcd.print(numberOfCoils);
    lcd.setCursor(0,2);
   }


void moveStepper() {
    totalNumberOfSteps = (numberOfCoils * stepsPerCoil); // calculate number of steps to move
    digitalWrite(enablePin0, LOW);            // ========================================
    myStepper0.setSpeed(200);                 // ===== Set the speed to rpm needed: =====
    myStepper0.step(totalNumberOfSteps);      // ======= Get to steppin brother!! =======
   }                                             //=========================================

In this code block:

    char key = keypad.getKey();               // == Check the keypad for pressed keys ==
    numberOfCoils = key;

If you touch the '1' key, the character that getkey() returns is the ASCII code for the character '1'. See:

So what actually gets assigned into the variable key is 49, the ASCII code for '1'. However, since it is an ASCII code, it's not the pure numeric value 1. To get the numeric value use:

int number = key - '0';

The ASCII code for zero is 48, so:

int number = 49 - 48;
int number = 1;

Now number is a real numeric value that can be manipulated mathematically.

Need to convert int char to int

You have been shown how to do this three times in this thread when the input is a single digit.

I would like for the user to have to press # key after entering number

Will the user enter a single digit number or a larger number ? The technique to deal with multiple digits is different to that for single digits.

How can I tell? So I will catch this in the future.

Call the function and print a message to serial immediatly afterwards. If the message does not print until you press a key then the function is blocking.

econjack

If you touch the '1' key, the character that getkey() returns is the ASCII code for the character '1'. See:

I understand this, the problem is converting to the proper int.

So what actually gets assigned into the variable key is 49, the ASCII code for '1'. However, since it is an ASCII code, it's not the pure numeric value 1. To get the numeric value use:

int number = key - '0';

I tried this again, "as UKHeliBob showed me this as well just slightly diff format" And it I still could not get it to work. I'm not sure what I'm doing wrong.

UKHeliBob

Call the function and print a message to serial immediatly afterwards. If the message does not print until you press a key then the function is blocking.

Seial.print worked

Will the user enter a single digit number or a larger number ? The technique to deal with multiple digits is different to that for single digits.

User will only be entering 1 thru 9, but I would like for it to be able to do double digits.

You have been shown how to do this three times in this thread when the input is a single digit.

And I told you I could not get it to work, and I still can't! It just starts the stepper and never stops!!

I understand this, the problem is converting to the proper int.

I don't think so. If you understood what was being done, you would also know that the technique does convert to a "proper int". The only reason that this:

int number = key - '0';

wouldn't work is if you think that is an "Oh" at the end of the statement instead of a zero character.

That technique will NOT work for multiple digits. In that case, read the digits into a char buffer, and after all digit characters have been entered, add a null character and then use atoi() on the buffer.

I don't think so. If you understood what was being done, you would also know that the technique does convert to a "proper int". The only reason that this:

int number = key - '0';

wouldn't work is if you think that is an "Oh" at the end of the statement instead of a zero character.

I'm not that stupid :wink:

I tried it

void userInput() {
    lcd.setCursor(0,0);
    lcd.print("Number of Coils?");
    lcd.setCursor(17,0);
    char key = keypad.getKey();               // == Check the keypad for pressed keys ==
    Serial.print(key);
    numberOfCoils = key;
    int numberOfCoils = key - '0';
    lcd.print(numberOfCoils);
    lcd.setCursor(0,2);
   }

And it still did not work. I have made progress here is the code as it is now, and is much closer to what I'm looking for.

#include <Wire.h>                           //==========================
#include <Keypad.h>                         //===== Included Libs ======
#include <Stepper.h>                        //==========================
#include <LiquidCrystal_I2C.h>              //==========================

// ======initialize Variables =======================
char numberOfCoils;                     // ======
long totalNumberOfSteps = 0;                // ======
const int stepsPerCoil = 800;               // ======
const byte enablePin0 = 38;                 // ======
// const byte enablePin1 = 56;              // ======
int coils;                                 // ======
byte ledPin = 13; 

// These constants won't change:
const int threshold = 0;   // an arbitrary threshold level that's in the range of the analog input

//=======_setup keypad library ===================================
const byte ROWS = 4;                        // ==four rows =======
const byte COLS = 3;                        // ==three columns ===
char keys[ROWS][COLS] = {                   // ===================
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'*','0','#'}
};

byte rowPins[ROWS] = {47, 37, 39, 43};          // ==== connect to the row pinouts of the keypad =====
byte colPins[COLS] = {45, 32, 41};              // === connect to the column pinouts of the keypad ===

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // === initialize keypad ===
LiquidCrystal_I2C lcd(0x27,20,4);               // ========= initialize the liquid crystal library ===
Stepper myStepper0(stepsPerCoil, 54, 55);       // ========= initialize the stepper library ==========

void setup() {
    lcd.init();                             // ============== Initialize LCD ================
    lcd.backlight();                        // ============= Turn on backlight ==============
    lcd.setCursor(0, 1);                    // ============== Cursor Position ===============
    pinMode(ledPin, OUTPUT);                // ======= Sets the digital pin as output.=======
    pinMode(enablePin0, OUTPUT);            // ================ Active low ==================
//  pinMode(enablePin1, OUTPUT);            // ================ Active low ==================
    digitalWrite(ledPin, HIGH);             // ============= Turn the LED on ================
    digitalWrite(enablePin0, HIGH);         // == Set high untill needed (it's active low) ==    
//  digitalWrite(enablePin1, HIGH);         // == Set high untill needed (it's active low) == 
    Serial.begin(115200);
}

void loop() {
    lcd.setCursor(2, 0);
    lcd.print("*** Welcome! ***");
    lcd.setCursor(4, 1);
    lcd.print("Please Enter");
    lcd.setCursor(2, 2);
    lcd.print("Number of Coils~~");
    char key = keypad.getKey();

  if (key > threshold) {
        userInput();
        moveStepper();
  }
  else {
    digitalWrite(ledPin, LOW);
  }
  
  
  delay(1);        // delay in between reads for stability
}
// ===================== Functions ===================== 

void userInput() {
    lcd.setCursor(0,3);
    lcd.print("Number of Coils? = ");
    lcd.setCursor(19,3);
    char key = keypad.getKey();               
    int coils = key - '0';
    int numberCoils = (coils - 1);
    int numberOfCoils = (coils - numberCoils);
    lcd.print(numberOfCoils);
    lcd.setCursor(0,2);
   }


void moveStepper() {
    totalNumberOfSteps = (numberOfCoils * stepsPerCoil); // calculate number of steps to move
    Serial.print(totalNumberOfSteps);
    digitalWrite(enablePin0, LOW);            // ========================================
    myStepper0.setSpeed(200);                 // ===== Set the speed to rpm needed: =====
    myStepper0.step(totalNumberOfSteps);      // ======= Get to steppin brother!! =======
   }                                             //=========================================

It still doesn't work. I used serial.print, and saw that for some reason the result is always 0 ?? But it shows the correct number on the LCD.

    numberOfCoils = key;
    int numberOfCoils = key - '0';

Why on earth are you trying to have TWO variables with the same name?

And I was wrong sorry. If Use both then even though it's using ascii the motors still start n stop.
If I remove

numberOfCoils = key;

then the motors don't work and all I get from Serial is 0 as long as I hold a key pressed.

And I posted the wrong code sorry

I see that you have no intention of posting the right code. Good luck, then.

Call the function and print a message to serial immediatly afterwards. If the message does not print until you press a key then the function is blocking.

Seial.print worked

In that case the function is non blocking so will return something that is not a keypress in the key variable when a key is not pressed. Even if the rest of your code worked you would, therefore, have a nonsense value for key most of the time. Way back at the start of this thread I said

setting numberOfCoils to key - '0' after checking that key is in the right range.

Note the section of that quote that I have underlined. Check that key >= '0' and <= '9' before assuming that it is a number.

I see that you have no intention of posting the right code. Good luck, then.

I was working on the code here is the code

#include <Wire.h>
#include <Keypad.h>
#include <Stepper.h>
#include <LiquidCrystal_I2C.h>

// ======initialize Variables =======================

char numberOfCoils;
long totalNumberOfSteps = 0;
const int stepsPerCoil = 800;
const byte enablePin0 = 38;
// const byte enablePin1 = 56;
int coils;

//=======_setup keypad library ======================

const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'*','0','#'}
};

byte rowPins[ROWS] = {47, 37, 39, 43};
byte colPins[COLS] = {45, 32, 41};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal_I2C lcd(0x27,20,4);
Stepper myStepper0(stepsPerCoil, 54, 55);
// Stepper myStepper1(stepsPerCoil, 60, 61);

byte ledPin = 13; 

void setup() {
    lcd.init();
    lcd.backlight();
    lcd.setCursor(0, 0);
    pinMode(ledPin, OUTPUT);
    pinMode(enablePin0, OUTPUT);
//  pinMode(enablePin1, OUTPUT);
    digitalWrite(ledPin, HIGH);
    digitalWrite(enablePin0, HIGH);    
//  digitalWrite(enablePin1, HIGH);
    Serial.begin(115200);

}

void loop() {
    lcd.setCursor(2, 0);
    lcd.print("*** Welcome! ***");
    lcd.setCursor(4, 1);
    lcd.print("Please Enter");
    lcd.setCursor(2, 2);
    lcd.print("Number of Coils~~");
    userInput();
    moveStepper();
    }                                             

// ===================== Functions ===================== 

void userInput() {
    lcd.setCursor(0,3);
    lcd.print("Number of Coils?");
    lcd.setCursor(17,3);
    char key = keypad.getKey();
    Serial.print(key);
 //   numberOfCoils = key;
    int numberOfCoils = key - '0';
    Serial.print(numberOfCoils);
    lcd.print(numberOfCoils);
    lcd.setCursor(0,3);
   }


void moveStepper() {
    totalNumberOfSteps = (numberOfCoils * stepsPerCoil);
    digitalWrite(enablePin0, LOW);
    myStepper0.setSpeed(200);
    myStepper0.step(totalNumberOfSteps);
   }

In that case the function is non blocking so will return something that is not a keypress in the key variable when a key is not pressed. Even if the rest of your code worked you would, therefore, have a nonsense value for key most of the time. Way back at the start of this thread I said

Sorry I'm just beginner at best at this. Some of the conepts take a bit before I get it.

Check that key >= '0' and <= '9' before assuming that it is a number.

How do I do that with if or switch statements?

Also is this the same as cast

int coils = key - '0';