2D Arrays. Why isn't it simple?!

int ledArray[25][3] = {
{4,163,52},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0}
};

So that’s my array. To try to access the array, I’m using:

for(int i=0; i<25; i++) {
     Serial.println("VALUE IS: " + ledArray[i][0]);
}

All I get is "VALUE IS: " being printed. Surely it should print the first value of each of the arrays. I don’t get it! Please help! :frowning:

Need to 2 print statements

Serial.print("VALUE IS: ");
 Serial.println(ledArray[i][0]);

Mod edit: code tags added :wink:

Woohoo! It worked! :) Thanks. When I try to actually use the value however... it doesn't work...

This works:

strip.setPixelColor(i,Color(20, 30, 80));

But this doesn't:

strip.setPixelColor(i,Color(ledArray[i][0], ledArray[i][1], ledArray[i][2]));

Any ideas?

Define"works".

Need to see your code.

#include <SPI.h>
#include "WS2801.h"

WS2801 strip = WS2801(25);

int ledArray[25][3] = {
{4,163,52},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0}
};


void setup() {
  Serial.begin(9600);
  strip.begin();
  strip.show();
}

void loop() {
    for(int i=0; i<25; i++) {
     strip.setPixelColor(i,Color(20, 30, 80));
     //strip.setPixelColor(i,Color(ledArray[i][0], ledArray[i][1], ledArray[i][2]));
     strip.show();
     delay(20);
    }
    
}

/* Helper functions */

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

I’m using a library from https://github.com/adafruit/WS2801-Library to help drive a strip of LEDs. :~

Why are you trying to pass integer values to Color()? (that's a hint, son)

The two cases are not the same. In the first case you're setting every pixel color to Color(20, 30, 80), and in the second case you're setting pixel 0 to Color(4,163,52) and the rest to Color(0,0,0).

So what do you mean by "works" verses "doesn't work"?

Typical debugging techniques would be to add a few Serial.println() commands so you can see exactly what you're passing to Color() and to the setPixelColor() function. (E.g. have the Color() function print out the parameter's passed to it). Then you can compare the differences when you switch between the hard-coded Color and the array-based Color.

I'm sorry guys... feel bed for wasting your time. You're right dhunt! It was working all along... I was just being a complete dumbass... Now I can get on with writing my program! Thanks guys!! :D