Arduino UNO hang/integer array

Hello,

I've run into a little problem, which I hope somebody might be able to explain. I'm using an UNO Rev3 to send some RAW iR commands to a Samsung TV by means of the IRremote library. (Note: if anybody has a library that can send Samsung commands 'natively', I'll gladly revise the entire sketch!).

At the moment, I have the Channel 0 code configured as an integer array:

unsigned int iCode[68] = {
        4650,4300,700,1550,700,1500,700,1550,700,400,700,400,700,400,700,450,650,450,650,1550,700,1550,650,
        1550,700,400,700,400,700,400,700,450,700,400,700,1550,650,400,700,450,700,400,650,1550,700,400,700,
        450,700,400,700,400,700,1500,700,1550,700,1500,700,400,700,1550,650,1550,700,1500,700};
      irSend.sendRaw(iCode, 68, iFreq);
      Serial.println("Executed TV CHAN 0 command.");

I also have the power on, power off, and few other codes defined. This works fine, but when I add the remaining numbers, the UNO hangs at boot, or at the latest when I try to execute any command. The other numbers are also defined in the same way. I won't bore you with the entire sketch, but here's an example:

   if (striRCode == "tv000") {
      unsigned int iCode[68] = {
        4650,4300,700,1550,700,1500,700,1550,700,400,700,400,700,400,700,450,650,450,650,1550,700,1550,650,
        1550,700,400,700,400,700,400,700,450,700,400,700,1550,650,400,700,450,700,400,650,1550,700,400,700,
        450,700,400,700,400,700,1500,700,1550,700,1500,700,400,700,1550,650,1550,700,1500,700};
      irSend.sendRaw(iCode, 68, iFreq);
      Serial.println("Executed TV CHAN 0 command.");
    }
    if (striRCode == "tv111") {
      unsigned int iCode[68] = {
        4500,4450,600,1600,600,1600,600,1650,600,500,600,500,600,500,650,500,600,500,600,1600,600,1650,600,
        1600,600,500,600,550,600,500,600,500,600,500,600,500,650,500,600,1600,600,500,600,550,600,500,600,
        500,600,500,600,1650,600,1600,600,500,600,1650,600,1600,600,1650,600,1600,600,1600,600};
      irSend.sendRaw(iCode, 68, iFreq);
      Serial.println("Executed TV CHAN 1 command.");
    }
    if (striRCode == "tv222") {
      unsigned int iCode[68] = {
        4550,4400,600,1600,600,1650,600,1600,600,500,600,550,600,500,600,500,600,500,600,1650,600,1600,600,
        1650,600,500,600,500,600,500,600,550,600,500,600,1600,600,500,600,1650,600,500,600,500,600,550,600,
        500,600,500,600,500,600,1650,600,500,600,1600,600,1650,600,1600,600,1650,600,1600,600};
      irSend.sendRaw(iCode, 68, iFreq);
      Serial.println("Executed TV CHAN 2 command.");
    }

The 'striRCode' variable is taken straight from the serial input (me typing). What I don't understand is why the mere presence of the other arrays makes it unstable. After all, it only receives one command at a time, and if it didn't flush it's memory after performing a command, I would still expect the first one to work (even if I had to hit the reset button afterwards). If it was trying to put all the arrays in memory as soon as one command was executed, it would crash with only two commands defined, although I can cram in about 4 before it goes wrong. Additionally, that would also cause a compiler error as I use the name 'iCode' for every single array and that would cause conflicts.
The code above is in a separate function, not loop(). The loop() only grabs the serial input and hands it to the function containing all the integer arrays, etc.
If I only put in the codes for let's say channels 0 through 4, it also works - so the contents of the array aren't the problem either. Is there some black magic going on in the IDE that I'm not aware of?

Any help would be appreciated.

Kind regards,

PelliX

You are probably running out of RAM. Those arrays are being created on the stack.

Please, post code, not snippets.

And what KeithRB said.

I won't bore you with the entire sketch

Please feel free to bore us.

This problem has the look and feel of a (lack of) memory problem as has been suggested.

Hello,

@KeithRB: Yes, the symptoms would indicate that, I just don't understand why it's OK to declare 4 arrays (any combination of which would exceed the SRAM) but not 8.

@AWOL: Yes, you're right, but the sketch is hundreds of lines, of which most is irrelevant stuff. Obviously I stripped it down to the bare minimum to test this, but I'm now using the full sketch again, as it didn't make any difference.
Also, when I try to post the code here, it exceeds the 9500 char maximum. I've attached it as a file.

sketch.ino (10.7 KB)

Because the way you do it the data lives in flash most of the time, it only gets put into RAM when the stack frame for the function is created. (Which is also a performance penalty, btw.)

Thanks!

I'm not worried about the performance issues, this isn't about speed right now. If I understand you correctly, if I were to create another function, I could bypass this issue, right? Or would I end up still getting a hang/reset when I performed function1() and then function2() (assuming that function1() contains the power on, power off and channel 0 commands, and function2() contains the codes for channel 1 through 5)?

Just leave the whole lot in PROGMEM, and forget you ever had a problem