Pages: [1]   Go Down
Author Topic: Arrays - 3D or 2D  (Read 1058 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 643
Posts: 50443
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 643
Posts: 50443
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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]);
 
}
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 643
Posts: 50443
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
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.
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 643
Posts: 50443
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 643
Posts: 50443
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 5597
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just a note: Technically, any array of N dimensions can be expressed as a 1-dimensional array...
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Pages: [1]   Go Up
Jump to: