Skip to content

Instantly share code, notes, and snippets.

@Stefan-Xp

Stefan-Xp/cube3x3x3.ino

Forked from davedarko/cube3x3x3.ino
Last active Aug 29, 2015
Embed
What would you like to do?
// 3x3x3 LED Cube Control Programm
//
// Version 0.0 2014-08-18 DaveDarko
// Version 0.5 2014-08-22 Stefan-Xp
// Version 0.6 2014-08-23 Stefan-Xp - Improved Precompiler statements
// Version 0.7 2014-08-24 Stefan-Xp - Added Animation_New function - 50 shades of green
// Version 0.8 2014-08-24 Stefan-Xp - Added Animation_Star
//
// ATMEL ATMEGA16 Pinning
//
// +---\/---+
// (D 0) PB0 1| |40 PA0 (AI 0 / D24)
// (D 1) PB1 2| |39 PA1 (AI 1 / D25)
// INT2 (D 2) PB2 3| |38 PA2 (AI 2 / D26)
// PWM (D 3) PB3 4| |37 PA3 (AI 3 / D27)
// /SS (D 4) PB4 5| |36 PA4 (AI 4 / D28)
// MOSI (D 5) PB5 6| |35 PA5 (AI 5 / D29)
// /MISO (D 6) PB6 7| |34 PA6 (AI 6 / D30)
// /SCK (D 7) PB7 8| |33 PA7 (AI 7 / D31)
// RST 9| |32 AREF
// VCC 10| |31 GND
// GND 11| |30 AVCC
// XTAL2 12| |29 PC7 (D 23)
// XTAL1 13| |28 PC6 (D 22)
// RX0 (D 8) PD0 14| |27 PC5 (D 21) TDI
// TX0 (D 9) PD1 15| |26 PC4 (D 20) TDO
// RX1/INT0 (D 10) PD2 16| |25 PC3 (D 19) TMS
// TX1/INT1 (D 11) PD3 17| |24 PC2 (D 18) TCK
// PWM (D 12) PD4 18| |23 PC1 (D 17) SDA
// PWM (D 13) PD5 19| |22 PC0 (D 16) SCL
// (D 14) PD6 20| |21 PD7 (D 15) PWM
// +--------+
//
// BOF preprocessor bug prevent - insert me on top of your arduino-code
// From: http://www.a-control.de/arduino-fehler/?lang=en
#if 1
__asm volatile ("nop");
#endif
// Global Defines
#define C_OFF LOW
#define C_ACT HIGH
// Select Pin Settings!
//#define ARDUINO_UNO_HARDWARE TRUE
// Pin Setup:
// A B C Layer 3 (highest)
// D E F Layer 2
// G h I Layer 1 (lowest)
#ifdef ARDUINO_UNO_HARDWARE
// Arduino Uno / DaveDarkos Build
#define PIN_A 5
#define PIN_B 6
#define PIN_C 7
#define PIN_D 8
#define PIN_E 9
#define PIN_F 10
#define PIN_G 11
#define PIN_H 12
#define PIN_I 13
#define PIN_1 2
#define PIN_2 3
#define PIN_3 4
#else
// Atmega 16 Built Pins (ToDo!)
#define PIN_A 18
#define PIN_B 20
#define PIN_C 22
#define PIN_D 19
#define PIN_E 21
#define PIN_F 23
#define PIN_G 29
#define PIN_H 30
#define PIN_I 31
#define PIN_1 26
#define PIN_2 27
#define PIN_3 28
#endif
byte anodes[] = {
PIN_A,
PIN_B,
PIN_C,
PIN_D,
PIN_E,
PIN_F,
PIN_G,
PIN_H,
PIN_I,
};
byte cathodes[] = {
PIN_1,
PIN_2,
PIN_3
};
byte mode;
//const byte frames = 21 ;
byte animation_scanner[21][3][9] = {
{/* Frame 1*/ {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0} },
{/* Frame 2*/ {20, 20, 20, 0, 0, 0, 0, 0, 0}, {20, 20, 20, 0, 0, 0, 0, 0, 0}, {20, 20, 20, 0, 0, 0, 0, 0, 0} },
{/* Frame 3*/ {40, 40, 40, 0, 0, 0, 0, 0, 0}, {40, 40, 40, 0, 0, 0, 0, 0, 0}, {40, 40, 40, 0, 0, 0, 0, 0, 0} },
{/* Frame 4*/ {50, 50, 50, 0, 0, 0, 0, 0, 0}, {50, 50, 50, 0, 0, 0, 0, 0, 0}, {50, 50, 50, 0, 0, 0, 0, 0, 0} },
{/* Frame 5*/ {40, 40, 40, 0, 0, 0, 0, 0, 0}, {40, 40, 40, 0, 0, 0, 0, 0, 0}, {40, 40, 40, 0, 0, 0, 0, 0, 0} },
{/* Frame 6*/ {20, 20, 20, 0, 0, 0, 0, 0, 0}, {20, 20, 20, 0, 0, 0, 0, 0, 0}, {20, 20, 20, 0, 0, 0, 0, 0, 0} },
{/* Frame 7*/ {0, 0, 0, 20, 20, 20, 0, 0, 0}, {0, 0, 0, 20, 20, 20, 0, 0, 0}, {0, 0, 0, 20, 20, 20, 0, 0, 0} },
{/* Frame 8*/ {0, 0, 0, 40, 40, 40, 0, 0, 0}, {0, 0, 0, 40, 40, 40, 0, 0, 0}, {0, 0, 0, 40, 40, 40, 0, 0, 0} },
{/* Frame 9*/ {0, 0, 0, 50, 50, 50, 0, 0, 0}, {0, 0, 0, 50, 50, 50, 0, 0, 0}, {0, 0, 0, 50, 50, 50, 0, 0, 0} },
{/* Frame 10*/ {0, 0, 0, 40, 40, 40, 0, 0, 0}, {0, 0, 0, 40, 40, 40, 0, 0, 0}, {0, 0, 0, 40, 40, 40, 0, 0, 0} },
{/* Frame 11*/ {0, 0, 0, 20, 20, 20, 0, 0, 0}, {0, 0, 0, 20, 20, 20, 0, 0, 0}, {0, 0, 0, 20, 20, 20, 0, 0, 0} },
{/* Frame 12*/ {0, 0, 0, 0, 0, 0, 20, 20, 20}, {0, 0, 0, 0, 0, 0, 20, 20, 20}, {0, 0, 0, 0, 0, 0, 20, 20, 20} },
{/* Frame 13*/ {0, 0, 0, 0, 0, 0, 40, 40, 40}, {0, 0, 0, 0, 0, 0, 40, 40, 40}, {0, 0, 0, 0, 0, 0, 40, 40, 40} },
{/* Frame 14*/ {0, 0, 0, 0, 0, 0, 50, 50, 50}, {0, 0, 0, 0, 0, 0, 50, 50, 50}, {0, 0, 0, 0, 0, 0, 50, 50, 50} },
{/* Frame 15*/ {0, 0, 0, 0, 0, 0, 40, 40, 40}, {0, 0, 0, 0, 0, 0, 40, 40, 40}, {0, 0, 0, 0, 0, 0, 40, 40, 40} },
{/* Frame 16*/ {0, 0, 0, 0, 0, 0, 20, 20, 20}, {0, 0, 0, 0, 0, 0, 20, 20, 20}, {0, 0, 0, 0, 0, 0, 20, 20, 20} },
{/* Frame 17*/ {0, 0, 0, 20, 20, 20, 0, 0, 0}, {0, 0, 0, 20, 20, 20, 0, 0, 0}, {0, 0, 0, 20, 20, 20, 0, 0, 0} },
{/* Frame 18*/ {0, 0, 0, 40, 40, 40, 0, 0, 0}, {0, 0, 0, 40, 40, 40, 0, 0, 0}, {0, 0, 0, 40, 40, 40, 0, 0, 0} },
{/* Frame 19*/ {0, 0, 0, 50, 50, 50, 0, 0, 0}, {0, 0, 0, 50, 50, 50, 0, 0, 0}, {0, 0, 0, 50, 50, 50, 0, 0, 0} },
{/* Frame 20*/ {0, 0, 0, 40, 40, 40, 0, 0, 0}, {0, 0, 0, 40, 40, 40, 0, 0, 0}, {0, 0, 0, 40, 40, 40, 0, 0, 0} },
{/* Frame 21*/ {0, 0, 0, 20, 20, 20, 0, 0, 0}, {0, 0, 0, 20, 20, 20, 0, 0, 0}, {0, 0, 0, 20, 20, 20, 0, 0, 0} }
};
byte animation_star[12][3][9] = {
{/* Frame 2*/ {0, 0, 0, 0, 20, 0, 0, 0, 0}, {0, 20, 0, 20, 40, 20, 0, 20, 0}, {0, 0, 0, 0, 20, 0, 0, 0, 0} },
{/* Frame 3*/ {0, 0, 0, 0, 40, 0, 0, 0, 0}, {0, 40, 0, 40, 50, 40, 0, 40, 0}, {0, 0, 0, 0, 40, 0, 0, 0, 0} },
{/* Frame 4*/ {0, 0, 0, 0, 50, 0, 0, 0, 0}, {0, 50, 0, 50, 50, 50, 0, 50, 0}, {0, 0, 0, 0, 50, 0, 0, 0, 0} },
{/* Frame 5*/ {0, 0, 0, 0, 50, 0, 0, 0, 0}, {0, 50, 0, 50, 50, 50, 0, 50, 0}, {0, 0, 0, 0, 50, 0, 0, 0, 0} },
{/* Frame 6*/ {0, 0, 0, 0, 50, 0, 0, 0, 0}, {0, 50, 0, 50, 50, 50, 0, 50, 0}, {0, 0, 0, 0, 50, 0, 0, 0, 0} },
{/* Frame 7*/ {0, 0, 0, 0, 40, 0, 0, 0, 0}, {0, 40, 0, 40, 50, 40, 0, 40, 0}, {0, 0, 0, 0, 40, 0, 0, 0, 0} },
{/* Frame 8*/ {0, 0, 0, 0, 40, 0, 0, 0, 0}, {0, 40, 0, 40, 50, 40, 0, 40, 0}, {0, 0, 0, 0, 40, 0, 0, 0, 0} },
{/* Frame 9*/ {0, 0, 0, 0, 40, 0, 0, 0, 0}, {0, 40, 0, 40, 50, 40, 0, 40, 0}, {0, 0, 0, 0, 40, 0, 0, 0, 0} },
{/* Frame 10*/ {0, 0, 0, 0, 30, 0, 0, 0, 0}, {0, 30, 0, 30, 40, 30, 0, 30, 0}, {0, 0, 0, 0, 30, 0, 0, 0, 0} },
{/* Frame 11*/ {0, 0, 0, 0, 20, 0, 0, 0, 0}, {0, 20, 0, 20, 30, 20, 0, 20, 0}, {0, 0, 0, 0, 20, 0, 0, 0, 0} },
{/* Frame 12*/ {0, 0, 0, 0, 20, 0, 0, 0, 0}, {0, 20, 0, 20, 40, 20, 0, 20, 0}, {0, 0, 0, 0, 20, 0, 0, 0, 0} },
{/* Frame 13*/ {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 20, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0} }
};
void setup() {
Serial.begin(19200);
for (int i=0; i<3; i++) {
pinMode(cathodes[i], OUTPUT);
}
for (int i=0; i<9; i++) {
pinMode(anodes[i], OUTPUT);
}
all_off();
Serial.println("Setup finished");
mode = 0;
}
void loop() {
//// select one
//mode = random(8);
//mode = 1;
mode++;
//Serial.print("Mode: ");
//Serial.println(mode);
switch(mode)
{
case 0:
//Serial.println("All off");
all_off();
break;
case 1:
//Serial.println("Full fading");
full_fading(254, 10, 5); // Steps, Slowness, Floor Value
break;
case 2:
//Serial.println("TestCube");
TestCube();
break;
case 3:
//Serial.println("Animation");
animation_new (animation_scanner, 5, 4, 21); //length, cycles, frames
break;
case 4:
//Serial.println("All fading");
all_fading();
all_fading();
all_fading();
break;
case 5:
//Serial.println("All after");
all_after();
break;
case 6:
//Serial.println("Snow");
snow();
break;
case 7:
//Serial.println("Pyramide");
pyramide();
//Serial.println("Pyramide");
pyramide();
//Serial.println("Pyramide");
pyramide();
break;
case 8:
//Serial.println("snow 2");
snow2();
break;
case 9:
//Serial.println("Animation Scanner");
animation_new (animation_scanner, 10, 254, 21); //length, cycles, frames
break;
case 10:
//Serial.println("Animation New");
animation_new (animation_star, 10, 254, 7); //length, cycles, frames
break;
default: mode = 0; break;
} // end switch
}
void TestCube()
{
digitalWrite(cathodes[0], C_OFF);
digitalWrite(cathodes[1], C_OFF);
digitalWrite(cathodes[2], C_OFF);
for (int i=0; i<3; i++)
{
digitalWrite(cathodes[i], C_ACT);
for (int j=0; j<9; j++)
{
digitalWrite(anodes[j], HIGH);
delay(100);
}
for (int j=0; j<9; j++)
{
digitalWrite(anodes[j], LOW);
delay(100);
}
digitalWrite(cathodes[i], C_OFF);
}
}
void all_fading() {
int factor = 1;
for (int i=0; i<3; i++) {
digitalWrite(cathodes[i], C_ACT);
for (int t=0;t<16;t++) {
for (int j=0; j<9; j++) {
digitalWrite(anodes[j], HIGH);
}
delay(t*factor);
for (int j=0; j<9; j++) {
digitalWrite(anodes[j], LOW);
}
delay((16-t)*factor);
}
for (int t=0;t<16;t++) {
for (int j=0; j<9; j++) {
digitalWrite(anodes[j], HIGH);
}
delay((16-t)*factor);
for (int j=0; j<9; j++) {
digitalWrite(anodes[j], LOW);
}
delay(t*factor);
}
digitalWrite(cathodes[i], C_OFF);
}
}
/* void full_fading(int steps, int slowness, int floor_value)
Description: This function fades all LEDs of the Cube.
Parameters:
--> steps: Steps between full on and off (default: 200)
--> slowness: As bigger this value as slower the fade (default: 5)
--> floor_value: This is the dimmest brightnessvalue (default: 23)
Return Values: None
*/
void full_fading(int steps, int slowness, int floor_value) {
int factor = 1; // This is used as a prescaler - every frame it is increased by one
int brightness; // This is used as a brightness value
// The delayMicroseconds function works very accurately in the range 3 microseconds and up.
if(floor_value < 3)
brightness = 3;
else
brightness = floor_value;
// Activate all anodes
for (int j=0; j<9; j++)
{
digitalWrite(anodes[j], HIGH);
}
// fade on
while (brightness+1<steps)
{
// Go through the layers for every frame
for (int i=0; i<3; i++)
{
// Activate layer
if(brightness > 2)
{
digitalWrite(cathodes[i], C_ACT);
// Wait depending on actual brightness
delayMicroseconds(brightness);
}
// Deactivate layer
digitalWrite(cathodes[i], C_OFF);
// time to stay dark depending on brightness
delayMicroseconds(steps-brightness);
}
// Each frame the factor is counted uo until its equal to slowness
if(factor > slowness)
{
factor = 0;
brightness++;
}
else
{
factor++;
}
}
// fade off
while (brightness > floor_value)
{
// Go through the layers for every frame
for (int i=0; i<3; i++)
{
// Activate layer
if(brightness > 2)
{
digitalWrite(cathodes[i], C_ACT);
// Wait depending on actual brightness
delayMicroseconds(brightness);
}
// Deactivate layer
digitalWrite(cathodes[i], C_OFF);
// stay dark depending on brightnes
delayMicroseconds(steps-brightness);
}
// Each frame the factor is counted uo until its equal to slowness
if(factor > slowness)
{
factor = 0;
brightness--;
}
else
{
factor++;
}
}
// Deactivate all anodes
for (int j=0; j<9; j++)
{
digitalWrite(anodes[j], LOW);
}
}
void all_after() {
for (int i=0; i<3; i++) {
digitalWrite(cathodes[i], C_ACT);
for (int j=0; j<9; j++) {
digitalWrite(anodes[j], HIGH);
delay(50);
digitalWrite(anodes[j], LOW);
}
digitalWrite(cathodes[i], C_OFF);
}
}
void all_after2() {
for (int i=0; i<3; i++) {
digitalWrite(cathodes[i], C_ACT);
for (int j=0; j<9; j++) {
for (int t=0;t<16; t++) {
digitalWrite(anodes[j], HIGH);
delay(t);
digitalWrite(anodes[j], LOW);
delay(15-t);
}
for (int t=0;t<16;t++) {
digitalWrite(anodes[j], HIGH);
delay(15-t);
digitalWrite(anodes[j], LOW);
delay(t);
}
}
digitalWrite(cathodes[i], C_OFF);
}
}
void snow() {
all_off();
for (int j=0; j<9; j++) {
digitalWrite(anodes[j], HIGH);
for (int i=0; i<3; i++) {
digitalWrite(cathodes[2-i], C_ACT);
delay(100);
digitalWrite(cathodes[2-i], C_OFF);
}
digitalWrite(anodes[j], LOW);
}
}
void snow2() {
all_off();
for (int j=0; j<9; j++) {
for (int i=0; i<3; i++) {
digitalWrite(anodes[j], HIGH);
for (int t=0;t<16;t++) {
digitalWrite(cathodes[2-i], C_ACT);
delay(t);
digitalWrite(cathodes[2-i], C_OFF);
delay((15-t));
}
for (int t=0;t<16;t++) {
digitalWrite(cathodes[2-i], C_ACT);
delay((15-t));
digitalWrite(cathodes[2-i], C_OFF);
delay(t);
}
digitalWrite(anodes[j], LOW);
}
}
}
void all_off() {
for (int i=0; i<3; i++) {
for (int j=0; j<9; j++) {
digitalWrite(anodes[j], LOW);
}
digitalWrite(cathodes[i], C_OFF);
}
}
void animation (boolean animation_arr[][3][9], int length, int cycles, byte frames) {
for (int c=0;c<cycles;c++) {
for (int a=0;a<frames;a++) {
for (int l=0;l<length;l++) {
for (int i=0; i<3; i++) {
digitalWrite(cathodes[i], C_ACT);
for (int j=0; j<9; j++) {
if (animation_arr[a][i][j]) digitalWrite(anodes[j], HIGH);
else digitalWrite(anodes[j], LOW);
} //j
delayMicroseconds(1000);
digitalWrite(cathodes[i], C_OFF);
}//i
}//l
}//a
}//c
}
void animation_new (byte animation_arr[][3][9], byte length, byte cycles, byte frames)
{
for (int c=0;c<cycles;c++)
{
for (int a=0;a<frames;a++)
{
for (int l=0; l<length; l++)
for (int i=0; i<3; i++)
{
digitalWrite(cathodes[i], C_ACT);
for (int TickCount = 0; TickCount <= 50; TickCount++)
{
for (int j=0; j<9; j++)
{
if (animation_arr[a][i][j] > TickCount) digitalWrite(anodes[j], HIGH); else digitalWrite(anodes[j], LOW);
} //j
//delay(100);
//delayMicroseconds(3);
}
digitalWrite(cathodes[i], C_OFF);
}//i / TickCount
}//a
}//c
}
boolean test[3][9] ={
{
1, 1, 1,
1, 1, 1,
1, 1, 1
}
,
{
0, 1, 0,
1, 1, 1,
0, 1, 0
}
,
{
0, 0, 0,
0, 1, 0,
0, 0, 0
}
};
void pyramide() {
for (int t=1;t<255;t++) {
for (int i=0; i<3; i++) {
digitalWrite(cathodes[i], C_ACT);
for (int j=0; j<9; j++) {
if (test[i][j]) digitalWrite(anodes[j], HIGH);
else digitalWrite(anodes[j], LOW);
}
delayMicroseconds(255-t);
digitalWrite(cathodes[i], C_OFF);
for (int j=0; j<9; j++) {
digitalWrite(anodes[j], LOW);
}
delayMicroseconds(t);
}
}
for (int t=1;t<255;t++) {
for (int i=0; i<3; i++) {
digitalWrite(cathodes[i], C_OFF);
for (int j=0; j<9; j++) {
if (test[i][j]) digitalWrite(anodes[j], HIGH);
else digitalWrite(anodes[j], LOW);
}
delayMicroseconds(t);
digitalWrite(cathodes[i], C_OFF);
for (int j=0; j<9; j++) {
digitalWrite(anodes[j], LOW);
}
delayMicroseconds(255-t);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.