Troublesome ARRAYS

Hi Folks, I'm trying to get a number from my array "litestate(12,4)". but when i try to retrieve a number from the array I get "litestate cannot be used as a function"

      int litestate[12][4]=
      {
        {1,0,0,0},
        {0,1,0,0},
        {0,0,1,0},
        {0,0,0,1},
        {1,1,0,0},
        {1,0,1,0},
        {1,0,0,1},
        {0,1,1,0},
        {0,1,0,1},
        {0,0,1,1},
        {1,1,1,0},
        {1,1,1,1}
      };

//   colum = random(0,13);
//   for (row = 0; row <= 4; row++);
//      int state = litestate(colum,row);
//      digitalWrite(row+1, state);

this gets me "litestate cannot be used as a function"
basically i have four lights i want to turn on and off, hence the array 1=on 0=off

Index arrays with [] not ().

You need to use square [...] brackets for arrays, e.g. litestate [0,3].

Also as arrays are indexed from 0, the index should go from 0..11 if you declare it as size 12. So it should be colum = random(0,12);

I don't think that works.

litestate[0][3]

would work.

4 Likes

See:
forum.arduino.cc/t/italiano-lo-spamm-bar-part-3/1333957/308
:rofl:

Oh yeah, wrong language :slight_smile:

2 Likes

Thanks for trying to use code tags; it unfortunately did not work out. I did fix it for you.

Code tags are three back ticks (```), not three ticks ('''). They need to be at the beginning of a line (before and after the code) and on their own line.

1 Like

My first Post and I absolutley love you people, been struggling for ages with this. STUPID STUPID me with the brackets. as soon as i changed the brackets it worked.....
THANK YOU SO MUCH

2 Likes

It might compile OK, but it won't work 100% correctly. As pointed out, your code will attempt to access indexes outside of the array. This will lead to strange bugs which may appear only randomly, so don't do it. Make sure your code only ever accesses indexes which are inside the array. The first index must be between 0 and 11. If it is 12, that would be outside the array. The second index should be between 0 and 3, but not 4.

1 Like

@franitauk
As a newbie to arrays(clearly), you need to internalize one thing. When defining an array, you define the number of elements, e.g.
int arry[12];
tells the compiler there are 12 elements. What confuses many is that the elements are indexed with numbers from 0 to 11, which confounds many - where did
arry[12] ;
go? Answer - It doesn't exist, due to zero-based indexing, which you must get used to quickly.
Maybe that helps, maybe you've already got it, can't tell.

1 Like

This will update pin 1. What type of Arduino are you using? On many Arduino, like Uno, Nano V3, Mega etc, pin 1 is not available for attaching LEDs because it is used for uploading code and using serial monitor.

You use () with functions, math and much more.
You use [] with arrays.
The arrays start from 0, keep that in mind.
And, you can't do litestate(0,3) or litestate[0,3] you've got to do litestate[0][3]
Remember, if you have 12 objects in an array, it'll go 0 o 11 not 1 to 12. (since 0's a number too.) so that would be colum = random(0,11);.

1 Like
  • as others noted, the random statement must produce a value from 0...11, so check it's documentation
  • You've confused rows and columns. In the array as presented, there are 4 columns(0...3) and 12 rows(0...11),
  • that for loop should run 0...3, so the conditional should be "row <=3"
  • the digital write should be arranged to write to pins 2,3,4,5 - stay away from pins 0 and 1; so, digital Write(row+2, state) would be better. Even better would be to index an array of 4 pin numbers using the row value, so that you can connect ANY 4 digital pins, any order.

Just some hopefully helpful comments.

1 Like

Yes. Curl up in front of the fire or under the umbrella, set the Wayback Machine for 1982 and enjoy:

a7

2 Likes

Again, I can't thank you people enough. reading all your replies has learnt me a lot about arrays. I used to use "BASIC" and only just started using Arduino, unfortunatley i mix the two at times and get bad results along with frustration......
THANK EACH AND EVERYONE OF YOU

Hi People, i changed my code around as sugested but now i'm getting......
Compilation error: too many initializers for 'int [11][4]'
My Book "Programing Arduino, getting started with sketches" 2nd edition. only does single arrys, i'm trying to do a 2 dimensional array

As I said earlier, the creation of the array is different from the use.

int arry [11,4];
will have 11 rows, so you can only provide 11 rows of initialization.

P.S. When you come back with additional questions, please include the newest version of your code; we otherwise have to infer, and that leads to poor assumptions about what you've done.
Thanks

1 Like

@franitauk
I provided this privately, but it really should be included here.

const int LEDpins[] = {2, 3, 4, 5};//create an array of pin numbers

const int LEDpincount = sizeof(LEDpins) / sizeof(LEDpins[0]);//calculate the size for use later

const int ON = HIGH;  //defines the LED conditions in terms of output pin state
const int OFF = LOW;

int litestate[][LEDpincount] =    //defines  your states.  Remember, if the number of pins changes, the number of columns will need changing
{
  {1, 0, 0, 0},
  {0, 1, 0, 0},
  {0, 0, 1, 0},
  {0, 0, 0, 1},
  {1, 1, 0, 0},
  {1, 0, 1, 0},
  {1, 0, 0, 1},
  {0, 1, 1, 0},
  {0, 1, 0, 1},
  {0, 0, 1, 1},
  {1, 1, 1, 0},
  {1, 1, 1, 1}
};
const int states = sizeof(litestate) / LEDpincount / sizeof(litestate[0][0]); //calculate number of random states created

void setup() {
  for (int n = 0; n < LEDpincount; n++) {  //loop through pin array
    pinMode(LEDpins[n], OUTPUT);            //sets the mode of every LED pin
    digitalWrite(LEDpins[n], OFF);  //not essential, but shuts off all LEDs to start with
  }
}

void loop() {
  int row = random(0, states);// since second limit is exclusive, we will never get states as a value, just states to states-1
  for (int column = 0; column < LEDpincount; column++) {  
    digitalWrite(LEDpins[column],litestate[row][column]);  //access the correct state for the row:pin desired
  }
}

Untested, unfortunately, so beware.

Not untested. It works.

It's a bit more interesting if a short delay is included in the for loop. 777 is too long, 166 is effective.

I changed both the size calculations, which were fine. I used the "it always works" in my pocket to avoid thinking about it any more. :expressionless:

const int states = sizeof litestate / sizeof *litestate;

a7

1 Like

Yes, I avoided the pointer notation for our newbie's sake, but it is a better form overall.
And yes, the delay is needed, for mere humans to distinguish the action. Thank you.