Infrared interruption in an infinite loop

hello,
I want to make an infrared remote control meter.
with an arduino uno v3, a display, an infrared receiver and remote control

for the momentwhen I press button 1 the counter starts
but when I press button 2 the counter does not stop
When I press button 3 the counter does not reset to Zero

How could I arrange the infinite loop to handle button interrupts 2 and 3?
here is my code:

#include <TM1637Display.h>
#include <IRremote.h>
#define CLK 2
#define DIO 3
TM1637Display display(CLK, DIO);


int etatBouton,cp,run;
const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;

 
 
void setup(){
run=1; 
cp=0;
	display.setBrightness(0x0f);
	irrecv.enableIRIn();
	irrecv.blink13(true);
			}
 
 

void loop(){
if (irrecv.decode(&results)){    
	if (results.value == 0XFF38C7) {            // button1	start counter
		while(run==1) {
			cp++;
			display.showNumberDec(cp);
			delay(1000);
			if (results.value == 0XFFB04F){	 //  button2  stop counter
				run=0;
				delay(1000);
			}}
		
	}
	if (results.value == 0XFF6897) {            //button3 reset counter
	cp=0;
	}
}
}
//FF38C7    button1
//FFB04F    button2
//FF6897    button3

Thanks for your help

In the while loop you are never performing another irrecv.decode(&results) and therefore results.value never gets updated with the button2 code. Also you need to do an irrecv.resume() after a successful decode to get the next value.

What version of the library are you using ?

#include <IRremote.h>


Never use delay ( ) or blocking while( )
Well, unless you know why you shouldn’t :wink:

@LarryD makes a good point. I forgot about the update. 2.x vs 3.x

https://github.com/Arduino-IRremote/Arduino-IRremote#converting-your-2x-program-to-the-3x-version

Get rid of the infinite loop. Use a millis() timer to increment and display the counter. That leaves only a little to do for each button.

void loop()
{
  static unsigned long lastCountTime = 0;
  unsigned long currentTime = millis();

  if (currentTime - lastCountTime >= 1000)
  {
    lastCountTime += 1000;
    if (run == 1)
    {
      cp++;
      display.showNumberDec(cp);
    }
  }

  if (irrecv.decode(&results))
  {
    if (results.value == 0XFF38C7)        // button1  start counter
    {
      if (run == 0)
      {
        run = 1;
        lastCountTime = currentTime;
      }
    }
    else if (results.value == 0XFFB04F)  //  button2  stop counter
    {
      run = 0;
    }
    else if (results.value == 0XFF6897)  // button3 reset counter
    {
      cp = 0;
      display.showNumberDec(cp);
      lastCountTime = currentTime;
    }

    irrecv.resume();  // Edit: Thanks @ToddL1962 for the reminder.
  }
}
//FF38C7    button1
//FFB04F    button2
//FF6897    button3
1 Like

...and don't forget about the irrecv.resume() call;

thank you it works great

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.