Add an int array to the end of another int array.

Hi everyone i am having an issue trying to work out how i can add an existing array to the end of array like so.

unsigned int test1 [] {1,2,3,4,5};
unsigned int test2 [] {6,7,8,9,10};
unsigned int test3 [10];

test3[] should look like {1,2,3,4,5,6,7,8,9,10};

Please help me i would love it if you kept it as simple as possible so i can clearly understand the process.

Many thanks

Edit: sorry i mistyped the title.

The only way I can see right now, is to create a new (bigger) array and to copy the first array's data. It is a waste of memory as the first array's memory has already been reserved by the compiler.

Why would you want that? Maybe another solution could be found. Tell us more about your project.

For example: Are the integers that you want in your array between 0..255 (bytes)? There can be a very simple solution for that.

jbellavance:
Why would you want that? Maybe another solution could be found.

That is also my immediate reaction.

Thanks for your reply, I am trying to play with a Toshiba Ac unit and its ir remote and i have already managed to talk to the unit successfully but it uses an 145 length array RAW IR codes to talk to the AC unit, and no library has the format such as NEC or Sony etc so i have to use RAW format. the issue is that the array parts are 3 digits long and just two IR codes use up 74 percent of the Unos memory, so i have converted them to binary from their raw format with a neat code and placed the binary in an array and that uses just 9 bytes 0%, now i have isolated the binary octets for each function of the AC units IR remote code and the base code which is the same not matter what code is sent from the remote.

So i wanted to easily add the temp array to the end of the base array plus the fan speed array and mode array, i know how to do loops and such so i can just do a for loop and add to each column with a larger array which is what i have already done, but i was just hoping for a simpler way such as "array1 = array1 + array2" but its not that simple, i am just about to finish my degree in computing / and networking so i am ok ish at coding :o but the arduinos are unlike my normal experience with code such as Android studio, SQL, Python and so on. I have seen a post on bitshift which i briefly read up on but it i don't think i could write a piece of code smaller than what i already have to do the same job with bit shift but i don't know any thing about arduino and binary other than how to declare it "x0 or xB" so that's my issue and i was just hoping for a simple solution other that a for loop, however i will have to learn bit shifting as other wise i would have to have an array for each temp and mode etc, though the way Toshiba designed the ir code is annoying and does not count like normal binary i have spreadsheet of every but which i recorded and converted.
17c 00000000
18c 00000001
19c 00000100
20c 00000101
21c 00010000
22c 00010001
23c 00010100 there is a pattern but still i don't see a simple way to count up in binary like that do you ?

// Binary 145 bits: 01010101 00000100 00000000 01010001 00000000 00000101 01010101 01010000 00000000 00000001 --- temp 00000101 --- 00000000 --- fan full 01010000 --- heat 00000101 00000000 00000000 01010101 00000100 0

unsigned int base [145] {0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
           1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
};

