IR receiver reliability

Hi All,
I’ve built a robot that has two arms and a head controlled by stepper motors. I’ve got a function (called ‘Stealth mode’ in my code) that stops all movement with the robot
until a button is pressed on an IR remote (or the for loop times out), taking it back to the main loop.

Now this sometimes works… and sometimes doesn’t, which obviously makes debugging a challenge. Further added to the challenge is the fact that this function seems to work perfectly when simulated in TinkerCad. The function is below, and the code for the whole program is attached.

void stealth () {
  lightsoff();
  delay (100);
  Serial.println("Stealth Mode Started");
  for (long d = 0; d < stealthpausetime; d++) {
    Serial.println(d);
    if (irrecv.decode(&results)) // have we received an IR signal?
    {
      Serial.println(results.value, HEX);
      if (results.value == 0xFF38C7) {
        Serial.println("OK Button Pressed!");
        irrecv.resume();
        break;
      }
      irrecv.resume();
    }
  }
  Serial.println("Stealth Mode Time Over");
  irrecv.enableIRIn();
  delay (200);
}

When I press the button and nothing happens (i.e. the specific button press of Hex 0xFF38C7 hasn’t been registered) I’ve seen in the serial monitor that different codes are showing that don’t correspond to the button that has been pressed, or are a jumbled version of it, with letters missing or others added e.g. ‘FF38C’ and ‘488F3CBB’. Has anyone got any insight into why this might be happening beyond cheap components?

Any advice would be really useful.
Many thanks,
Paul

Robot code v37.txt (22.7 KB)

Your testing, faultfinding, debugging is well done.
However, due to the current problem, I suggest that You add even more printouts telling things like "entertering xx state", "leaving…." etc..
Another try I can think of is a hard test of the IR link.

That sketch is based on for/while/delay with no touch of cooperative multitasking.

Very hard not to miss IR commands with that 'structure', even harder to run a decent robot.

There are modules that handle the reception of IR and pass only the codes per serial,
maybe with such a module your robot would loose less commands.

Your blocking approach will not work well in any case.

Thanks both, much appreciated. I just had another thought, my IR receiver is attached to pin 17, which isn't one of the PWM pins. Could this be having an effect? Sorry, this feels in the ball-park of a stupid thing to ask...

The transmitter likes to use a hardware controlled pin.

The receiver works better on a pin with interrupt capabilities,
17 should have a pinchange interrupt IIRC and if it basically works, it seems so.

Thanks Whandall. I've just found another thread in which other people report the same issue, so time to start working through the various suggestions...

Just for anyone who stumbles across this in the future...

My work-around in the end was just to include some of the other codes that the IR receiver was reporting for the same button press in the IF statement - so if any of the various codes was received, it would also activate. Not the most elegant, but it works 90% of the time.