Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumProgramming Questions & HelpSyntax › two demensional Array

Page Index Toggle Pages: 1
two demensional Array (Read 11154 times)
11/26/08 at 19:04:31

Vaccumdiode2   Offline
Full Member
chicago, IL USA

Posts: 120
***
 
Hello everyone. 
           Can wiring make a two demensional Array?  I'm play around with some three color LEDs and want to make a flashing star for my Chirstmas Tree.  the star I found has 11 bulbs so I have 11 LEDs and 33 output pins  with about 7 possible colors for Each LED.
            I want to set up several patterns and use a random number generator to pick which one to run and for how long.
            Any Ideas Please let me.

Vaccumdiode Cool
 
IP Logged
 
Reply #1 - 04/18/09 at 16:55:08

rer   Offline
YaBB Newbies

Posts: 3
*
 
If it can't, you can create the second dimension simply by using a uniform offset, that is, the first 11 elements of the array will be the first block of the second dimension, the next 11 elements will be the second block of dimension 2, etc.

rer
 
IP Logged
 
Reply #2 - 04/18/09 at 19:45:29

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
a two dimensional array can be declared as this:

int H = 10;
int W = 10;
byte displayPicture[H][W];

a 3-D array like this:

byte bitmaps[8][8][16];

 
IP Logged
 
Reply #3 - 11/17/09 at 15:25:28

Vaccumdiode2   Offline
Full Member
chicago, IL USA

Posts: 120
***
 
Hello Everyone,

          I'm Back on the Christmas Star project. 

          I still have not master an understand of multidimensional arrays.

I have 11 RGB LEDs.  Arranged on an old Christmas tree topper, five are the outer star, five are an inner star and one in the center.  Each RGB LED has eight states, all off, red, green, blue, redgreen, redblue, greenblue, and redgreenblue.

I have an eleven by eight matrix of 88 possible states.
I have 33 digital out pins.

I was think of setting up 33 Char string arrays. Using HIGH(off) and LOW(on).  then using a variable for my digitalWrite State.

I'm Try to think of a way to make it easier to write patterns.   My test program is a simple series digitalwrites just using red, Green and Blue.  It is getting pretty long 11 digitalwrites per State set and 5 sets total.

Any Ideas would be greatly appreciated

Thank you, Vaccumdiode



 
IP Logged
 
Reply #4 - 11/17/09 at 21:25:01

Vaccumdiode2   Offline
Full Member
chicago, IL USA

Posts: 120
***
 
Hello,

I set up this one to randomly blink one of them.

assigning the pins and setting up the pinmodes took a couple of page when you print them out.

Random blinker

rand = random(32);  // Sets x to a random number between 0 and 32
rand = rand+1;  //  converts the number to, 1 to 33

switch(rand) {

  case 1:
    digitalWrite(O1R, LOW);   // Outer#1 Red ON
    delay(Blinkdelay03);
     digitalWrite(O1R, HIGH);   // Outer#1 Red OFF
    break;

case 2:
   digitalWrite(O1G, LOW);   // Outer#1 Green ON
    delay(Blinkdelay03);
     digitalWrite(O1G, HIGH);   // Outer#1 Green OFF
    break;

case 3:
   digitalWrite(O1B, LOW);   // Outer#1 Blue ON
    delay(Blinkdelay03);
     digitalWrite(O1B, HIGH);   // Outer#1 Blue OFF
    break;
--------------
case 31:
    digitalWrite(I5R, LOW);   // Inner#5 Red ON
    delay(Blinkdelay03);
     digitalWrite(I5R, HIGH);   // Inner#5 Red OFF
    break;

case 32:
   digitalWrite(I5G, LOW);   // Inner#5 Green ON
    delay(Blinkdelay03);
     digitalWrite(I5G, HIGH);   // Inner#5 Green OFF
    break;

case 33:
   digitalWrite(I5B, LOW);   // Inner#5 Blue ON
    delay(Blinkdelay03);
     digitalWrite(I5B, HIGH);   // Inner#5 Blue OFF
    break;
}

Vaccumdiode
 
IP Logged
 
Reply #5 - 11/18/09 at 00:20:23

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
Ok, I'm not sure if this is what you're looking for, but it turns on each LED in all possible colors, I haven't tested it yet, but please let me now.

