Array question

Hello

I’m pretty new at programming. I’ve watch countless hours of tutorials and read lots of pdf books on c/c++ programming as well as bought and read exploring arduino by Jeremy Blum.
I can’t seem to find information on how to assign multiple pins into a single variable or even if this is possible. I’d like to ‘group’ the pins of 2 ws2803 that are connected to the red leg of a rgb led to one variable, so one line of a sketch will address all the red led , is this possible and if so can some one help me with a simple, illustration of what that might look like.

Thank you,
Derek

I can't seem to find information on how to assign multiple pins into a single variable or even if this is possible.

You can't

Mark

About the best you can do is write a function that treats the LEDs as you want them within the function. Even a structure is still oging to require access to individual pins.

something like this should work.

int drinkPins[] = {41,42,43,44,45,46};
drinkPincount = 6;


 for (int thisPin = 0; thisPin < drinkPincount; thisPin++) {digitalWrite(drinkPins[thisPin], HIGH); }

Can always put the pins into an array:

byte ledPins [] = {2,3,4,5,6,7,8,9,}; // ledPins[0] = 2, ledPins[1] = 3, etc.
byte x;

in setup:
for (x=0; x<8; x=x+1){
pinMode (ledPins[x], OUTPUT);
}

in loop, some examples:
digitalWrite (ledPins[0], HIGH); // turn on LED2, assuming anode is on 2 and cathode has resistor to Gnd

for (x=1; x<5; x=x+1){
digitalWrite (ledPins[x], LOW); // turn off LEDs on pins 3,4,5,6
}

There is also Direct Port Manipulation, where you can have control of the pins in each PORT. However this could cause issues if you are not careful. It is suggested to not be used but it is another option.

Wow thanks all for the help!

Derek

I use direct port manipulation all time. Maybe the code's not as portable, but if I'm using direct port manipulation I'm writing for speed on a particular device anyway.

Use pinMode in setup to get the IO data direction correct (sets the Data Direction Register for t he port, DDRx), then: outputs: PORTD = PORTD & 0b11111011; // clear bit 2, leave the rest alone PORTD = PORTD | 0b00000100; // set bit 2, leave the rest alone

inputs: readPortD = PIND; // manipulate as needed after that

can also toggle a bit by Writing to PINx: PIND = PIND & 0b00000100; // toggle bit 2, from High to Low, or Low to High

I agree, it is very fast but you know how to properly use DPM. But for someone who doesn’t, they might go nuts trying to figure out what went wrong. I would save it until he actually gets it to work first then make it more efficient.

Good call.

battleshy: I can't seem to find information on how to assign multiple pins into a single variable or even if this is possible.

If each pin is 1 bit, OFF/ON, then you can pack 8 into a byte, 16 into a word, 32 into an unsigned long.

What you plan to do with them may or may not work. From this link I see the ws2803 wants byte per PWM output.

http://thomasolson.com/PROJECTS/WS2803_Testing/

If you had three 100 byte arrays, one for red, one for green, one for blue, each representing the 0-255 PWM intensity to be sent out for 100 RGB LEDs, then the code could be written to send a byte from each in turn. Or a [100][3] array I guess.

Depends on how they're wired. I get this impression that maybe the OP is using chip-per-color, but it's only an impression.

I'd like to 'group' the pins of 2 ws2803 that are connected to the red leg of a rgb led to one variable, so one line of a sketch will address all the red led

And the answer is to send the WS2803 a whole byte variable per pin via SPI, not 1 variable for multiple pins. It's possible to pack 4 bytes into an unsigned long but the SPI port will only read 1 at a time so why bother?

Got all the led’s lighting up just not the right colors, struggling with the sketch but I’m having a blast with it. Here is what I wrote with the help of you guys and http://www.thomasolson.com/PROJECTS/WS2803_Testing/ , my schematics are the same as in this link.

