add new values to array

Hello ,
I want to add new values into an array .

I understand I need to create new array but how do I know what size it will be ?
(because I don't know how many changes I need to do )

I did the same thing in C# - so maybe this can help (I used "List" so it much more easy ....)
this is the C# code that is working

 List<string> msg= new List<string>();

*
* // insert data into msg list - so it's not empty.
*

 List<string> changeOne = new List<string>();

 for (int i = 0; i < msg.Count; i++)
            {
                if (msg[i]=="A") //this is a new message 
                {
                    changeOne.AddRange(new[] { "5C", "70", "5C" ,"70", "5C", "70", "5C","73", "20" });
                }

                

                else
                {
                    if (msg[i] == "0")
                    {
                        //changeOne.Add("1A");//start of new msg 
                    }
                    else
                    {
                        changeOne.Add(msg[i]);
                    }

               
               }

          
            }

Thanks,

Thing is, you can make an array with variable length. BUT, because of the limited memory and basic memory management you just don't want to do that.

Just make the array as big as you ever need.

If you don't know how large that will be, think again. Because like I said, memory is limited.

If you say, really big, think again. Something about limited memory.

Do you really need to store all that in RAM? Or would an SD card be easier.

can you say what is the max size I can save? - I don't think it will be more then 150 hex values
also how can I know how much am I using when the code is running?

thanks ,

An Uno has 2k RAM so 150 bytes is fine. But I do assume byte because hex isn't a type but a representation (just like decimal and ASCII).

I'm using Arduino mega
and when I upload the code I don't see any problem :
I'm using 4028 bytes(1%)
global use 691 bytes (8%)

so I guess as you said the problem is in the "reading part"
how can I know how much ram is used ? can this be the problem?

Thanks,

Hello ,
I want to create array from array 1 and array
for example

byte array1[3] = {0x00 , 0x01 , 0x02};
byte array2[3] = {0x0A , 0x0B , 0x0C};

so in the end I will get :

byte final  [6] = {0x00 , 0x01 , 0x02 , 0x0A , 0x0B , 0x0C}

is there a function to it ?
or I need to make a loop?

also if I wand to add 4 arrays? what do to ?

Thanks ,

I don't know simply because I can't see your code. No idea how and where you made the array :wink:

You can use a loop. BUT, it's wasteful... The Arduino isn't a Intel i9 with 64GB of RAM and fancy memory management. Why not:

a) Put the values in one array to start with?
b) Just loop over both sequentially if you need it.

But really, this is the THIRD very related question about arrays. Which all would be clear if you did some research / leaned a bit more about actually using micro controllers...

On a micro controller you*
a) don't dynamically size an array
b) don't concatenate arrays
c) don't do fancy dances like reverse if you don't really need to. For example, sort it.

  • Of course there are always exceptions but he. "Exceptions prove the rule" as we say.

a) I can't do that because I get data from all kind of devices and then need to send it as 1 array to another device.

b) can you show me an example of how to do it?
because let say I define a array of size 150 (which is the maximum byte I can think of I will need)
how do I loop the next one
what will be the first place to put the data for the next array ?

array1 [] {0x01 ,0x02}
array2 [] {0xA1 , 0xA2}
array3 [] {0xC1 , 0XC2}
arrayFinal [6] = {0};

for (int i = 0 ; i<sizeof(arrayFinal); i++)
{
arrayFinal[i] =array1 [i]; 
}

for (int i ="What do I need to write here? ;  i<sizeof(arrayFinal); i++)

Why?

why what?

Koren12345:
Hello ,
I want to create array from array 1 and array
for example

byte array1[3] = {0x00 , 0x01 , 0x02};

byte array2[3] = {0x0A , 0x0B , 0x0C};




so in the end I will get : 



byte final  [6] = {0x00 , 0x01 , 0x02 , 0x0A , 0x0B , 0x0C}




is there a function to it ? 
or I need to make a loop?

also if I wand to add 4 arrays? what do to ?

Thanks ,

what is the purpose of final is your example?

why not call the smaller arrays one by one as and when needed?

Why do you want to concatenate byte arrays?

let say I took enough place
how can I add values in the middle of the new array like I did on the C#?
any thoughts?

Thanks ,

Why?

Koren12345:
let say I took enough place
how can I add values in the middle of the new array like I did on the C#?
any thoughts?

Thanks ,

again see reply #11 and to repeat AWOL: WHY?

I will explain :
my project is to read from 1 RS485 device , and send to another RS485 device the data with some changes.

I'm using Mega

to be able to read from device and save the data as HEX - is working.
to send data to other device (is working with sample\demo message )
so I know how to read and how to write .

now my problem is adding some chars in the original message

this is why I want\need to do this

do you need more explain or something to understand what I need to get ?

  • this project is working for me on on C# on x86 , so it's doable -
    my problem is to make it work on Arduino ...

Thanks ,

to be able to read from device and save the data as HEX - is working.

What is the data type of the array where the HEX data is saved ?

byte array

Why do you think you need to add (concatenate) byte arrays, if all you are doing is passing on messages?