tiny LED cube

Hi all!
Today I got my new arduino MEGA 1280. I decided I that needed a project
to make use of some extra ports that the board has. I quickly hooked it up
with the duemillanove and used some of the communication ports for a bit
of playing. Soon getting bored of that, I decided to build an LED cube. Sadly,
I only had about 12 LEDs lying around. Nothing puts me off though!

Pulling out all of the working ones, I soldered them together and wrote some code!

This is what I came up with:

//variables used:

int brightnessRead;//used to take the reading for the brightness
int brightness = 128;    //used for the brightness of the LEDs
int operation;     //this will control which LED operation occurs
int previousNumber;//used to try and prevent any operation repeating twice in a row
int delayValue = 200; //this determines how long a function lasts before moving on
int i;             //this will be used in for loops and array controlls


//pin declerations:

const int TFL = 3;    //top front, left LED
const int TFR = 4;    //top front, right LED
const int TRL = 5;    //top rear, left LED
const int TRR = 6;    //top rear, right LED
const int BFL = 7;    //bottom front, left LED
const int BFR = 8;    //bottom front, right LED
const int BRL = 9;    //bottom rear, left LED
const int BRR = 10;   //bottom rear, right LED
const int brightPin = A0;    //This pin will denote the brightness of the LEDs


//function prototypes for LED operations:

void top();
void bottom();
void left();
void right();
void corners1();
void corners2();
void all();


//arrays used:

char chaseTop[4] = {TFL, TFR, TRR, TRL};
char chaseBottom[4] = {BFL, BFR, BRR, BRL};
char chaseLeftSide[4] = {TFL, BFL, BRL, TRL};
char chaseRightSide[4] = {TFR, BFR, BRR, TRR};
char chaseCorners[4] = {TFR, TRL, BRL, BFR};
char chaseCorners1[4] = {TFL, TRR, BRR, BFL};




void setup(){
  Serial.begin(9600);
  //start serial communications to find out what it is doing
  
  //the LED brighness pin is an analog INPUT by default.
  //As we are using it as an analog input pin, we do not need to
  //declare it's use.
  //the LED brighness pin is an analog OUTPUT by default.
  //As we are using it as an analog output pin, we do not need to
  //declare it's use.
}


void loop(){
  loops:
  
  //TURN ALL PINS OFF
  analogWrite(TFL, 0);
  analogWrite(TFR, 0);
  analogWrite(TRL, 0);
  analogWrite(TRR, 0);
  analogWrite(BFL, 0);
  analogWrite(BFR, 0);
  analogWrite(BRL, 0);
  analogWrite(BRR, 0);
  
  delay(50);
  
 // brightnessRead = analogRead(brightPin);             //read the potentiometer value on analog pin 0
 // brightness = map(brightnessRead, 0, 1023, 0, 255);  //map the brighness value from the analog reading
                                   // range of 0 - 1023 to the brightness range of 0 - 255
  randomSeed(analogRead(A7));      //pick a random place to start making random numbers from
  operation = random(1, 13);       //create a random number with a max of 13 and a min of 1. 
                                   //This will be used for choosing the LED operation.
  if(operation == previousNumber){
    goto loops;
  }
  
  else{
  
  switch(operation){             
      //perform a different function depending on the value of operation
      case 1:    //if 'operation' equals 1,
      Serial.println("top()");
      void top();    //call function 'top'
      break;         //used to close switch statement

      case 2:    //if 'operation' equals 2,
      Serial.println("bottom()");
      void bottom();    //call function 'bottom'
      break;            //used to close switch statement
      
      case 3:          //same as above
      Serial.println("left()");
      void left();
      break;
      
      case 4:
      Serial.println("right()");
      void right();
      break;
      
      case 5:
      Serial.println("corners1()");
      void corners1();
      break;
      
      case 6:
      Serial.println("corners2()");
      void corners2();
      break;
      
      case 7:
      Serial.println("all()");
      void all();
      break;
      
      case 8:
      Serial.println("chaseTop()");
      for(int l = 0; l < 3; l++){
        //a for loop has been used here to chase more than once
        for(i = 0; i < 4; i++){
          //this for loop turns on each LED in the array chaseTop
          analogWrite(chaseTop[i], brightness);
          delay(100);
          analogWrite(chaseTop[i], 0);
        }
        i = 0;    //return the value of i back to it's original value
        delay(delayValue);
      }
      break;
      
      case 9:
      Serial.println("chaseBottom()");
      for(int l = 0; l < 3; l++){
        //a for loop has been used here to chase more than once
        for(i = 0; i < 4; i++){
          //this for loop turns on each LED in the array chaseBottom
          analogWrite(chaseBottom[i], brightness);
          delay(100);
          analogWrite(chaseBottom[i], 0);
        }
        i = 0;    //return the value of i back to it's original value
        delay(delayValue);
      }
      break;
      
      case 10:
      Serial.println("chaseLeftSide()");
      for(int l = 0; l < 3; l++){
        //a for loop has been used here to chase more than once
        for(i = 0; i < 4; i++){
          //this for loop turns on each LED in the array chaseLeftSide
          analogWrite(chaseLeftSide[i], brightness);
          delay(100);
          analogWrite(chaseLeftSide[i], 0);
        }
        i = 0;    //return the value of i back to it's original value
        delay(delayValue);
      }
      break;
      
      case 11:
      Serial.println("chaseRightSide()");
      for(int l = 0; l < 3; l++){
        //a for loop has been used here to chase more than once
        for(i = 0; i < 4; i++){
          //this for loop turns on each LED in the array chaseRightSide
          analogWrite(chaseRightSide[i], brightness);
          delay(100);
          analogWrite(chaseRightSide[i], 0);
        }
        i = 0;    //return the value of i back to it's original value
        delay(delayValue);
      }
      break;
      
      case 12:
      Serial.println("chaseCorners()");
      for(int l = 0; l < 3; l++){
        //a for loop has been used here to chase more than once
        for(i = 0; i < 4; i++){
          //this for loop turns on each LED in the array chaseCorners
          analogWrite(chaseCorners[i], brightness);
          delay(100);
          analogWrite(chaseCorners[i], 0);
        }
        i = 0;    //return the value of i back to it's original value
        delay(delayValue);
      }
      break;
      
      case 13:
      Serial.println("chaseCorners1()");
      for(int l = 0; l < 3; l++){
        //a for loop has been used here to chase more than once
        for(i = 0; i < 4; i++){
          //this for loop turns on each LED in the array chaseCorners1
          analogWrite(chaseCorners1[i], brightness);
          delay(100);
          analogWrite(chaseCorners1[i], 0);
        }
        i = 0;    //return the value of i back to it's original value
        delay(delayValue);
      }
      break;
  }
 }
}


