# Large Variable from text to Arduino

Hey everyone,

Basically what I am trying to do is to import a large matrix from MatLab into Arduino. It is approximately 96x365 values and the values range from -180 to 180. Obviously I cant define a matrix (is that what you call them in C?) and type this out into the program editor (possibly copy and paste) so my next thought was I should import it from a text file. I have calculated that all of the data should take up approx. 36 kilobytes. How do I go about importing this matrix and saving it within my Arduino??

many thanks, sunssoldier

I have calculated that all of the data should take up approx. 36 kilobytes.

That's going to need a very large shoe horn to fit into the 2K of SRAM that you actually have. Unless you have a Mega, with 8K SRAM, in which case you can use a slightly smaller shoe horn.

Yeah I am using the Mega, can I use the 128 KB of flash memory? or do I need to put it on an SD card?

can I use the 128 KB of flash memory?

Flash memory is read-only. At run-time, you can not write to it.

or do I need to put it on an SD card?

You could. It’s a mystery what this data is, or what it represents, or what you will do with it, so it’s not really possible to offer any more help.

You can define multi-dimension arrays in C++. You can define them with special access functions if you want.

When you’re planning how you’re going to use your RAM, be sure to leave enough for the stack. Every local variable will need stack space.

Will you be using floating point? Besides the lack of FPU you will find that floats and doubles are both 4 bytes only.

Obviously I cant define a matrix ...

Why is that?

It is approximately 96x365 values and the values range from -180 to 180.

...

I have calculated that all of the data should take up approx. 36 kilobytes.

Well, actually a signed byte will hold -128 to +127. Since your values are larger than that you will have to use an int, which is 2 bytes. Therefore you will need 96 x 365 x 2 bytes which is 70080 (about 68.4 Kb).

If the values are not going to change, then you could just define a large PROGMEM array, and hold it all in that.

While I am just getting used to posting on this forum I wont be quoting

Thanks PaulS, let me explain The 35040 values define the most efficient angle that a tracking solar array is to be at. Calculating these takes a lot of oomph so I am doing the calculation in MatLab for a specific geographic place, time and array orientation. Then will upload those values into my Mega, which will allow it to know on whatever day, at whatever time, the proper angle should be some corresponding value in my matrix. So the data needs to be read only and it will be integers

Nick Gammon The range which the angles span will only be 90 degrees after some modifications, so I guess each value will use 1 byte a pop. Doesn't matter of the values are signed or unsigned because I can modify my program to take whichever ones. When I said I can't define a matrix I meant I can't/won't type each angle value in one by one from Matlab to Arduino. PROGMEM uses the flash memory?

Smoke I will not be using floating points, and I have brushed up against arrays but thought that there was an easier way.

So I still have a problem with putting that large matrix into my Mega flash memory. If I could just import a csv file to some variable V and call it out by its row and column i.e V(r,c)=___ I would be set...but I guess C is much different than MatLab.

All you need to do is get your figures into some format, like one per line with a comma, like in the example below. Then just compile it:

``````#include <avr/pgmspace.h>

#define NUMITEMS(x) (sizeof(x) / sizeof(x[0]))

PROGMEM char foo  [] = {

5,
6,
7,
8,
9,

// etc. etc. for thousands of lines

};

void setup ()
{
Serial.begin (115200);
for (long i = 0; i < NUMITEMS (foo); i++)
{
byte x = pgm_read_byte (foo + i);
Serial.println (x, HEX);
}
} // end of setup

void loop () {}
``````

Back in the 70's a teacher showed me how they used to use a clock movement to keep telescopes, once aimed, on track. After all, elevation of stars and planets won't change much in a night, you only need to match the Earth turning.

There is a language called Forth written by an astronomer named Charles Moore. It is especially good at fast Fourrier (sp) transforms, fast enough to have been useful on late-1950's computers as the first Forth core was finished and distro'd in 1959. You might get some tricks from somewhere in that direction as he was pointing telescopes with computers that a 328P is a supercomputer by comparison!

Thanks everyone

Calculating these takes a lot of oomph

Perhaps, but the Arduino has nothing else to do and you'll only need the numbers for the current day (or less) so why not let the Arduino calculate it on the fly?