Pages: [1]   Go Down
Author Topic: 3x3x3 LedCube; shorten POV code  (Read 1778 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I've build a 3x3x3 led cube with the multiplexing technique so i only have 13 pins to control it. So far so good.

I use the next code to scroll the horizontal rows up and down (i don't know how to explain is clearly)
Code:
// Scripted by Stealth0113 (stealthaerox@gmail.com)

//declare the pins for the 3x3x3 Ledcube
int colpins[] = {29,30,31,32,33,34,35,36,37}; //ColumnPins
int lvlpins[] = {11,12,13}; //LevelPins
int count;

void setup() {               
  for (count=0;count<9;count++) {
    pinMode(colpins[count], OUTPUT); // Set al the columns as output
    digitalWrite(colpins[count], LOW);} //ensure that they are low
  for (count=0;count<4;count++) {
    pinMode(lvlpins[count], OUTPUT); // Set al the levels as output
    digitalWrite(lvlpins[count], LOW);} //ensure that they are low
}

void loop() {
  for (int i=0; i<10; i++){
  digitalWrite(11, HIGH);
  digitalWrite(29, HIGH);
  digitalWrite(30, HIGH);
  digitalWrite(31, HIGH);
  delay(5);
  digitalWrite(11, LOW);
  digitalWrite(29, LOW);
  digitalWrite(30, LOW);
  digitalWrite(31, LOW);
  digitalWrite(13, HIGH);
  digitalWrite(35, HIGH);
  digitalWrite(36, HIGH);
  digitalWrite(37, HIGH);
  delay(5);
  digitalWrite(13, LOW);
  digitalWrite(35, LOW);
  digitalWrite(36, LOW);
  digitalWrite(37, LOW);
  delay(5);
  }
 
  for (int i=0; i<10; i++){
  digitalWrite(11, HIGH);
  digitalWrite(13, HIGH);
  digitalWrite(32, HIGH);
  digitalWrite(33, HIGH);
  digitalWrite(34, HIGH);
  delay(5);
  digitalWrite(11, LOW);
  digitalWrite(13, LOW);
  digitalWrite(32, LOW);
  digitalWrite(33, LOW);
  digitalWrite(34, LOW);
  delay(5);
  }
 
  for (int i=0; i<10; i++){
  digitalWrite(13, HIGH);
  digitalWrite(29, HIGH);
  digitalWrite(30, HIGH);
  digitalWrite(31, HIGH);
  delay(5);
  digitalWrite(13, LOW);
  digitalWrite(29, LOW);
  digitalWrite(30, LOW);
  digitalWrite(31, LOW);
  digitalWrite(11, HIGH);
  digitalWrite(35, HIGH);
  digitalWrite(36, HIGH);
  digitalWrite(37, HIGH);
  delay(5);
  digitalWrite(11, LOW);
  digitalWrite(35, LOW);
  digitalWrite(36, LOW);
  digitalWrite(37, LOW);
  delay(5);
  }
 
  for (int i=0; i<10; i++){
  digitalWrite(12, HIGH);
  digitalWrite(29, HIGH);
  digitalWrite(30, HIGH);
  digitalWrite(31, HIGH);
  digitalWrite(35, HIGH);
  digitalWrite(36, HIGH);
  digitalWrite(37, HIGH);
  delay(5);
  digitalWrite(12, LOW);
  digitalWrite(29, LOW);
  digitalWrite(30, LOW);
  digitalWrite(31, LOW);
  digitalWrite(35, LOW);
  digitalWrite(36, LOW);
  digitalWrite(37, LOW);
  delay(5);
  }
}

As you can see the code is not very short for such a simple thing. I've seen that some people use hex or binary code's to control leds on rotating led strips. I think this is the best way to manage the leds on my cube, but i don't know how to apply it on my cube.
Logged

Watch my stealth-bot @ let's make robots

Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Without a good explanation, it will be hard to help.  It also depends on how you have you multiplexing set up.  I built a 4x4x4 and 5x5x5 cube and control it using multiplexing. http://arduino.cc/forum/index.php/topic,74574.msg562027.html#msg562027

Are you trying to light an outside square on one level then shift it up and down? 
Logged

St. Louis
Offline Offline
Newbie
*
Karma: 0
Posts: 29
SIUE-ECE
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm by no means an expert on this sort of things but that doesn't look to bad. Sometimes the best way to approach something simple like this is to just pound it out. I do wish that arduino had some way to declare something like a port and control it saying something like
Code:
port selectPort{3,4,5}//declares a port over pins 3,4,5
.
.
.
void loop(){
selectPort{001};
selectPort{101};
...
}
but we don't have that ability and the predefined ports aren't a very good thing to mess with 90% of the time you mess with pins you dont mean to
Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Without a good explanation, it will be hard to help.  It also depends on how you have you multiplexing set up. 

The 9 columns are the anodes of the leds. The 3 levels have a common cathode and can be turn on/off with transistors so i can use HIGH an LOW to control them.



Are you trying to light an outside square on one level then shift it up and down? 

No, i try to explain:
- Front down 3 leds, the 3 horizontal in the middle and the back-top 3 leds.
- The middle 9 vertical leds.
- Back down 3 leds, the 3 horizontal in the middle and the front-top 3 leds.
- The middele 9 horizontal leds.

Something like this: / ->|-> \ -> --

I didn't use shift-registers, but i think that i'm gonna use them next time. Because i can use binary numbers to set the colums/levels HIGH and LOW. This is so much more easy than the tons of digitalwrite-lines that i have to use now.

Logged

Watch my stealth-bot @ let's make robots

Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For starters, you should shift your code over to use interrupts and store your cube data in an array.  You will get much better performance this way and it will be easier to program. 

The array will be an image of the cube in memory and you just change the array values for what you want to display.  The interrupt routine runs on its own and continuously cycles through the array to turn on and off the LEDs.  You can then write your routines using X & Y type addressing where X could represent a level of your cube and Y could represent a row on that level.  The value of Y would set which of the 3 leds on that row would be on or off (ie. 000 = all off 010 = middle on and 111 = all on)

Here is the code I used with several effects for my 4x4x4 cube: http://www.2shared.com/file/jdgOMlr5/Cube_4x4x41.html

Within the code you will see some arrays like this:
Code:
const unsigned char spinning_line[6][2] PROGMEM =
{
  { 0x84, 0x21 },
  { 0x0c, 0x30 },
  { 0x03, 0xc0 },
  { 0x12, 0x48 },
  { 0x22, 0x44 },
  { 0x44, 0x22 },
};
These set the LEDs on or off using bits mentioned above (0=off, 1=on).  Each Hex number is 8 bits and represents 2 rows of 4 leds.  By adapting the code to use your 3x3x3 cube,  you then just load up the cube array with these values and your cube will display the pattern.  If you don't know much about programming like this, it will be a bit of a learning curve.  It certainly was with me.

You could also try to use the code for the 3x3x3 cube here: http://arduino.cc/playground/Main/LEDCube3x3
It doesn't use interrupts but it does use bits to determine on and off states.
Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you so much Cranium! I'm gonna test it later en post my results with it.
Logged

Watch my stealth-bot @ let's make robots

Pages: [1]   Go Up
Jump to: