IR Remote - Stop motor in progress

Hi all,

I would like to control a stepper motor with a IR remote.
Is working fine, if I push on up the motor forward and on down the motor reverse until my hall sensor.
When my hall sensor detect the motor, the while is stop.
But I would like to stop my motor when I press on the OK button too.
This is my code :

#include <IRremote.h>

int ENA=9; //Connecté sur votre Arduino, Pin 2
int IN1=8; //Connecté sur votre Arduino, Pin 3
int IN2=7; //Connecté sur votre Arduino, Pin 4

int ENB=3; //Connecté sur votre Arduino, Pin 5
int IN3=5; //Connecté sur votre Arduino, Pin 6
int IN4=4; //Connecté sur votre Arduino, Pin 7

const int irReceiver = 6;
const int switchDownPin=2;
const int switchUpPin=12;
const int hallSensorUpPin=10;
const int hallSensorDownPin=11;

int switchDownState=0;
int switchUpState=0;
int hallSensorUpState=0;
int hallSensorDownState=0;

unsigned long key_value = 0;

IRrecv irrecv(irReceiver);
decode_results results;

void setup() {
 pinMode(ENA,OUTPUT);
 pinMode(ENB,OUTPUT);
 pinMode(IN1,OUTPUT); 
 pinMode(IN2,OUTPUT);
 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT);
 pinMode(switchDownPin, INPUT);
 pinMode(switchUpPin, INPUT);
 pinMode(hallSensorUpPin, INPUT);
 pinMode(hallSensorDownPin, INPUT);
 
 irrecv.enableIRIn();
 irrecv.blink13(true);
 
 Serial.begin(9600);
}


void loop(){
  hallSensorUpState = digitalRead(hallSensorUpPin);
  hallSensorDownState = digitalRead(hallSensorDownPin);

  if(hallSensorUpState == LOW || hallSensorDownState == LOW){
   Serial.print("LOW");
  }

  if (irrecv.decode(&results)){
    Serial.println(results.value, HEX);
    
    if (results.value == 0XFFFFFFFF){
      results.value = key_value;
    }
    
    switch(results.value){
      case 0xFF02FD: // OK
        Serial.println('OK');
      break;
      case 0xFF629D: // UP
        Serial.println('UP');
        up();
      break;
      case 0xFFA857: // DOWN
        Serial.println('DOWN');
        down();
      break;     
    }
    key_value = results.value;
    irrecv.resume(); 
      
  }

void up(){
  int state = digitalRead(hallSensorUpPin);

  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
  
  while(digitalRead(hallSensorUpPin) != LOW){
  	Serial.print(digitalRead(hallSensorUpPin));


    digitalWrite(IN1, 0);
    digitalWrite(IN2, 1);
    digitalWrite(IN3, 0);
    digitalWrite(IN4, 1);
    delay(2);
  
    digitalWrite(IN1, 0);
    digitalWrite(IN2, 1);
    digitalWrite(IN3, 1);
    digitalWrite(IN4, 0);
    delay(2);
      
    digitalWrite(IN1, 1);
    digitalWrite(IN2, 0);
    digitalWrite(IN3, 1);
    digitalWrite(IN4, 0);
    delay(2);
  
    digitalWrite(IN1, 1);
    digitalWrite(IN2, 0);
    digitalWrite(IN3, 0);
    digitalWrite(IN4, 1);
    delay(2); 

    if(results.value == 0xFF02FD){
    	break;
    }
  }

 digitalWrite(ENA, LOW);
 digitalWrite(ENB, LOW);
}

void down() {
  int state = digitalRead(hallSensorDownPin);

  // Set both motors ON
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);

  while(digitalRead(hallSensorDownPin) != LOW){
    digitalWrite(IN1, 0);
    digitalWrite(IN2, 1);
    digitalWrite(IN3, 0);
    digitalWrite(IN4, 1);
    delay(2);

    digitalWrite(IN1, 1);
    digitalWrite(IN2, 0);
    digitalWrite(IN3, 0);
    digitalWrite(IN4, 1);
    delay(2);

    digitalWrite(IN1, 1);
    digitalWrite(IN2, 0);
    digitalWrite(IN3, 1);
    digitalWrite(IN4, 0);
    delay(2);

    digitalWrite(IN1, 0);
    digitalWrite(IN2, 1);
    digitalWrite(IN3, 1);
    digitalWrite(IN4, 0);
    delay(2);
  }

  // set both motors OFF
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);

}  // end forward()

I dont know how to read my IR remote during my while, like the hall sensor.

Thank you for your help

You need non blocking code, read about “state machines” - there are plenty of tutorials on line, here is one of the highest hits or this one when googling for “arduino state machines tutorial”. You’ll find also videos if you are more of a visual learner than a reader

Non-blocking code and a state machine are two different things which are often used together.

if you want a responsive program then you need to check the IR input between each step and you should not use a blocking WHILE loop. In most cases WHILE can be replaced with IF and then allow loop() to take care of the repetition.

Have a look at how the code is organized in a non-blocking way in Several Things at a Time and in Planning and Implementing a Program

...R

Stepper Motor Basics
Simple Stepper Code

Thank you guys for your different way !