void top(){
  analogWrite(TFR, brightness);    //turns on the top front right LED
  analogWrite(TRR, brightness);    //turns on the top back right LED
  analogWrite(TFL, brightness);    //turns on the top front left LED
  analogWrite(TRL, brightness);    //turns on the top back left LED
  delay(delayValue);
}
  
void bottom(){
  analogWrite(BFR, brightness);    //turns on the bottom front right LED
  analogWrite(BRR, brightness);    //turns on the bottom back right LED
  analogWrite(BFL, brightness);    //turns on the bottom front left LED
  analogWrite(BRL, brightness);    //turns on the bottom back left LED
  delay(delayValue);
}
  
void left(){
  analogWrite(TFL, brightness);    //turns on the top front left LED
  analogWrite(TRL, brightness);    //turns on the top back left LED
  analogWrite(BFL, brightness);    //turns on the top front left LED
  analogWrite(BRL, brightness);    //turns on the top back left LED
  delay(delayValue);
}  
  
void right(){
  analogWrite(TFR, brightness);    //turns on the top front right LED
  analogWrite(TRR, brightness);    //turns on the top back right LED
  analogWrite(BFR, brightness);    //turns on the top front right LED
  analogWrite(BRR, brightness);    //turns on the top back right LED
  delay(delayValue);
}  

void corners1(){
  analogWrite(TFL, brightness);    //turns on the top front left LED
  analogWrite(BRR, brightness);    //turns on the bottom back right LED    
  delay(delayValue);
}

void corners2(){
  analogWrite(TFR, brightness);    //turns on the top front right LED
  analogWrite(BRL, brightness);    //turns on the bottom back left LED    
  delay(delayValue);
}

void all(){
  analogWrite(TFL, brightness);
  analogWrite(TFR, brightness);
  analogWrite(TRL, brightness);
  analogWrite(TRR, brightness);
  analogWrite(BFL, brightness);
  analogWrite(BFR, brightness);
  analogWrite(BRL, brightness);
  analogWrite(BRR, brightness); 
  delay(delayValue);
}

The code was meant to be simple and easy to understand, but arrays could
be out of some peoples scope (slight pun there!). It still seems simple enough
though!


Although the pictures do not show it very well, it is a 3D cube!
A video can be seen on youtube: http://www.youtube.com/watch?v=1P4rjOF1FIw

Onions

