LED not lighting UP in conjunction with a button and IR remote

Hello people.

I have ran in to a brick wall with this issue. First off – the code

main

void loop() 
{
  //-----PART 1-----
  if(house.button1Read() || house.button2Read()) digitalWrite(LED, HIGH);
  if(house.button3Read() || house.button4Read()) digitalWrite(LED, LOW);

  // ----PART 2-----
  int numberpulses = house.listenForIR();
  
  if(house.IRcompare(numberpulses, ONOFFsignal))
    digitalWrite(LED, HIGH);

  if(house.IRcompare(numberpulses, MUTEsignal))
    digitalWrite(LED, LOW);
}

ButtonXRead() function (using Bounce2 library, same for other 3 buttons)

bool automatedHouse::button1Read()
{
	button1Bounce.update();
	return button1Bounce.read();
}

listenForIR() function

#define IRpin_PIN PIND
#define IRpin 2
#define MAXPULSE 10000 
#define RESOLUTION 20
#define FUZZINESS 20
int automatedHouse::listenForIR()
{
	currentpulse = 0;

	while (1) 
	{
		uint16_t highpulse, lowpulse;  
		highpulse = lowpulse = 0; 
		while (IRpin_PIN & (1 << IRpin)) 
		{
			highpulse++;
			delayMicroseconds(RESOLUTION);
			if ((highpulse >= MAXPULSE) && (currentpulse != 0))
				return currentpulse;
		}
		pulses[currentpulse][0] = highpulse;
		while (! (IRpin_PIN & _BV(IRpin))) 
		{
			lowpulse++;
			delayMicroseconds(RESOLUTION);
			if ((lowpulse >= MAXPULSE)  && (currentpulse != 0))
				return currentpulse;
		}
		pulses[currentpulse][1] = lowpulse;
		currentpulse++;
	}	
}

IRcompare() function

bool automatedHouse::IRcompare(int numpulses, int Signal[])
{
	for (int i=0; i< numpulses-1; i++) 
	{
		int oncode = pulses[i][1] * RESOLUTION / 10;
		int offcode = pulses[i+1][0] * RESOLUTION / 10;
		if ( (abs(oncode - Signal[i*2 + 0]) > (Signal[i*2 + 0] * FUZZINESS / 100)) || ( abs(offcode - Signal[i*2 + 1]) > (Signal[i*2 + 1] * FUZZINESS / 100)))
			return false;
	}
	return true;
}

PROBLEM:
-Scenario 1: Using PART 1 of the main code - All 4 buttons work, LED turns on/off.
-Scenario 2: Using PART 2 of the main code - remote works, LED turns on/off.
-Scenario 3: Using all of the code - remote works, LED turn on/off; All 4 buttons don’t work, LED no reaction.

I think the problem is in the listenForIR function, but I can’t find the solution. So why don’t the buttons work if the listenForIR function is running?

Any help would be greatly appreciated!

I think the problem is in the listenForIR function, but I can't find the solution. So why don't the buttons work if the listenForIR function is running?

Because while you are listening for IR data, you are NOT reading the switches.

Why are you not using Ken Shirriff's IR library, based on interrupts?

I should. :slight_smile:

Thank you.