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: tiny LED cube - YouTube
Onions