I have revised all the code so it should work better. It also contains an LCD display, but no serial communication. Although I mentioned that the project was for the arduino mega, I forgot to say that it only works with it. I finnished the last post with only 8 characters remaining, so I could not include all the details Iwanted to. The LED address will look something like TRL or BFR. These are simple to understand: The first letter denotes the level - top or bottom. The second letter denotes the position - front or rear. The third letter denotes the side - left or right. A potentiometer could be connected to the analog pin A0 to control the brightness, or a number can be put in when you upload the program. All the LEDs are connected to PWM pins to allow this.

Those familliar with the liquidCrystal library will know that the initialization goes LiquidCrystal(rs, enable, d4, d5, d6, d7). In the new code we initialize it with: LiquidCrystal lcd(53, 51, 41, 39, 33, 31). So, RS, LCD pin 4, goes to digital pin 53, enable, LCD pin 6, goes to digital pin 51, d4, LCD pin 11, goes to pin 41, d5, lcd pin 12, goes to pin 39, d6, LCD pin 13, goes to digital pin 33 and d7, lcd pin 14, goes to digital pin 31. The LCD is then set up as shown in the following link, only with the new pin arrangement. http://arduino.cc/en/uploads/Tutorial/LCD_schem.png

The new code would not fit on this post, so it is on the next one. To get liquid crystal to work, you need the liquid crystal library. When I import it, it comes up with the two libraries you see. If you try it and it does not work, delete the libraries at the top of the code and go into sketck>>import library>>liquidCrystal. It shuld work.

The revised code:

#include <LiquidCrystal (2).h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(53, 51, 41, 39, 33, 31);

//variables used:

int brightness = 175;    //used for the brightness of the LEDs
int operation;     //this will control which LED operation occurs
int previousNumber;//used to try and prevent any operation repeating twice in a row
int delayValue = 500; //this determines how long a function lasts before moving on
int i;             //this will be used in for loops and array controlls


//pin declerations:

const int TFL = 3;    //top front, left LED
const int TFR = 4;    //top front, right LED
const int TRL = 5;    //top rear, left LED
const int TRR = 6;    //top rear, right LED
const int BFL = 7;    //bottom front, left LED
const int BFR = 8;    //bottom front, right LED
const int BRL = 9;    //bottom rear, left LED
const int BRR = 10;   //bottom rear, right LED


//function prototypes for LED operations:

void top();
void bottom();
void left();
void right();
void corners1();
void corners2();
void all();


//arrays used:

char chaseTop[4] = {TFL, TFR, TRR, TRL};
char chaseBottom[4] = {BFL, BFR, BRR, BRL};
char chaseLeftSide[4] = {TFL, BFL, BRL, TRL};
char chaseRightSide[4] = {TFR, BFR, BRR, TRR};
char chaseCorners[4] = {TFR, TRL, BRL, BFR};
char chaseCorners1[4] = {TFL, TRR, BRR, BFL};




void setup(){
  lcd.begin(16,2);
  //set up the LCD so we can see what it is doing
  
  //the LED brighness pin is an analog INPUT by default.
  //As we are using it as an analog input pin, we do not need to
  //declare it's use.
  //the LED brighness pin is an analog OUTPUT by default.
  //As we are using it as an analog output pin, we do not need to
  //declare it's use.
}