Quote:
int
leds[3*11] = {
  0, 1, 2,   
// each 3 numbers are the pins used for RGB

  3, 4, 5,    
// on each LED

  6, 7, 8,
  9, 10, 11,
  12, 13, 14,
  15, 16, 17,
  18, 19, 20,
  21, 22, 23,
  24, 25, 26,
  27, 28, 29,
  30, 31, 34 }; 
// skip 32 and 33 used for Serial


void
setup
() {
  
Serial
.
begin
(9600);

  
// set all used pins as OUTPUTS

  
for
(
int
i=0; i<33; i++) {
    
pinMode
(i,
OUTPUT
);
  }

  
// just for print the BINARY representation of numbers from 0-7

  
/*

   for(byte i = 0; i<8; i++) {

   Serial.print(i, BIN);

   Serial.print(" ");

   }

   */

}

void
loop
() {
  
for
(
int
theLed=0; theLed<11; theLed++) {
    
for
(
byte
color=0; color<8; color++) {
      ledOn(theLed, color);
      
delay
(100);
    }
  }
}

void
ledOn(
int
led,
byte
RGB) {
  
digitalWrite
(leds[3*led],
bitRead
(RGB, 0)?
HIGH
:
LOW
);
  
digitalWrite
(leds[3*led+1],
bitRead
(RGB, 1)?
HIGH
:
LOW
);
  
digitalWrite
(leds[3*led+2],
bitRead
(RGB, 2)?
HIGH
:
LOW
);
}

 
IP Logged
 
Reply #6 - 11/18/09 at 00:38:22

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
Another possible cycle in the loop() method turning on all LEDs in the same color at a time:

Quote:
byte
color = 0;
void
loop
() {
  
for
(
int
theLed=0; theLed<11; theLed++) {
    ledOn(theLed, color);
  }
  
delay
(100);
  color = (color+1)%8;
}

 
IP Logged
 
Reply #7 - 11/18/09 at 16:24:29

Vaccumdiode2   Offline
Full Member
chicago, IL USA

Posts: 120
***
 
Thanks H,

            I have four patterns now and Ideas for two more.   I finish the hardware last night and update my wiring version to 22.  tonight I'll get all the pins assigned.  I still need to add a decorative plate behind the star to hide the hardware and a pieces of PVC pipe for mounting onto the tree and I'm ready.  I'll keep you posted.

Vaccumdiode
 
IP Logged
 
Reply #8 - 11/23/09 at 17:15:47

Vaccumdiode2   Offline
Full Member
chicago, IL USA

Posts: 120
***
 
Hello all,

Barragan,
                     I Got the Hardware working!  I used My Random program and my Star blinker program.  and I fused them together.  It looks great. 

I tried getting the one below, you gave me, to work.  But my knowlegde of C type commands is more limited than I thought. 

were you using the bitwise functions?  Or something else?

I had to change the pin number to match my hardware set up.

I when thought it and fixed the simple syntex errors the commpiler found.  But I could not get it to fully compile. 

Any Ideas would be great!  Tonight I'm going to video it and try and get it posted on YouTube.  I'll Post the link.
            

int leds[3*11] = {
  0, 1, 2,         
  5, 6, 7,         
  8, 9, 10,
  13, 14, 15,
  16, 17, 18,
  21, 22, 23,
  24, 25, 26,
  29, 30, 31,
  37, 38, 39,
  40, 41, 42,
  45, 46, 47 };  //skip 32 and 33 used for Serial

void setup() {
  Serial.begin(9600);

  // set all used pins as OUTPUTS
  for(inti=0; i<33; i++) {
    pinMode(i, OUTPUT);
  }

  // just for print the BINARY representation of numbers from 0-7
  /*
   for(byte i = 0; i<8; i++) {
   Serial.print(i, BIN);
   Serial.print(" ");
   }
   */
}

void loop() {
  for(int theLed=0; theLed<11; theLed++) {
    for(bytecolor=0; color<8; color++) {
      ledOn(theLed, color);
      delay(100);
    }
  }
}

void ledOn(int led, byteRGB) {
  digitalWrite(leds[3*led], bitRead(RGB, 0)? HIGH: LOW);
  digitalWrite(leds[3*led+1], bitRead(RGB, 1)? HIGH: LOW);
  digitalWrite(leds[3*led+2], bitRead(RGB, 2)? HIGH: LOW);
}