const int ws2803_clockPin = 8;
const int ws2803_dataPin = 9;
int Delay = 5;
//byte nLEDs= 12;
byte Red[] = {0,3,6,9,12,15,18,21,24,27,30,33};
byte Green[] = {1,4,7,10,13,16,19,22,25,28,31,34};
byte Blue[] = {2,5,8,11,14,17,20,23,26,29,32,35};
byte wsOut;
#define nLEDs 12
uint8_t ledBar[nLEDs]; 
void setup() {
  Serial.begin(9600);
  pinMode(ws2803_clockPin, OUTPUT);
  pinMode(ws2803_dataPin, OUTPUT);
  digitalWrite(ws2803_clockPin, LOW);
  delayMicroseconds(600);
  for(wsOut = 0; wsOut < nLEDs; wsOut++) {
    Red[wsOut] = 0x00;
    Green[wsOut] = 0x00;
    Blue[wsOut] = 0x00;
  }
  loadWS2803();
}
  void loop() 
{
  for( wsOut = 0; wsOut < nLEDs; wsOut++)
  {
    Red[wsOut] = 255;
    loadWS2803();
    delay(Delay);
  }
}
  void loadWS2803()
  {
  for (int wsOut = 0; wsOut < nLEDs; wsOut++)
  {
  shiftOut(ws2803_dataPin, ws2803_clockPin, MSBFIRST, Red[wsOut] | Green[wsOut] | Blue[wsOut]);
  }
  delayMicroseconds(600);
}

Pretty sure this ORs the three values together and then send out the resulting byte:
Red[wsOut] | Green[wsOut] | Blue[wsOut]

Whereas what you need is 18 bytes, one for each output of the WS2803.
With 6 RGB LEDs, that’s one byte for each color.
See page 6 - 144 bits go out, 8 bits per each of 18 channels (outputs).

WS2803-preliminary-En.pdf (437 KB)

Should be:
(Red[wsOut] << 16) | (Green[wsOut] << 8 ) | Blue[wsOut]

Made some progress! The code is lighting the right colors, most of the time.

const int ws2803_clockPin = 8;
const int ws2803_dataPin = 9;
#define nLEDs 12

int Delay = 50;

byte Red[12] = {0,3,6,9,12,15,18,21,24,27,30,33};
byte Green[12] = {1,4,7,10,13,16,19,22,25,28,31,34};
byte Blue[12] = {2,5,8,11,14,17,20,23,26,29,32,35};
int wsOut;
uint8_t ledBar[nLEDs]; 
void setup()
{
 // Serial.begin(9600);
  pinMode(ws2803_clockPin, OUTPUT);
  pinMode(ws2803_dataPin, OUTPUT);
  digitalWrite(ws2803_clockPin, LOW);
  delayMicroseconds(600);
  loadWS2803();
}
  void loop() 
{
 
   for(int x= 0;x<12;x++)
    {
    wsOut=Red[x];
    ledBar[wsOut] = 0xff;
    loadWS2803();
    delay(Delay);
    }
    for(int x= 12;x>0;x--)
    {
    wsOut=Red[x];
    ledBar[wsOut] = 0x00;
    loadWS2803();
    delay(Delay);
    }
   for(int x= 0;x<12;x++)
    {
    wsOut=Green[x];
    ledBar[wsOut] = 0xff;
    loadWS2803();
    delay(Delay);
    }
    for(int x= 12;x>0;x--)
    {
    wsOut=Green[x];
    ledBar[wsOut] = 0x00;
    loadWS2803();
    delay(Delay);
    }
    
   for(int x= 0;x<12;x++)
   {
   wsOut=Blue[x];
   ledBar[wsOut] = 0xff;
   loadWS2803();
   delay(Delay);
    } 
    for(int x= 12;x>0;x--)
   {
   wsOut=Blue[x];
   ledBar[wsOut] = 0x00;
   loadWS2803();
   delay(Delay); 
}
}
  void loadWS2803()
  {
   for (int wsOut = 0; wsOut < nLEDs; wsOut++)
  {
   
  //Serial.print(ledBar[wsOut]);
  
 shiftOut(ws2803_dataPin, ws2803_clockPin, MSBFIRST, ledBar[wsOut]);  }
  delayMicroseconds(600);

  }

Derek

You don't have an index 12, so Red12 is a random piece of memory. Your range is 0 - 11 and not 0 - 12

Yup,

    for(int x= 12;x>0;x--)
   {

should be

    for(int x= 11;x>=0;x--)  // or should that be x>0 so the next 0 to 11 doesn't start at the same point?
   {

I'm not going to go into the delays.

Ty guys got the arrays fixed. I'd ask what was wrong with the delays but you don't want to talk about them so I wont ;) . Still have 3 led out of the 12 not changing after they turn blue. I'll see if I can get past that tonight. It's been a lot of fun trying to figure out how to code the ws2803 with no library. Yesterday morning I knew nothing about Assigning multiple values to arrays and this morning I know a little....that's progress.

Thanks to all of you for the help, Derek