void loop(){
  loops:
  
  //TURN ALL PINS OFF
  analogWrite(TFL, 0);
  analogWrite(TFR, 0);
  analogWrite(TRL, 0);
  analogWrite(TRR, 0);
  analogWrite(BFL, 0);
  analogWrite(BFR, 0);
  analogWrite(BRL, 0);
  analogWrite(BRR, 0);
  
  delay(50);
  
 // brightnessRead = analogRead(brightPin);             //read the potentiometer value on analog pin 0
 // brightness = map(brightnessRead, 0, 1023, 0, 255);  //map the brighness value from the analog reading
                                   // range of 0 - 1023 to the brightness range of 0 - 255
  randomSeed(analogRead(A7));      //pick a random place to start making random numbers from
  operation = random(1, 13);       //create a random number with a max of 13 and a min of 1. 
                                   //This will be used for choosing the LED operation.
  if(operation == previousNumber){
    goto loops;
  }
  
  else{
    
    lcd.clear();    //eraxe the current contents of the LCD to display the new contents  
  switch(operation){             
      //perform a different function depending on the value of operation
      case 1:    //if 'operation' equals 1,
      lcd.print("top()");
      analogWrite(TFR, brightness);    //turns on the top front right LED
      analogWrite(TRR, brightness);    //turns on the top back right LED
      analogWrite(TFL, brightness);    //turns on the top front left LED
      analogWrite(TRL, brightness);    //turns on the top back left LED
      delay(delayValue);
      break;         //used to close switch statement

      case 2:    //if 'operation' equals 2,
      lcd.print("bottom()");
      analogWrite(BFR, brightness);    //turns on the bottom front right LED
      analogWrite(BRR, brightness);    //turns on the bottom back right LED
      analogWrite(BFL, brightness);    //turns on the bottom front left LED
      analogWrite(BRL, brightness);    //turns on the bottom back left LED
      delay(delayValue);
      break;            //used to close switch statement
      
      case 3:          //same as above
      lcd.print("left()");
      analogWrite(TFL, brightness);    //turns on the top front left LED
      analogWrite(TRL, brightness);    //turns on the top back left LED
      analogWrite(BFL, brightness);    //turns on the top front left LED
      analogWrite(BRL, brightness);    //turns on the top back left LED
      delay(delayValue);
      break;
      
      case 4:
      lcd.print("right()");
      analogWrite(TFR, brightness);    //turns on the top front right LED
      analogWrite(TRR, brightness);    //turns on the top back right LED
      analogWrite(BFR, brightness);    //turns on the top front right LED
      analogWrite(BRR, brightness);    //turns on the top back right LED
      delay(delayValue);
      break;
      
      case 5:
      lcd.print("corners1()");
      analogWrite(TFL, brightness);    //turns on the top front left LED
      analogWrite(BRR, brightness);    //turns on the bottom back right LED
      delay(delayValue);
      break;
      
      case 6:
      lcd.print("corners2()");      
      analogWrite(TFR, brightness);    //turns on the top front right LED
      analogWrite(BRL, brightness);    //turns on the bottom back left LED  
      delay(delayValue);
      break;
      
      case 7:
      lcd.print("all()");
      analogWrite(TFL, brightness);
      analogWrite(TFR, brightness);
      analogWrite(TRL, brightness);
      analogWrite(TRR, brightness);
      analogWrite(BFL, brightness);
      analogWrite(BFR, brightness);
      analogWrite(BRL, brightness);
      analogWrite(BRR, brightness); 
      delay(delayValue);
      break;
      
      case 8:
      lcd.print("chaseTop()");
      for(int l = 0; l < 3; l++){
        //a for loop has been used here to chase more than once
        for(i = 0; i < 4; i++){
          //this for loop turns on each LED in the array chaseTop
          analogWrite(chaseTop[i], brightness);
          delay(100);
          analogWrite(chaseTop[i], 0);
        }
        i = 0;    //return the value of i back to it's original value
      }
      break;
      
      case 9:
      lcd.print("chaseBottom()");
      for(int l = 0; l < 3; l++){
        //a for loop has been used here to chase more than once
        for(i = 0; i < 4; i++){
          //this for loop turns on each LED in the array chaseBottom
          analogWrite(chaseBottom[i], brightness);
          delay(100);
          analogWrite(chaseBottom[i], 0);
        }
        i = 0;    //return the value of i back to it's original value
      }
      break;
      
      case 10:
      lcd.print("chaseLeftSide()");
      for(int l = 0; l < 3; l++){
        //a for loop has been used here to chase more than once
        for(i = 0; i < 4; i++){
          //this for loop turns on each LED in the array chaseLeftSide
          analogWrite(chaseLeftSide[i], brightness);
          delay(100);
          analogWrite(chaseLeftSide[i], 0);
        }
        i = 0;    //return the value of i back to it's original value
      }
      break;
      
      case 11:
      lcd.print("chaseRightSide()");
      for(int l = 0; l < 3; l++){
        //a for loop has been used here to chase more than once
        for(i = 0; i < 4; i++){
          //this for loop turns on each LED in the array chaseRightSide
          analogWrite(chaseRightSide[i], brightness);
          delay(100);
          analogWrite(chaseRightSide[i], 0);
        }
        i = 0;    //return the value of i back to it's original value
      }
      break;
      
      case 12:
      lcd.print("chaseCorners()");
      for(int l = 0; l < 3; l++){
        //a for loop has been used here to chase more than once
        for(i = 0; i < 4; i++){
          //this for loop turns on each LED in the array chaseCorners
          analogWrite(chaseCorners[i], brightness);
          delay(100);
          analogWrite(chaseCorners[i], 0);
        }
        i = 0;    //return the value of i back to it's original value
      }
      break;
      
      case 13:
      lcd.print("chaseCorners1()");
      for(int l = 0; l < 3; l++){
        //a for loop has been used here to chase more than once
        for(i = 0; i < 4; i++){
          //this for loop turns on each LED in the array chaseCorners1
          analogWrite(chaseCorners1[i], brightness);
          delay(100);
          analogWrite(chaseCorners1[i], 0);
        }
        i = 0;    //return the value of i back to it's original value
      }
      break;
  }
 }
}