I want to run for 2 minutes and shuts off for 4 minutes I add function to stop by remote control but the problem is that the remote command is not received until the end of the delay
help in modifying the code
#include <IRremote.h>
int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
int buttonState = 0;
int lastpressed = 0;
int i = 1;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(13, OUTPUT);
}
void loop() {
unsigned long codeValue;
if (irrecv.decode(&results) ) {
Serial.println (results.value, DEC) ;
codeValue = results.value;
irrecv.resume();
}
if (codeValue == 83579055){
i = 1;
}
if (codeValue == 83570895){
i = 0;
}
if (i == 1){
digitalWrite(13, HIGH);
delay(120000);
digitalWrite(13, LOW);
delay(240000);
} else {
digitalWrite(13, LOW);
}
delay(10);
}
All you are doing is recording the time, you're not doing anything with it. Notice how in the BlinkWithoutDelay example, there is an if statement involving the previous recorded time?
now it's work but with same time for on and off
thank you and check the code
#include <IRremote.h>
int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
int buttonState = 0;
int lastpressed = 0;
int i = 1;
long previousMillis = 0;
long interval = 120000;
int ledState = HIGH;
const int ledPin = 13;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(13, OUTPUT);
}
void loop() {
unsigned long codeValue;
if (irrecv.decode(&results) ) {
Serial.println (results.value, DEC) ;
codeValue = results.value;
irrecv.resume();
}
if (codeValue == 83579055){
i = 1;
}
if (codeValue == 83570895){
i = 0;
}
if ( i == 1 ) {
digitalWrite(ledPin, ledState);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
} else {
digitalWrite(ledPin, LOW);
}
delay(10);
}