unsigned int bin20C [16] {0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned int bin26C [16] {0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned int fanFull [8] {0, 1, 0, 1, 0, 0, 0, 0};
unsigned int Heat [41] {0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0
};
unsigned int Cool [41] { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int ldr;
void setup() {
  // just for forum
  if ( ldr < 200) { // light off
    for (int i = 80, n = 0; i < 96; i++, n++) {
      base [i] = bin20C [n];
    }
    for (int i = 96, n = 0; i < 104; i++, n++) {
      base [i] = fanFull [n];
    }
    for (int i = 104, n = 0; i < 145; i++, n++) {
      base [i] = Heat [n];
    }
  }

}

void loop() {

}

I may have the size of the arrays wrong please tell me if i do its quarter to one in the morning

#include <IRremote.h>
IRsend irsend;

// 20c with max fan
unsigned int  raw26max[] = {4400, 4300, 600, 1550, 550, 1600, 550, 1600, 550, 1600, 550, 500, 550, 550, 550, 1550, 600, 500,
                            550, 500, 600, 500, 550, 500, 550, 550, 550, 1600, 550, 1550, 550, 550, 550, 1600, 550, 500, 550, 550, 550, 500, 550, 500, 600,
                            500, 550, 500, 550, 1600, 600, 1550, 550, 1600, 550, 1600, 550, 1600, 550, 1600, 550, 1550, 600, 1550, 550, 550, 550, 500,
                            550, 550, 550, 500, 550, 500, 600, 500, 550, 500, 600, 500, 550, 500, 550, 1600, 550, 1600, 550, 500, 600, 500, 550, 1600,
                            550, 500, 550, 550, 550, 500, 550, 550, 550, 1550, 600, 1550, 550, 550, 550, 500, 550, 550, 550, 500, 550, 500, 550, 550,
                            550, 500, 550, 550, 550, 500, 550, 550, 550, 500, 550, 500, 600, 500, 550, 500, 550, 550, 550, 1600, 550, 500, 550, 1600,
                            550, 500, 550, 550, 550, 500, 600, 1550, 550
                           };
// 26c with max fan
unsigned int  raw20max[] = {4400, 4300, 550, 1600, 550, 1600, 550, 1600, 550, 1600, 550, 500, 550, 550, 550, 1600, 550, 500,
                            550, 550, 550, 500, 550, 500, 550, 550, 550, 1600, 550, 1600, 550, 500, 550, 1600, 550, 500, 550, 550, 550, 500, 550, 550, 550,
                            500, 550, 550, 550, 1550, 600, 1550, 550, 1600, 550, 1600, 550, 1600, 550, 1600, 550, 1600, 550, 1550, 600, 500, 550, 500,
                            550, 550, 550, 500, 550, 550, 550, 500, 550, 550, 550, 500, 550, 500, 550, 1600, 550, 550, 550, 500, 550, 1600, 550, 1600,
                            550, 500, 550, 550, 550, 500, 550, 550, 550, 1550, 600, 1550, 600, 500, 550, 500, 550, 550, 550, 500, 550, 550, 550, 500,
                            550, 500, 550, 550, 550, 500, 550, 550, 550, 500, 550, 550, 550, 500, 550, 550, 550, 1550, 600, 1550, 550, 1600, 550,
                            1600, 550, 500, 600, 500, 550, 500, 550, 1600, 550
                           };

int ldr;
boolean off = true;
boolean on = true;

void setup() {
  pinMode(12, OUTPUT); // green led
  pinMode(13, OUTPUT); // red led
  ldr = analogRead(0);
  //Serial.begin(9600);
}

void loop() {
  delay(1000);
  ldr = analogRead(0);
  //Serial.println(ldr);
  light_on();
  light_off();
}

void light_on() {
  if (ldr >= 200) {
    digitalWrite(12, HIGH);
    digitalWrite(13, LOW);
    off = true;
    if (on) {
      irsend.sendRaw(raw26max, 147, 38);
      on = false;
    }
  }
}

void light_off() {
  if (ldr <= 50) {
    digitalWrite(13, HIGH);
    digitalWrite(12, LOW);
    on = true;
    if (off) {
      irsend.sendRaw(raw20max, 147, 38);
      off = false;
    }
  }
}

This is what i first used but it uses 74% of the variables space so i had to convert it to binary to see what is changing then break it up for each function into octets just to save variable space. But i then need to add them back together in different orders such as fan off temp 19c and cooling mode and Then i need to convert them back to Raw such as 0 is 550 and 1 is 1550 i already have that code but still the process is long and all just to save space as there is too many variations to just have loads of RAW 145 length arrays so they must be broken up and then adjusted and reassemble .

Data seems to be byte sized. Run this code and see if it could help you.

String s;
byte value;

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(A4));
  value = random(65,90); //An uppercase character
  Serial.println(value);
  s = char(value);
  Serial.println(s);
  for (int i = 1 ; i <= 50; i++) {
    byte value = random(65,90);
    s += char(value);
  }
  Serial.println(s);
  Serial.println(s.charAt(0));
  Serial.println(byte(s.charAt(0)));
}

void loop() {
}

Jacques

Thanks for the warning. I was not aware of this.

Jacques

Hmm the simplest way is always the best Delta_G lol your right i never thought to do that i feel foolish to say the lest i just test it with my tv.

#include <IRremote.h>

IRsend irsend;

unsigned int  rawData[46] = {8950, 4400, 600, 550, 550, 550, 550, 1650, 550, 550, 550, 550, 550, 600, 500, 600, 500, 600, 550, 1600, 600, 1650, 550, 550, 550, 1650, 550, 1650, 600, 1650, 550, 1650, 550, 1650, 550, 600, 500, 600, 550, 550, 550, 1650, 550, 550, 550, 550}; // FIRST PART OF RAW CODE
unsigned int test [21] = {550, 600, 500, 600, 500, 1650, 600, 1650, 550, 1650, 550, 550, 550, 1650, 550, 1650, 600, 1650, 550, 1650, 550}; // SECOND PART OF RAW CODE
void setup()
{
  Serial.begin(9600);
  // initialize digital pin LED_BUILTIN - D13 as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  irsend.sendRaw(rawData, 46, 38);
  // no delay but it works , any suggestions for what the gap - delay is for 38Mhz between each binary digit i don't have a Oscilloscope to find out.
  irsend.sendRaw(test, 21, 38);

  digitalWrite(LED_BUILTIN, LOW);
  delay(2000);
}

I see that you deal with binary. The shift left operator (<<) could be of some help to you.


  for (int i = 0 ; i < 16 ; i++) {
    if (i == 5 || i == 7) value = 1; else value = 0;
    bin20C = (bin20C << 1) | value;
  }

That places bin20C in a single int value.

memcpy(test3, test1, sizeof(test1));
memcpy(test3+5, test2, sizeof(test2));

or, if you prefer

memcpy(test3, test1, sizeof(*test3) * 5);
memcpy(test3+5, test2, sizeof(*test3) * 5);

this works because arrays are treated like pointers, and when you add a number to a pointer the offset is multiplied by the size of the array elem,ent.

That is, [nobbc]test3+5[/nobbc] and [nobbc]&test3[5][/nobbc] are exactly the same, the only difference is syntactical.

You could change the library to be able to RAW-send PROGMEM data.

You could build the array to RAW-send from a table in PROGMEM.

You coud store the template with all entries divided by 50,
even the biggest (8950) will then fit in one byte (179).