Pages: [1]   Go Down
Author Topic: Variable Types as Applied to Arrays  (Read 1155 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Wit is a pale imposter of wisdom.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First of all, thanks for your patience since this may be a really noobish question.

I have a couple of arrays with a maximum of 5 values between the two of them.  FWIW These values actually relate to digital pins 9 - 13.  The array values are selected by user input and can be reset during operation.  

As it stands now I fill these two arrays with zeroes whenever the values are to be redefined since more or less positions may be filled the next time around.  Elsewhere in the sketch these arrays are used to indicate which pins should be set HIGH or LOW based on other "user-set" timing variables.

Right now, I just count through all the array positions with a for-loop no matter how many "real" (non-zero) values are in there.  Essentially the sketch will set pin 0 HIGH (or LOW) when the count reaches positions in the array beyond the values the user intends.

Is there any "real" action taken when the chip is told to set pin zero HIGH or LOW?  Should I append a special value to indicate the array is over? Should I check that the value extracted from the array is > zero?    Is there some other accepted method of counting through an array of unknown length?

I just don't have the experience to know if this extra effort is even necessary.

  Right now I am declaring these arrays as int syncdArray[4] and int oppArray[4].

Am I correct in thinking that I could instead use byte instead of int when declaring the arrays since all the values are positive and between 0-255?
 
Thanks so much for your help and input.

Sam

PS If I call function loop(); to get out of an option selection menu, is that okay?  It seems to work.
Logged

I have never let my schooling interfere with my education.
~ Mark Twain

http://www.lewrockwell.com/

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

As long as you can tell whether the entry is "valid" (9-13) or "invalid" (0), it sounds fine to me.  If pin 0 were a possible valid setting, I'd use -1 for the "invalid" marker.  The overhead of checking a few extra array entries is miniscule; unless you make a giant array I wouldn't worry about it.
« Last Edit: March 09, 2009, 06:16:23 pm by halley » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have a couple of arrays with a maximum of 5 values between the two of them.

If this means you actually need an array with 5 indexes you'll need to declare them as int array[5]; smiley

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Wit is a pale imposter of wisdom.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Halley,
  Thanks for your response.  Am I to understand that there is no effect by setting pin 0 HIGH or LOW?


AlphaBeta,
Thanks too for your response.  Isn't the extra position only required when it is a "char array"?  Since arrays are zero indexed I should declare an "int array" with 5 positions as:
Code:
int array [4];
Whereas if I needed a "char array" with 5 positions I should declare it as:
Code:
char array [5];
Please correct me if I'm wrong.

My other question was if it is okay to use byte in lieu of int when declaring my small arrays.  My hunch is yes I should but I suppose I should just try it and find out empirically!
 smiley-wink

Thanks again,
  Sam



Logged

I have never let my schooling interfere with my education.
~ Mark Twain

http://www.lewrockwell.com/

Bristol, UK
Offline Offline
Edison Member
*
Karma: 1
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want an int array with 5 locations, you must declare:

Code:
int myarray[5];

You can then use the locations numbered 0 to 4:

Code:
myarray[0] = 10;
myarray[1] = 20;
myarray[2] = 30;
myarray[3] = 40;
myarray[4] = 50;

Arrays are declared just the same whether character or integer.  Character arrays that are going to be used as nul-terminated strings require an extra character to be declared.  All array index numbers begin at zero.
« Last Edit: March 10, 2009, 11:32:49 am by anachrocomputer » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The number of indices in your array is independent of the size of each individual index. That is to say if you declare

Code:
int myArray[4];

you can change that to

Code:
char myArray[4];

Both of these have 4 places allocated. However, the int array is double the size of the char array. You access them the same way tho.

Code:
myValue = myArray[1];

This will give you the second value of each of those arrays. The data however, will either be int or char depending on your declaration.
« Last Edit: March 10, 2009, 11:58:30 am by MHill » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
However, the char array is double the size of the int array.
I think wanted to say that the other way around smiley-wink
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I think wanted to say that the other way around  

Oops, you are correct.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Wit is a pale imposter of wisdom.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Splendid!

Thanks to all for a thorough explanation!

Sam  smiley-wink
Logged

I have never let my schooling interfere with my education.
~ Mark Twain

http://www.lewrockwell.com/

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Am I to understand that there is no effect by setting pin 0 HIGH or LOW?

No, pin 0 is a valid pin, so whatever is connected (such as your USB serial connection) would be affected.

What I meant to say was that for your purposes, you need to distinguish entries that are valid vs entries that are invalid.

Code:
#define NUM_ENTRIES 5
byte myPinNumbers[NUM_ENTRIES] = { 8, 9, 10, [glow]-1[/glow], 13 };
byte myDesiredOutputs[NUM_ENTRIES] = { HIGH, HIGH, LOW, [glow]LOW[/glow], HIGH };

int i;
for (i = 0; i < NUM_ENTRIES; i++) {
    [glow]if (myPinNumbers[i] >= 0)[/glow]
        digitalWrite(myPinNumbers[i], myDesiredOutputs[i]);
}

The highlighted parts look for "invalid" pin numbers and don't try to output anything for those entries.
« Last Edit: March 10, 2009, 12:24:44 pm by halley » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Wit is a pale imposter of wisdom.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, I'm on it.  Thanks again for the clarification.

Sam
Logged

I have never let my schooling interfere with my education.
~ Mark Twain

http://www.lewrockwell.com/

Pages: [1]   Go Up
Jump to: