Pages: [1]   Go Down
Author Topic: remote control and delay problem  (Read 495 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 525
Posts: 26551
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Rewrite it following blink without delay methodology.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried and I didn't succeed
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried and I didn't succeed

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

Offline Offline
Edison Member
*
Karma: 64
Posts: 2489
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try again and ask for us for help.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

led blink very fast 0.1 sec
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

now it's work but with same time for on and off
thank you and check the code
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);
}


Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: