I've never worked with IR remotes myself, so I'm not that familiar with what the code
should do, but I can clarify where your problem exists.
Your listenForIR function has two loops, each attempting to measure the duration of a series of high and low pulses.
Let's take a look at the loop for low pulses, since that's likely where it's getting blocked:
while (! (IRpin_PIN & _BV(IRpin))) {
// pin is still LOW
lowpulse++;
delayMicroseconds(RESOLUTION);
// KGO: Added check for end of receive buffer
if (((lowpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) {
return currentpulse;
}
}
This while loop will run for as long as
!(IRpin_PIN & _BV(IRpin))
is true. If you are not issuing a command from your remote, then this is always going to be true. The signal is always going to be low.
Now inside your loop you do have an if() statement that will return from the function under the right conditions, but the problem is, if you are not issuing a command from your remote, those conditions will never be met. The function will return IF one of two conditions are met. The last,
currentpulse == NUMPULSES
obviously isn't going to be met if there's no command.
The first
(lowpulse >= MAXPULSE) && (currentpulse != 0)
also isn't going to be met because currentpulse will always be 0.
It's possible that all you need to do is remove
&& (currentpulse != 0)
So that your conditional is just this:
if (lowpulse >= MAXPULSE || currentpulse == NUMPULSES)
That would need to be made in both your highpulse and lowpulse loops. Your code does not include the definitions of Play, Speedup, Slowdown, Stop, so it's not clear what affect that would have on your IRcompare calls, as you would be passing in a value of 0 for numpulses.