Arduino freezing after two commands

I wrote this sketch that basically waits for a command via Serial (just a single character) and, depending on the command, invokes one of several functions I created, each to handle a specific IR command. (e.g. Typing “p” will invoke the “sendPower” function, which will blink an IR LED and toggle the power for my TV.

For some reason, though, the Arduino freezes after only a couple of commands - I added a couple debugging messages and they stop appearing in the Serial Monitor after I enter “p” a couple of times. I’ve already had to refactor this sketch several times due to it somehow using up “too much memory” and now I’ve finally managed to get it to compile but now it just freezes and I am getting very frustrated ><

Here’s the code:

#include <IrSenderPwm.h>
#include <Pronto.h>

IrSignal* code = NULL;

void sendPower() {
  delete code;
  code = Pronto::parse("0000 006D 0012 0012 0008 0022 0008 0041 0008 001D 0008 006A 0008 0032 0008 0032 0008 002C 0008 0065 0008 020C 0008 0022 0008 001D 0008 001D 0008 001D 0008 001D 0008 006A 0008 001D 0008 001D 0008 0BEF 0008 0022 0008 0041 0008 001D 0008 006A 0008 0032 0008 0032 0008 002C 0008 0065 0008 020C 0008 0022 0008 0046 0008 0046 0008 001D 0008 001D 0008 006A 0008 001D 0008 001D 0008 0BEF");
  IrSenderPwm::getInstance(true)->sendIrSignal(*code);
  delete code;
}

void sendGuide() {
  delete code;
  code = Pronto::parse("0000 006D 0012 0012 0008 0022 0008 0041 0008 001D 0008 006A 0008 0032 0008 0032 0008 002C 0008 0065 0008 020C 0008 0022 0008 001D 0008 001D 0008 001D 0008 001D 0008 006A 0008 001D 0008 001D 0008 0BEF 0008 0022 0008 0041 0008 001D 0008 006A 0008 0032 0008 0032 0008 002C 0008 0065 0008 020C 0008 0022 0008 0046 0008 0046 0008 001D 0008 001D 0008 006A 0008 001D 0008 001D 0008 0BEF");
  IrSenderPwm::getInstance(true)->sendIrSignal(*code);
  delete code;
}

<--- SNIP --->

void setup() {
  Serial.begin(9600);
  Serial.println("### Send IR Signal Code ###");
}

void loop() {
  while (Serial.available() > 0) {
    char ch = Serial.read();
    
    switch(ch) {
     
      
      case 'p': // POWER
        Serial.println("Toggling Power");
        sendPower();
        break;

      case 'g': // GUIDE
        Serial.println("Opening Guide");
        sendGuide();
        break;

    }
  }
}

I feel like this shouldn’t be so complicated - Any ideas what’s going on here? Much appreciated

I feel like this shouldn't be so complicated

I really can't disagree.

IrSignal* code = NULL;
void sendPower() {
  delete code;
  code = Pronto::parse("0000 006D 0012 0012 0008 0022 0008 0041 0008 001D 0008 006A 0008 0032 0008 0032 0008 002C 0008 0065 0008 020C 0008 0022 0008 001D 0008 001D 0008 001D 0008 001D 0008 006A 0008 001D 0008 001D 0008 0BEF 0008 0022 0008 0041 0008 001D 0008 006A 0008 0032 0008 0032 0008 002C 0008 0065 0008 020C 0008 0022 0008 0046 0008 0046 0008 001D 0008 001D 0008 006A 0008 001D 0008 001D 0008 0BEF");
  IrSenderPwm::getInstance(true)->sendIrSignal(*code);
  delete code;
}

I don’t think deleting pointers twice is a good idea.

Where does this RAM wasting parse stuff come from?

I don't think deleting pointers twice is a good idea.

Good call, I assumed it wouldn't hurt to clear the pointer before/after, but clearly I was wrong on that, because I commented out the "delete code" at the end of the function and the issue now seems to be fixed - Thank you SO MUCH!

Where does this RAM wasting parse stuff come from?

Not sure which you're referring to, you mean the Pronto hex strings? I got 'em off http://irdb.tk/

applejuicerules: I wrote this sketch that basically waits for a command via Serial (just a single character) and, depending on the command, invokes one of several functions I created, each to handle a specific IR command. (e.g. Typing "p" will invoke the "sendPower" function, which will blink an IR LED and toggle the power for my TV.

For some reason, though, the Arduino freezes after only a couple of commands I feel like this shouldn't be so complicated - Any ideas what's going on here? Much appreciated

Usually, when an Arduino crashes after a few operations, it's usually a power supply glitch problem.

How are you driving your IR LED? And, how much current does it draw when it pulses?

If you are running it off the Arduino 5 volt rail, that may be pulling enough current to spike the rail low for a few microseconds and crashing the CPU.

Try placing a large (at LEAST 100 uF if not 1000 uF, in parallel with a 0.1uF ceramic) capacitor across the IR LED supply to bypass the spikes. If you have an oscilloscope, look at the 5 volt rail on the Arduino and trigger the IR. You should see some teeny tiny glitches that are maybe 100 millivolts or so, but any larger downward spikes and that's your problem.

Hope this helps.

(edit to add): Here's code for an IR Remote that uses an ATTiny85 processor and sends Pronto-type IR codes stored in either EEPROM or PROGMEM. It's a very simple program and you may be able to adapt it for your own use.

IR REMOTE

On your first part the premature delete does not hurt because the pointer is still NULL. It's a different story after the first execution of the function.

On the website you linked I could not locate any IrSenderPwm.h Pronto.h.

On the website you linked I could not locate any IrSenderPwm.h Pronto.h.

Oh, sorry, the Pronto thing is here: http://www.harctoolbox.org/infrared-apidocs/classPronto.html