Direction change problem with stepper motor


I have a little problem with controlling stepper motor (NEMA 17).

I use: Arduino MEGA2560, A4988 controller, 5X4 matrix keypad.

The project contains other tasks I just talk about the stepper section. The task is following: If I push button 'A' the stepper starts revolve CW direction, if I push button 'S' the stepper stops revolve, if I push button 'B' the stepper starts revolve CCW direction. So... If I push the dedicated buttons on keypad the stepper should start revolves CW or CCW or stop. In the sketch I use 'for loop' (I found this on internet. I try it with do-while, but the issue is the same). so far it works properly. when I push one of trigger button (‘A’ or ‘B’) the motor starts revolve, but... my big problem that is I can not stop the stepper before the loop end. I can't intervene to loop (Arduino not receive any inputs from keypad) while it is running. I know there exist interrupt methods for every microcontroller, but I want to use the matrix keypad for this process and not an external button. Could someone advise? Can I somehow intervene in 'for loop' via matrix keypad?

I have tested reading a chrystall boll in other environments, and it worked, but to pick up Your code that way…. No, I can't. Your test running of the steppe,r CW, CCW and STOP tells that the circuitryand stepper is in god order. Left is the software...…, not attached. Use code tags and attach Your code!

AldoGMBH: my big problem that is I can not stop the stepper before the loop end

My problem is that you have not posted the program you need advice for.

...R Stepper Motor Basics Simple Stepper Code

You also have no chrystal ball Robin?

Hi Aldo,

These guys are funny, huh? What they are saying is POST YOUR CODE!

And be a good guy and use the proper tage, which you know all about because you read How to use this forum ...

Here is the code

#include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(48, 49, 50, 51, 52, 53);
//RS(D48), E(D49), D4(D50), D5(D51), D6(D52), D7(D53)

char key;
String charChain;
String charChainPreSt;

const int stepPin = 3;
const int dirPin = 4;
const int MS1 = 5;
const int MS2 = 6;
const int MS3 = 7;
const int Enable = 8;

String lcdPreState;           
float WO;                  
float WO2;                 

const byte ROWS = 5; //five rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {

byte rowPins[ROWS] = {30, 29, 28, 27, 26};
byte colPins[COLS] = {25, 24, 23, 22};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); 

void setup() {
  lcd.begin(16, 2);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  digitalWrite(MS1, HIGH);
  digitalWrite(MS2, HIGH);
  digitalWrite(MS3, HIGH);

void loop() {
   key = keypad.getKey();
  if (key != NO_KEY){
    charChain = charChain + key;
    while (Serial.available()) {
      charChain = charChain+(char(;
    lcd.print("text: " + charChain);

   WO = charChain.toFloat();
   WO2 = charChain.toFloat();

   if(key == '#'){
    charChainPreSt = charChain;
    charChain = "";

   if(key == 'B'){

   if(key == 'S'){


void StepMotCW(){

digitalWrite(dirPin, LOW);     //HIGH=CCW, LOW=CW
   digitalWrite(stepPin, HIGH);        
   digitalWrite(stepPin, LOW);
    while(key == 'F');

void StepMotCCW(){
  digitalWrite(dirPin, HIGH);     //HIGH=CCW, LOW=CW
   digitalWrite(stepPin, HIGH);        
   digitalWrite(stepPin, LOW);
    while(key == 'B');

void StepMotStop(){
  if(key == 'S'){
   digitalWrite(Enable, HIGH); 

StepMotStop is called if key =="S". Why test key again inside StepMotStop? Probably unimportant. As long as a StepMotCC, or StepMotCCW is called and running the processor is stuck in that Do While loop and no commands will be read from keybord and not from Serial.

Yes you’re right this was a mistake I duplicate that step. So, is there no way to intervene in loops with keypad if it is running? Probably I have to create an external interrupt button.

You need to change the approach to the questiion of handling both stepper moves and keyboard sencing. Involving real time programming using interrupts is not as easy as it sounds. Keyboard and Serial.available sends data like bombs from above and it is not easy to handle.

I don't see which commands You receive from Serial and which You receive from kbd.

Reading a keyboard is very different from reading Serial. I don't know the keypad.getKey(); Does it wait until a key is pressed?

Don't plan on doing something WHILE a key is pressed. Just do one step it IF the key is pressed. That way the Arduino can immediately check for other things.