Introduction :
Arduino enthusiasts and electronics hobbyists often find themselves captivated by the mesmerizing dance of LEDs in various projects. One such project that can truly mesmerize your senses is the LED cube. In this blog post, we will explore a captivating Arduino code that brings an LED cube to life with a series of stunning animations and lighting patterns.
The LED Cube Project :
Before diving into the code, let's briefly introduce the LED cube project. An LED cube is a three-dimensional array of LEDs that can display intricate patterns and animations. This cube can be built using LEDs, wires, and an Arduino microcontroller. The cube's visual appeal lies in its ability to create dynamic lighting effects, making it a perfect addition to your desk, room, or as a conversation starter.
The pin configuration in your code is crucial for controlling the LED cube and defining how the LEDs are connected to your Arduino. The cube appears to be a 4x4x4 LED matrix, and your code defines the pins used to control both the layers and the columns. Here's an explanation of your pin configuration:
Global Variable Declarations :
int layer[4]={A3,A2,A1,A0}; //initializing and declaring led layers
int column[16]={13,12,11,10,9,8,7,6,5,4,3,2,1,0,A5,A4}; //initializing and declaring led rows
int time = 250;
* layer[4]: An array that defines the pins for the LED cube's layers.
* column[16]: An array that defines the pins for the LED cube's columns.
* Timing : Delays (delay()) are used to control the speed of animations. The time variable is used to set the delay duration between animations in the loop().
Rows :
int layer[4] = {A3, A2, A1, A0}; Its defined an array with 4 elements to represent the layers. The pins are connected to analog pins A3, A2, A1, and A0.
Columns :
int column[16] = {13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, A5, A4}; These are the pins used to control the columns or the individual LEDs within each layer of the cube. here defined an array with 16 elements to represent the columns.
The pins are connected to digital pins 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, A5, and A4.
The Code Breakdown :
The provided Arduino code is a masterpiece of creativity, designed to control an LED cube with 4 layers and 16 columns. Each part of the code serves a unique purpose in creating captivating animations. Let's break down some of the key functions:
1. Setup Function : This initializes the pins, sets them as outputs, and seeds the random number generator for creating random patterns.
void setup()
{
for(int i = 0; i<16; i++)
{
pinMode(column[i], OUTPUT); //setting rows to ouput
}
for(int i = 0; i<4; i++)
{
pinMode(layer[i], OUTPUT); //setting layers to output
}
randomSeed(analogRead(10)); //seeding random for random pattern
}
2. Loop Function : This is the main loop of your program that runs repeatedly. It calls a series of functions to create different lighting patterns and animations on the LED cube, with delays between each animation.
C++
void loop()
{
turnEverythingOff();
flickerOn();
turnEverythingOn();
delay(time);
turnOnAndOffAllByLayerUpAndDownNotTimed();
layerstompUpAndDown();
spiralInAndOut();
turnOnAndOffAllByColumnSideways();
delay(time);
aroundEdgeDown();
turnEverythingOff();
randomflicker();
randomRain();
diagonalRectangle();
goThroughAllLedsOneAtATime();
propeller();
spiralInAndOut();
flickerOff();
turnEverythingOff();
delay(2000);
}
3. TurnEverythingOff Function : This function turns off all LEDs by setting columns high and layers low.
//turn all off
void turnEverythingOff()
{
for(int i = 0; i<16; i++)
{
digitalWrite(column[i], 1);
}
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 0);
}
}
4. TurnEverythingOn Function : Opposite to the previous function, this one turns on all LEDs.
//turn all on
void turnEverythingOn()
{
for(int i = 0; i<16; i++)
{
digitalWrite(column[i], 0);
}
//turning on layers
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 1);
}
}
5. FlickerOn Function : Creates a captivating effect by gradually turning on LEDs with flickering intervals.
//flicker on
void flickerOn()
{
int i = 150;
while(i != 0)
{
turnEverythingOn();
delay(i);
turnEverythingOff();
delay(i);
i-= 5;
}
}
6. TurnOnAndOffAllByLayerUpAndDownNotTimed Function : This function artfully animates the cube by turning layers on and off while moving up and down.
//turn everything on and off by layer up and down NOT TIMED
void turnOnAndOffAllByLayerUpAndDownNotTimed()
{
int x = 75;
for(int i = 5; i != 0; i--) { turnEverythingOn(); for(int i = 4; i!=0; i--)
{
digitalWrite(layer[i-1], 0); delay(x);
}
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 1); delay(x);
}
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 0); delay(x);
}
for(int i = 4; i!=0; i--)
{
digitalWrite(layer[i-1], 1); delay(x);
}
}
}
7. TurnOnAndOffAllByColumnSideways Function : Similar to the previous function but animates by turning columns on and off sideways.
//turn everything on and off by column sideways
void turnOnAndOffAllByColumnSideways()
{ int x = 75;
turnEverythingOff();
//turn on layers
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 1);
}
for(int y = 0; y<3; y++)
{
//turn on 0-3
for(int i = 0; i<4; i++){ digitalWrite(column[i], 0); delay(x);}
//turn on 4-7
for(int i = 4; i<8; i++)
{ digitalWrite(column[i], 0); delay(x); }
//turn on 8-11
for(int i = 8; i<12; i++)
{digitalWrite(column[i], 0); delay(x);}
//turn on 12-15
for(int i = 12; i<16; i++)
{ digitalWrite(column[i], 0); delay(x);
}
//turn off 0-3
for(int i = 0; i<4; i++)
{ digitalWrite(column[i], 1); delay(x); }
//turn off 4-7
for(int i = 4; i<8; i++)
{ digitalWrite(column[i], 1); delay(x);
}
//turn off 8-11
for(int i = 8; i<12; i++)
{ digitalWrite(column[i], 1); delay(x); }
//turn off 12-15
for(int i = 12; i<16; i++)
{ digitalWrite(column[i], 1); delay(x); }
//turn on 12-15
for(int i = 12; i<16; i++) { digitalWrite(column[i], 0); delay(x);}
//turn on 8-11
for(int i = 8; i<12; i++)
{ digitalWrite(column[i], 0); delay(x);}
//turn on 4-7
for(int i = 4; i<8; i++)
{ digitalWrite(column[i], 0); delay(x); }
//turn on 0-3
for(int i = 0; i<4; i++)
{ digitalWrite(column[i], 0); delay(x); }
//turn off 12-15
for(int i = 12; i<16; i++)
{digitalWrite(column[i], 1); delay(x); }
//turn off 8-11
for(int i = 8; i<12; i++)
{ digitalWrite(column[i], 1); delay(x);}
//turn off 4-7
for(int i = 4; i<8; i++)
{digitalWrite(column[i], 1); delay(x); }
//turn off 0-3
for(int i = 0; i<4; i++)
{ digitalWrite(column[i], 1); delay(x);}}}
8. LayerstompUpAndDown Function : Delivers a "stomp" effect by alternately turning on and off a single layer while moving up and down.
//up and down single layer stomp
void layerstompUpAndDown()
{
int x = 75;
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 0);
}
for(int y = 0; y<5; y++)
{
for(int count = 0; count<1; count++)
{
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 1);
delay(x);
digitalWrite(layer[i], 0);
}
for(int i = 4; i !=0; i--)
{
digitalWrite(layer[i-1], 1);
delay(x);
digitalWrite(layer[i-1], 0);
}
}
for(int i = 0; i<4; i++)
{
digitalWrite(layer[i], 1);
delay(x);
}
for(int i = 4; i!=0; i--)
{
digitalWrite(layer[i-1], 0);
delay(x);
}
}
}
9. FlickerOff Function : Like the FlickerOn function, it gradually turns off LEDs with flickering intervals.
//flicker off
void flickerOff()
{
turnEverythingOn();
for(int i = 0; i!= 150; i+=5)
{
turnEverythingOff();
delay(i+50);
turnEverythingOn();
delay(i);
}
}
10. AroundEdgeDown Function : Creates a mesmerizing animation where LEDs sequentially light up around the edges and then move down.
//around edge of the cube down
void aroundEdgeDown()
{
for(int x = 200; x != 0; x -=50)
{
turnEverythingOff();
for(int i = 4; i != 0; i--)
{
digitalWrite(layer[i-1], 1);
digitalWrite(column[5], 0);
digitalWrite(column[6], 0);
digitalWrite(column[9], 0);
digitalWrite(column[10], 0);
digitalWrite(column[0], 0);
delay(x);
digitalWrite(column[0], 1);
digitalWrite(column[4], 0);
delay(x);
digitalWrite(column[4], 1);
digitalWrite(column[8], 0);
delay(x);
digitalWrite(column[8], 1);
digitalWrite(column[12], 0);
delay(x);
digitalWrite(column[12], 1);
digitalWrite(column[13], 0);
delay(x);
digitalWrite(column[13], 1);
digitalWrite(column[15], 0);
delay(x);
digitalWrite(column[15], 1);
digitalWrite(column[14], 0);
delay(x);
digitalWrite(column[14], 1);
digitalWrite(column[11], 0);
delay(x);
digitalWrite(column[11], 1);
digitalWrite(column[7], 0);
delay(x);
digitalWrite(column[7], 1);
digitalWrite(column[3], 0);
delay(x);
digitalWrite(column[3], 1);
digitalWrite(column[2], 0);
delay(x);
digitalWrite(column[2], 1);
digitalWrite(column[1], 0);
delay(x);
digitalWrite(column[1], 1);
}
}
}
11. RandomFlicker Function : Randomly flickers LEDs for a dynamic effect.
//random flicker
void randomflicker()
{
turnEverythingOff();
int x = 10;
for(int i = 0; i !=750; i+=2)
{
int randomLayer = random(0,4);
int randomColumn = random(0,16);
digitalWrite(layer[randomLayer], 1);
digitalWrite(column[randomColumn], 0);
delay(x);
digitalWrite(layer[randomLayer], 0);
digitalWrite(column[randomColumn], 1);
delay(x);
}
}
12. RandomRain Function : Simulates raindrops falling by randomly lighting LEDs in a column and moving down.
//random rain
void randomRain()
{
turnEverythingOff();
int x = 100;
for(int i = 0; i!=60; i+=2)
{
int randomColumn = random(0,16);
digitalWrite(column[randomColumn], 0);
digitalWrite(layer[0], 1);
delay(x+50);
digitalWrite(layer[0], 0);
digitalWrite(layer[1], 1);
delay(x);
digitalWrite(layer[1], 0);
digitalWrite(layer[2], 1);
delay(x);
digitalWrite(layer[2], 0);
digitalWrite(layer[3], 1);
delay(x+50);
digitalWrite(layer[3], 0);
digitalWrite(column[randomColumn], 1);
}
}
13. DiagonalRectangle Function : Generates a diagonal rectangle pattern that moves through the layers and columns.
//diagonal rectangle
void diagonalRectangle()
{
int x = 350;
turnEverythingOff();
for(int count = 0; count<5; count++)
{
//top left
for(int i = 0; i<8; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[3], 1);
digitalWrite(layer[2], 1);
delay(x);
turnEverythingOff();
//middle middle
for(int i = 4; i<12; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[1], 1);
digitalWrite(layer[2], 1);
delay(x);
turnEverythingOff();
//bottom right
for(int i = 8; i<16; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[0], 1);
digitalWrite(layer[1], 1);
delay(x);
turnEverythingOff();
//bottom middle
for(int i = 4; i<12; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[0], 1);
digitalWrite(layer[1], 1);
delay(x);
turnEverythingOff();
//bottom left
for(int i = 0; i<8; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[0], 1);
digitalWrite(layer[1], 1);
delay(x);
turnEverythingOff();
//middle middle
for(int i = 4; i<12; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[1], 1);
digitalWrite(layer[2], 1);
delay(x);
turnEverythingOff();
//top right
for(int i = 8; i<16; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[2], 1);
digitalWrite(layer[3], 1);
delay(x);
turnEverythingOff();
//top middle
for(int i = 4; i<12; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[2], 1);
digitalWrite(layer[3], 1);
delay(x);
turnEverythingOff();
}
//top left
for(int i = 0; i<8; i++)
{
digitalWrite(column[i], 0);
}
digitalWrite(layer[3], 1);
digitalWrite(layer[2], 1);
delay(x);
turnEverythingOff();
}
14. Propeller Function : Crafts a propeller-like animation with LEDs in a specific pattern.
//propeller
void propeller()
{
turnEverythingOff();
int x = 90;
for(int y = 4; y>0; y--)
{
for(int i = 0; i<6; i++)
{
//turn on layer
digitalWrite(layer[y-1], 1);
//a1
turnColumnsOff();
digitalWrite(column[0], 0);
digitalWrite(column[5], 0);
digitalWrite(column[10], 0);
digitalWrite(column[15], 0);
delay(x);
//b1
turnColumnsOff();
digitalWrite(column[4], 0);
digitalWrite(column[5], 0);
digitalWrite(column[10], 0);
digitalWrite(column[11], 0);
delay(x);
//c1
turnColumnsOff();
digitalWrite(column[6], 0);
digitalWrite(column[7], 0);
digitalWrite(column[8], 0);
digitalWrite(column[9], 0);
delay(x);
//d1
turnColumnsOff();
digitalWrite(column[3], 0);
digitalWrite(column[6], 0);
digitalWrite(column[9], 0);
digitalWrite(column[12], 0);
delay(x);
//d2
turnColumnsOff();
digitalWrite(column[2], 0);
digitalWrite(column[6], 0);
digitalWrite(column[9], 0);
digitalWrite(column[13], 0);
delay(x);
//d3
turnColumnsOff();
digitalWrite(column[1], 0);
digitalWrite(column[5], 0);
digitalWrite(column[10], 0);
digitalWrite(column[14], 0);
delay(x);
}
}
//d4
turnColumnsOff();
digitalWrite(column[0], 0);
digitalWrite(column[5], 0);
digitalWrite(column[10], 0);
digitalWrite(column[15], 0);
delay(x);
}
15. SpiralInAndOut Function : Creates an enchanting spiral animation, both inward and outward.
//spiral in and out
void spiralInAndOut()
{
turnEverythingOn();
int x = 60;
for(int i = 0; i<6; i++)
{
//spiral in clockwise
digitalWrite(column[0], 1);
delay(x);
digitalWrite(column[1], 1);
delay(x);
digitalWrite(column[2], 1);
delay(x);
digitalWrite(column[3], 1);
delay(x);
digitalWrite(column[7], 1);
delay(x);
digitalWrite(column[11], 1);
delay(x);
digitalWrite(column[15], 1);
delay(x);
digitalWrite(column[14], 1);
delay(x);
digitalWrite(column[13], 1);
delay(x);
digitalWrite(column[12], 1);
delay(x);
digitalWrite(column[8], 1);
delay(x);
digitalWrite(column[4], 1);
delay(x);
digitalWrite(column[5], 1);
delay(x);
digitalWrite(column[6], 1);
delay(x);
digitalWrite(column[10], 1);
delay(x);
digitalWrite(column[9], 1);
delay(x);
//spiral out counter clockwise
digitalWrite(column[9], 0);
delay(x);
digitalWrite(column[10], 0);
delay(x);
digitalWrite(column[6], 0);
delay(x);
digitalWrite(column[5], 0);
delay(x);
digitalWrite(column[4], 0);
delay(x);
digitalWrite(column[8], 0);
delay(x);
digitalWrite(column[12], 0);
delay(x);
digitalWrite(column[13], 0);
delay(x);
digitalWrite(column[14], 0);
delay(x);
digitalWrite(column[15], 0);
delay(x);
digitalWrite(column[11], 0);
delay(x);
digitalWrite(column[7], 0);
delay(x);
digitalWrite(column[3], 0);
delay(x);
digitalWrite(column[2], 0);
delay(x);
digitalWrite(column[1], 0);
delay(x);
digitalWrite(column[0], 0);
delay(x);
//spiral in counter clock wise
digitalWrite(column[0], 1);
delay(x);
digitalWrite(column[4], 1);
delay(x);
digitalWrite(column[8], 1);
delay(x);
digitalWrite(column[12], 1);
delay(x);
digitalWrite(column[13], 1);
delay(x);
digitalWrite(column[14], 1);
delay(x);
digitalWrite(column[15], 1);
delay(x);
digitalWrite(column[11], 1);
delay(x);
digitalWrite(column[7], 1);
delay(x);
digitalWrite(column[3], 1);
delay(x);
digitalWrite(column[2], 1);
delay(x);
digitalWrite(column[1], 1);
delay(x);
digitalWrite(column[5], 1);
delay(x);
digitalWrite(column[9], 1);
delay(x);
digitalWrite(column[10], 1);
delay(x);
digitalWrite(column[6], 1);
delay(x);
//spiral out clock wise
digitalWrite(column[6], 0);
delay(x);
digitalWrite(column[10], 0);
delay(x);
digitalWrite(column[9], 0);
delay(x);
digitalWrite(column[5], 0);
delay(x);
digitalWrite(column[1], 0);
delay(x);
digitalWrite(column[2], 0);
delay(x);
digitalWrite(column[3], 0);
delay(x);
digitalWrite(column[7], 0);
delay(x);
digitalWrite(column[11], 0);
delay(x);
digitalWrite(column[15], 0);
delay(x);
digitalWrite(column[14], 0);
delay(x);
digitalWrite(column[13], 0);
delay(x);
digitalWrite(column[12], 0);
delay(x);
digitalWrite(column[8], 0);
delay(x);
digitalWrite(column[4], 0);
delay(x);
digitalWrite(column[0], 0);
delay(x);
}
}
16. GoThroughAllLedsOneAtATime Function : Progressively lights up each LED, traversing all layers and columns.
//go through all leds one at a time
void goThroughAllLedsOneAtATime()
{
int x = 15;
turnEverythingOff();
for(int y = 0; y<5; y++)
{
//0-3
for(int count = 4; count != 0; count--)
{
digitalWrite(layer[count-1], 1);
for(int i = 0; i<4; i++)
{
digitalWrite(column[i], 0);
delay(x);
digitalWrite(column[i], 1);
delay(x);
}
digitalWrite(layer[count-1], 0);
}
//4-7
for(int count = 0; count < 4; count++)
{
digitalWrite(layer[count], 1);
for(int i = 4; i<8; i++)
{
digitalWrite(column[i], 0);
delay(x);
digitalWrite(column[i], 1);
delay(x);
}
digitalWrite(layer[count], 0);
}
//8-11
for(int count = 4; count != 0; count--)
{
digitalWrite(layer[count-1], 1);
for(int i = 8; i<12; i++)
{
digitalWrite(column[i], 0);
delay(x);
digitalWrite(column[i], 1);
delay(x);
}
digitalWrite(layer[count-1], 0);
}
//12-15
for(int count = 0; count < 4; count++)
{
digitalWrite(layer[count], 1);
for(int i = 12; i<16; i++)
{ digitalWrite(column[i], 0); delay(x); digitalWrite(column[i], 1); delay(x); } digitalWrite(layer[count], 0); } }
}
Output :
Conclusion :
The Arduino code for controlling an LED cube showcased in this blog is a work of art that demonstrates the creative potential of electronics and programming. With this code, you can breathe life into your LED cube project, turning it into a mesmerizing display of light and motion. Whether you're a seasoned Arduino enthusiast or just starting with electronics, experimenting with LED cubes and animations like these is a rewarding and visually stunning journey. This project combines artistry with technology, offering endless possibilities for customization and creativity. So, gather your components, upload the code, and let your LED cube become a captivating centerpiece in your electronics repertoire.