Arduino IRremote

Hi,

When I click button1, it starts switching my led on and off.

As soon as I click button2, I want the led ON.

But it waits till the completion of case 1.

Is there any way to do it?

#include <IRremote.h>

int recv_pin = 7;
int send_pin = 8;

define button1 0xFFA25D

define button2 0xFF629D

IRrecv irrecv(recv_pin);

decode_results results;

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(send_pin, OUTPUT);
}

void loop() {
if (irrecv.decode(&results)) {
switch (results.value) {
case button1:
digitalWrite(send_pin, HIGH);
delay(1000);
digitalWrite(send_pin, LOW);
delay(500);
digitalWrite(send_pin, HIGH);
delay(800);
digitalWrite(send_pin, LOW);
delay(400);
digitalWrite(send_pin, HIGH);
delay(1200);
digitalWrite(send_pin, LOW);
delay(500);
digitalWrite(send_pin, HIGH);
delay(900);
digitalWrite(send_pin, LOW);
delay(700);
break;
case button2:
digitalWrite(send_pin, HIGH);
break;
}
irrecv.resume();
}
}

Yes. Use millis() instead of delay(). See the Blink without Delay example that ships with the IDE to see how that works.

umutk:
Is there any way to do it?

Not with delay() for timing.

Look at the BlinkWithoutDelay example. You can use an array of time intervals and an index in the array to keep track of how far through the sequence you have gotten. Because you won't be using delay() the loop() will repeat very quickly, giving you an opportunity to check for input even while the pattern is running.

Thanks for your reply but using millis() is also not working.

When I push button1, LED turns on.

But when push button2, LED does not turn off because I set while toop to run for 10 seconds.

I want to terminate while loop as soon as I push button2.

#include <IRremote.h>

define button1 0xFFA25D

define button2 0xFF629D

const int recv_pin = 7;
const int send_pin = 8;
unsigned long starttime = 0;
unsigned long difference = 0;

IRrecv irrecv(recv_pin);
decode_results results;

void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(send_pin, OUTPUT);
digitalWrite(send_pin, LOW);
}

void loop() {
if (irrecv.decode(&results)) {
switch (results.value) {
case button1:
funct();
break;
case button2:
digitalWrite(send_pin, LOW);
break;
}
irrecv.resume();
}
}

void funct() {
starttime = millis();
while (millis() - starttime < 10000) {
digitalWrite(send_pin, HIGH);
}
}

  while (millis() - starttime < 10000) {
      digitalWrite(send_pin, HIGH);
  }

That is basically the same thing as:

    digitalWrite(send_pin, HIGH);
    delay(10000);

You can’t spend ten seconds in a loop and expect the program to be responsive.

This is the sort of thing I mean:

const byte SendPin = 8;

const unsigned long PatternIntervals[] = {  // Milliseconds
  1000, 500,
  800, 400,
  1200, 500,
  900, 700
};

const int STEP_COUNT = sizeof PatternIntervals / sizeof PatternIntervals[0];
int PatternStep = -1;
int StepStartTime = 0;

void setup() {
  Serial.begin(9600);
  
  digitalWrite(SendPin, LOW);  // Start with LED off
  pinMode(SendPin, OUTPUT);
}

void StartPattern() {
  PatternStep = 0;
  StepStartTime = millis();
  digitalWrite(SendPin, HIGH);
}

void StopPattern() {
  PatternStep = -1;
  digitalWrite(SendPin, LOW);
}

void loop() {
  unsigned long currentMillis = millis();

  // If the pattern is running, check to see if it's time to step
  if (PatternStep >= 0) {
    // Check the current step
    if ((currentMillis - StepStartTime) >= PatternIntervals[PatternStep]) {
      StepStartTime += PatternIntervals[PatternStep];
      digitalWrite(SendPin, !digitalRead(SendPin)); // Toggle the LED
      PatternStep++;
      if (PatternStep >= STEP_COUNT)
        StopPattern();  // Run through only once
    }
  }

  // Start the pattern whenever a character arrives from Serial Monitor
  if (Serial.read() != -1) 
      StartPattern();
}

Thank you.