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