Vaccumdiode
 
IP Logged
 
Reply #9 - 11/23/09 at 17:24:33

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
you have a few syntax errors, here it is fixed:

int leds[3*11] = {
  0, 1, 2,         
  5, 6, 7,         
  8, 9, 10,
  13, 14, 15,
  16, 17, 18,
  21, 22, 23,
  24, 25, 26,
  29, 30, 31,
  37, 38, 39,
  40, 41, 42,
  45, 46, 47 };  //skip 32 and 33 used for Serial

void setup() {
  Serial.begin(9600);

  // set all used pins as OUTPUTS
  for(int i=0; i<33; i++) {
    pinMode(i, OUTPUT);
  }

  // just for print the BINARY representation of numbers from 0-7
  /*
   for(byte i = 0; i<8; i++) {
   Serial.print(i, BIN);
   Serial.print(" ");
   }
   */
}

void loop() {
  for(int theLed=0; theLed<11; theLed++) {
    for(byte color=0; color<8; color++) {
      ledOn(theLed, color);
      delay(100);
    }
  }
}

void ledOn(int led, byte RGB) {
  // if bit 0 is 1, turn on RED pin
  digitalWrite(leds[3*led], bitRead(RGB, 0)? HIGH: LOW);
  // if bit 1 is 1, turn on GREEN pin
  digitalWrite(leds[3*led+1], bitRead(RGB, 1)? HIGH: LOW);
  // if bit 2 is 1, turn on BLUE pin
  digitalWrite(leds[3*led+2], bitRead(RGB, 2)? HIGH: LOW);
}
 
IP Logged
 
Reply #10 - 11/23/09 at 19:14:02

Vaccumdiode2   Offline
Full Member
chicago, IL USA

Posts: 120
***
 
Barragan,

         Thank you for the quick help.  I'll try it tonight.   If all goes well then I'll fuse it into the main program with the others.  Also I'll try out that the second program.

         My father is an electronic nut from back in the late 1930's and he thinks that the star is better than any tree topper he has ever seen.   He is working a Bingo program and fighting with visual basic.  Last night I turn him onto Processing.  He is 82 years old.

Thank you,
Vaccumdiode
 
IP Logged
 
Reply #11 - 11/24/09 at 04:00:49

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
wow, it is great he´s coding, let me know if this works well for you. best h.
 
IP Logged
 
Reply #12 - 11/24/09 at 16:51:43

Vaccumdiode2   Offline
Full Member
chicago, IL USA

Posts: 120
***
 
Barragan,

                I got it to work last night.  I had to expand the pin int loop to 47 and slow it down a little.  but it works great and looks cool. 

                 I've got tomorrow off work, so i'm going to work on fusing this into my main program and get a video posted on YouTube.  I'll post a link.


int leds[3*11] = {
  0, 1, 2,         
  5, 6, 7,         
  8, 9, 10,
  13, 14, 15,
  16, 17, 18,
  21, 22, 23,
  24, 25, 26,
  29, 30, 31,
  37, 38, 39,
  40, 41, 42,
  45, 46, 47 };  //skip 32 and 33 used for Serial

void setup() {
  Serial.begin(9600);

  // set all used pins as OUTPUTS
  for(int i=0; i<47[/color]; i++) {
    pinMode(i, OUTPUT);
  }

  // just for print the BINARY representation of numbers from 0-7
  /*
   for(byte i = 0; i<8; i++) {
   Serial.print(i, BIN);
   Serial.print(" ");
   }
   */
}

void loop() {
  for(int theLed=0; theLed<11; theLed++) {
    for(byte color=0; color<8; color++) {
      ledOn(theLed, color);
      delay(300);
    }
  }
}

void ledOn(int led, byte RGB) {
  // if bit 0 is 1, turn on RED pin
  digitalWrite(leds[3*led], bitRead(RGB, 0)? HIGH: LOW);
  // if bit 1 is 1, turn on GREEN pin
  digitalWrite(leds[3*led+1], bitRead(RGB, 1)? HIGH: LOW);
  // if bit 2 is 1, turn on BLUE pin
  digitalWrite(leds[3*led+2], bitRead(RGB, 2)? HIGH: LOW);
}

Vaccumdiode
 
IP Logged
 
Page Index Toggle Pages: 1