Go Down

Topic: remote control and delay problem (Read 605 times) previous topic - next topic

brraaq

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

Code: [Select]

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


Arrch

BlinkWithoutDelay example. Learn it. Live it. Love it.

CrossRoads

Rewrite it following blink without delay methodology.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


Arrch


I tried and I didn't succeed


That's too bad. We could probably help if we saw what you tried.

LarryD

Try again and ask for us for help.
The way you have it in your schematic isn't the same as how you have it wired up!

brraaq

Code: [Select]

#include <IRremote.h>
int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
int buttonState = 0;
int lastpressed = 0;
int i = 1;
long time2 = 120000;   
long time4 = 240000;   
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);
   unsigned long time2 = millis();
   digitalWrite(13, LOW);
   unsigned long time4 = millis();
} else {

   digitalWrite(13, LOW);

     }

delay(10);
}



Arrch

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?

brraaq

now it's work but with same time for on and off
thank you and check the code
Code: [Select]

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



Arrch

So when you turn it on, set interval to whatever the off interval is. When you turn it off, set interval to whatever the on interval is.

Go Up