0

I am building a Infrared Remote with Arduino. Each Command for the Infrared transmitter is storred as an array of microsecond delays for a logic signal based on the RC-5 standard.

For example:

// Command #1866: Command:$1865
// Protocol: RC5, Parameters: D=30U F=10U
const uint16_t repeat_Command__1865[] PROGMEM = { 889U, 889U, 1778U, 1778U, 889U, 889U, 889U, 889U, 889U, 889U, 1778U, 889U, 889U, 889U, 889U, 1778U, 1778U, 1778U, 1778U, 65535U };

Because i want to first check which commands work on the devices i want to controll i want to check every available command inside the RC5 standard. Its 5-Bit Address and 6-Bit Commands result in about 2000 Commands i need to check. I generate these commands with a programm called IRScrutinizer which spitts out the microsecond delays when i give it the device Addres (1-15) and the command (1-63). It generates the above code. I have a way to generate all 2000 commands via a script. So what i have is 2000 Arrays named like: repeat_Command_{Number of Command}

As i read now there is no easy way in c++ to access variables by a variable defining the name of a variable.....

So i need to switch case trough all commands inside a for loop like this:

void loop() {
    Serial.println(F("Looping trough all Numbers"));
    for (int i = 0; i<1983;i++)
    {
      delay(1000)
    }
    switch (i) {
    case 1L:
        sendRaw(repeat_Command_, 24U, 36U);
        break;
    case 2L:
        sendRaw(repeat_Command__1, 22U, 36U);
        break;
    case 3L:
        sendRaw(repeat_Command__2, 24U, 36U);
        break;
.
.
.

But this uses to much memory for the Arduino to handle. Since i use Progmem the problem is only the switch case argument.

My idea was to store the commands in a multidimensional array and then accessing each "row" in a forloop.

But how can i do that?

When i try:

const uint16_t commands[1][] PROGMEM = {1,2,3,4};

const uint16_t commands[2][] PROGMEM = {5,6,7,8};

This does not work and i would not expect it to but i dont know my way with array good enough to do that.

I get an error:

declaration of 'commands' as multidimensional array must have bounds for all dimensions except the first

The problem is that the length of each command is not fixed.

Another way would be to genereate the arrays with the microseconds inside the for loop. But this a whole nother logic problem i would like to rather not solve :D

  • 1
    you can get a 2D array like: `const uint16_t commands[2][4]{{5,6,7,8}, {5,6,7,8}};` – Oblivion Oct 31 '21 at 18:06
  • 1
    You can use that same script to generate something like `const uint16_t* AllCommands[] = {repeat_Command_, repeat_Command__1, repeat_Command__2, ...};` Then you can write say `AllCommands[2]` instead of `repeat_Command__2` – Igor Tandetnik Oct 31 '21 at 18:17
  • https://stackoverflow.com/questions/9986591/vectors-in-arduino – Ripi2 Oct 31 '21 at 18:38
  • @IgorTandetnik i like this. But it seems that its somehow too much vor arduino. When i do this with all 2000 commands i get: ``` C:\Users\SEBAST~1\AppData\Local\Temp\ccyCQTch.s: Assembler messages: C:\Users\SEBAST~1\AppData\Local\Temp\ccyCQTch.s:52079: Error: value of 82960 too large for field of 2 bytes at 7 C:\Users\SEBAST~1\AppData\Local\Temp\ccyCQTch.s:52080: Error: value of 82916 too large for field of 2 bytes at 9 C:\Users\SEBAST~1\AppData\Local\Temp\ccyCQTch.s:52081: Error: value of 82868 too large for field of 2 bytes at 11 ``` – Sebastian Scholz Oct 31 '21 at 19:58
  • @Oblivion i try it like this.. i hope i can edit this with vim somehow because the output of IRScrutinizer is limited to multiple variables. – Sebastian Scholz Oct 31 '21 at 20:01

1 Answers1

0

Looks like you're getting confused by the syntax for multidimensional arrays - you can't declare the same thing in multiple places like that and expect them to be combined.

Your simple test case would have to be written like this:

const uint16_t commands[][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8}};

But this would only work if all the commands were the same length. (That's the 4 in the second dimension above.) 2D arrays are stored with each row immediately after the previous in memory, so each row has to be the same size.

One solution is to store each command in its own array, then you could use two more arrays (or an array of structs) to store the lengths of commands as well as pointers to them.

atspaeth
  • 1
  • 1