Go Down

### Topic: Arrays - 3D or 2D (Read 1 time)previous topic - next topic

#### Daniel Formosa

##### Mar 01, 2011, 04:33 pm
Dear all,
as part of my project, I have a large database of car manufacturers, the amont of gas each car needs for its A/C to work and a reference no. so that the user may enter a # instead of entering the car manufacturer.
For example:

Manufacturer   Grams           Decimal no ref     Binary
Alfa
Alfa 146                   700        0                      0
Alfa 147                     540        1                       0001

I have about 400 cars that need to be saved in arduino.

Will anyone guide me in the best way to do so?
Would one use a 2D array or a 3D array?

Tnx

#### PaulS

#1
##### Mar 01, 2011, 04:47 pm
If the data can be expressed in a table, a 2D array will hold the data. If you intend to store an index (int), a vehicle description (char array), and an amount of gas (int) for 400 entries, you will need some form of external storage for the data.

#### Daniel Formosa

#2
##### Mar 01, 2011, 04:58 pm
For now, I think its better if I have only the reference no/index - (int) and the amount of gas - (int).  So a 2D array will be used for now.

The question I have is how can I calculte the amount of bytes used if i will be using a 4 digit reference #; i.e. for example 0001 to 0400, etc.... and the maximun amount of gas is a 4 digit #; i.e. 1500grams.  How can I calculate the amount of bytes I will use?

Tnx

#### PaulS

#3
##### Mar 01, 2011, 05:05 pm
The number of digits is not really relevant. A byte can hold a value from 0 to 255. Since you have values that are beyond that range, you will have to use the next larger size variable, which is an int, which takes 2 bytes. Since you are storing a total of 800 values, you will need 1600 bytes of storage.

Of course, you could use a (potentially) sparse array to hold the gas values, where the car number is used as an index into that array, so you don't need to search the car number array to find the index, and then use the index to get the gas value. This would require storing only 400 values, in 800 bytes.

#### Daniel Formosa

#4
##### Mar 02, 2011, 12:34 pm
Ok tnx for the response...

I have decided for now as you said having a 2D array, having its y axis refering to the index, and the x axis refering to the amount of gas.
I have build this code, but I don't know why it is working properly. I tried to Serial monitor a value in the array, but nothing pops out. What am I doing wrong?

const int REF = 10;
const int GRAMS = 2;
int gas_referno[10][2] = {
{1,700},
{2,700},
{3,650},
{4,700},
{5,680},
{6,580},
{7,550},
{8,680},
{9,580},
{10,599}

};

void setup()
{

}

void loop()
{
Serial.println(gas_referno[2][1]);

}

#### Daniel Formosa

#5
##### Mar 02, 2011, 12:41 pm
Ok I found what the problem was....I didn't initiliase the Serial.begin(baudrate).
So now I can see that I am correctly saving the values in the array....

But is there an easier way and shorter way in which I can save in the array since I have about 400 values to be saved...

#### PaulS

#6
##### Mar 02, 2011, 12:46 pm
Code: [Select]
`int gas_referno[10][2] = {                                {1,700},                                {2,700},                                {3,650},                                {4,700},                                {5,680},                                {6,580},                                {7,550},                                {8,680},                                {9,580},                                {10,599}`
Will the first value in each pair always be equal to row number + 1? If so, a simple 1D array would suffice:
Code: [Select]
`int gas_referno[10] = {                                {700},                                {700},                                {650},                                {700},                                {680},                                {580},                                {550},                                {680},                                {580},                                {599}`
To get the gas amount for vehicle #3, gas_referno[2] contains the value. To get the value for vehicle #3, gas_referno[n-1] contains the value.

#### Daniel Formosa

#7
##### Mar 02, 2011, 12:52 pm
Yes the 400 cars are all in order.
But the user has to input the reference no of the car so that he can see the amount of gas needed, and later on develop an automated syste.....  For example if he wants car no 1 (for example Alfa), the user keys in #1, the arduino looks into the amount of gas, i.e. in first case 700g. This value later has to be taken care of....

The best think I would like is that when pressing 1 it displays the amount of gas and the car model.

So a 1D array would be good if I only need the amount of gas??

Tnx

#### PaulS

#8
##### Mar 02, 2011, 01:28 pm
You would need a 1D array of ints to show the amount of gas. The show the amount of gas and the vehicle description, you would need 2 1D arrays. An array of ints to hold the amount of gas and an array of pointers to char arrays to hold the description. Or, the second array could be an array of String objects.

But, you said that you didn't want to store the descriptions on the Arduino, and you can't really, because there isn't enough memory.

How will the user know what reference number to enter?

I'm really thinking that you need an SD card to hold all the data. The Arduino would then just look up data on the SD card.

#### Daniel Formosa

#9
##### Mar 02, 2011, 01:32 pm
Yes that is the problem.  The user will know the reference no since, I will make a 'manual' and in it there will be the car model - and its refernce no.  But it isn't that practical...Since at a later stage I have to use GUI to constantly update the Arduino with car models.
Is working with SD cards difficult since I have never looked at it?

Tnx again

#### PaulS

#10
##### Mar 02, 2011, 02:16 pm
Quote
Is working with SD cards difficult since I have never looked at it?

No. They are pretty easy, with the right hardware and the right library.

Quote
Since at a later stage I have to use GUI to constantly update the Arduino with car models.

Perhaps you've observed that the Arduino, by itself, does not have a way to display a graphic user interface. How/where will this GUI be displayed?

#### Daniel Formosa

#11
##### Mar 02, 2011, 02:19 pm
I have decided that for now, since this project has a deadline....it is better to work only with the refernce no only. And then If I manage to have time left I will look at the SD cards....so that I can also display the model.

Yes I know that it does not have GUI.  I will try to use the visual studio in order to achieve this by sending data serailly to the arduino.
Till now I have a very basic idea of how to create forms....

If you have any links to the GUI interface that would really be appreciated.

Tnx

#### cr0sh

#12
##### Mar 02, 2011, 06:54 pm
Just a note: Technically, any array of N dimensions can be expressed as a 1-dimensional array...
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Go Up

Please enter a valid email to subscribe