How to exit a while loop with ir remote command?

Hello, I am a newbie to arduino. I made a project, where i run my motor in two directions using IR remote. The amount of motor movement is related to time and stop button at the end of the mechanism movement line.
As you can see my motor runs for 25 second, but I need to stop it with a button on remote. I cannot figure out how to stop it, since arduino is stuck in while loop for 25 sec, so it cannot look for any other code. Also I have tried putting aka if reading != xxxxxx, then do the loop. But that did not seems to work.
In general if i run one condition, I cannot revert the motor until 25 seconds pass, because arduino is on while loop, not seeing if conditions about IR remote.
PLease help me to stop the motor with a button and to be able to revert it while at movement.

#include <IRremote.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <DFRobot_DHT11.h>
DFRobot_DHT11 DHT;


IRrecv IR (3);

LiquidCrystal_I2C lcd(0x27, 20, 4);

#define EN1 5
#define EN2 6
#define opened_but 10
#define closed_but 11
#define DHT11_PIN 8
#define DHT11_PIN2 9

unsigned long currenttime;

void setup() {
  IR.enableIRIn();
  Serial.begin(9600);
  
  lcd.begin();
	lcd.backlight();
  
	

  pinMode(EN1, OUTPUT);
  pinMode(EN2, OUTPUT);
  pinMode(opened_but, INPUT_PULLUP);
  pinMode(closed_but, INPUT_PULLUP);

}

void loop() {
  DHT.read(DHT11_PIN);
  lcd.setCursor(0, 0);
  lcd.print("Inside  ");
  lcd.print(DHT.temperature);
  lcd.print("C  ");
  lcd.print(DHT.humidity);
  lcd.print("%");
  
  DHT.read(DHT11_PIN2);
  lcd.setCursor(0, 1);
  lcd.print("Outside ");
  lcd.print(DHT.temperature);
  lcd.print("C  ");
  lcd.print(DHT.humidity);
  lcd.print("%");
  
  



  if(IR.decode()){
    Serial.println(IR.decodedIRData.decodedRawData);
    if ( (IR.decodedIRData.decodedRawData == 4161273600) & (digitalRead(closed_but)==HIGH) )
     {
       lcd.setCursor(0, 2);
       lcd.print("CLOSING");

       currenttime = millis();
      
      while ( (millis() - currenttime < 25000) & (digitalRead(closed_but)==HIGH)  )
      {
       digitalWrite(EN1, HIGH);
       digitalWrite(EN2, LOW);
      
      }
       digitalWrite(EN1, LOW);
       digitalWrite(EN2, LOW);
      
      lcd.setCursor(0, 2);
      lcd.print("       ");
     }

     if ( (IR.decodedIRData.decodedRawData == 3927310080) & (digitalRead(opened_but)==HIGH) ) 
     {
       lcd.setCursor(0, 2);
       lcd.print("OPENING");


      currenttime = millis();
      while ( (millis() - currenttime < 25000) & (digitalRead(opened_but)==HIGH)  )
      {
       digitalWrite(EN1, LOW);
       digitalWrite(EN2, HIGH);
       
      }
      digitalWrite(EN1, LOW);
      digitalWrite(EN2, LOW);
       
      lcd.setCursor(0, 2);
      lcd.print("       ");

     }
    delay(500);
    IR.resume();
    
  }

}




Can you put a "listen for IR button" in this

for example (pseudocode):

while ( (millis() - currenttime < 25000) & (digitalRead(opened_but) == HIGH)  )
{
  digitalWrite(EN1, LOW);
  digitalWrite(EN2, HIGH);

  if (read_an_IRcode = 0xthiscode)
    waitForAnotherButton();
}

then

void waitForAnotherButton() {
  while (IRcode != 0xthatcode) { /* do nothing */}
}

Try with "&&" instead of "&". In all instances.

"&&" is the logical "AND" operator to be used in logical conditions
"&" is a bitwise operator that does something totally different... (look it up)

The IR Remote decoding doesn't happen in the background so you have to explicitly to do a decode whenever you need to know if a new message has arrived. Something like this:

#include <IRremote.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DFRobot_DHT11.h>
DFRobot_DHT11 DHT;


IRrecv IR(3);

LiquidCrystal_I2C lcd(0x27, 20, 4);

#define EN1 5
#define EN2 6
#define opened_but 10
#define closed_but 11
#define DHT11_PIN 8
#define DHT11_PIN2 9

unsigned long currenttime;

void setup()
{
  IR.enableIRIn();
  Serial.begin(9600);

  lcd.begin();
  lcd.backlight();



  pinMode(EN1, OUTPUT);
  pinMode(EN2, OUTPUT);
  pinMode(opened_but, INPUT_PULLUP);
  pinMode(closed_but, INPUT_PULLUP);
}

void loop()
{
  DHT.read(DHT11_PIN);
  lcd.setCursor(0, 0);
  lcd.print("Inside  ");
  lcd.print(DHT.temperature);
  lcd.print("C  ");
  lcd.print(DHT.humidity);
  lcd.print("%");

  DHT.read(DHT11_PIN2);
  lcd.setCursor(0, 1);
  lcd.print("Outside ");
  lcd.print(DHT.temperature);
  lcd.print("C  ");
  lcd.print(DHT.humidity);
  lcd.print("%");


  unsigned long latestButton = checkForIRButton();


  if ((latestButton == 4161273600) && (digitalRead(closed_but) == HIGH))
  {
    lcd.setCursor(0, 2);
    lcd.print("CLOSING");

    currenttime = millis();

    while ((millis() - currenttime < 25000) & (digitalRead(closed_but) == HIGH))
    {
      digitalWrite(EN1, HIGH);
      digitalWrite(EN2, LOW);
      latestButton = checkForIRButton();
      if (latestButton != 4161273600)
        break;
    }
    digitalWrite(EN1, LOW);
    digitalWrite(EN2, LOW);

    lcd.setCursor(0, 2);
    lcd.print("       ");
  }

  if ((latestButton == 3927310080) & (digitalRead(opened_but) == HIGH))
  {
    lcd.setCursor(0, 2);
    lcd.print("OPENING");

    currenttime = millis();
    while ((millis() - currenttime < 25000) && (digitalRead(opened_but) == HIGH))
    {
      digitalWrite(EN1, LOW);
      digitalWrite(EN2, HIGH);

      latestButton = checkForIRButton();
      if (latestButton != 3927310080)
        break;
    }
    digitalWrite(EN1, LOW);
    digitalWrite(EN2, LOW);

    lcd.setCursor(0, 2);
    lcd.print("       ");
  }
}

unsigned long checkForIRButton()
{
  if (IR.decode())
  {
    Serial.println(IR.decodedIRData.decodedRawData);
    IR.resume();
  }
  return IR.decodedIRData.decodedRawData;
}

Thank you, it even wroked out the box. The solution turned out to be very simple.
Also thanks to the others, who replied.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.