Skip to content

Instantly share code, notes, and snippets.

@Tmw
Created February 1, 2016 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tmw/3a3f3d016a6592a989d8 to your computer and use it in GitHub Desktop.
Save Tmw/3a3f3d016a6592a989d8 to your computer and use it in GitHub Desktop.
Source for Cube V1
#include <SPI.h>
// define pins and other variables
const int latchPin = 8;
const int clockPin = 13;
const int dataPin = 11;
const int NUMBER_OF_CONNECTED_LEDS = 16;
// global array that keeps track of the LEDs brightnesses
bool brightnessMask_layer1[NUMBER_OF_CONNECTED_LEDS*4];
bool brightnessMask_layer2[NUMBER_OF_CONNECTED_LEDS*4];
bool brightnessMask_layer3[NUMBER_OF_CONNECTED_LEDS*4];
bool brightnessMask_layer4[NUMBER_OF_CONNECTED_LEDS*4];
// setup the correct pins and initialize SPI library
void setup() {
// Setup refresh
// setup pins
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// setup SPI framework
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.begin();
// True random by seeding it a random analogRead signal
randomSeed(analogRead(0));
// turn all LEDs off in advance
clearLeds();
}
// loop to draw animations
void loop() {
for(int z = 0; z < 4; z++){
for(int x =0; x < 4; x++){
for(int y =0; y < 4; y++){
led(x,y,z,15);
}
}
}
refresh();
}
////////////////////////////////////////
// Internal methods to drive the LEDs //
////////////////////////////////////////
void led(int x, int y, int z, int brightness){
// ensure 4-bit limited brightness
brightness = constrain(brightness, 0, 15);
int ledNr = y*4+x;
// turn 4-bit brightness into brightness mask
for (int i = 3; i >= 0; i--) {
if (brightness - (1 << i) >= 0) {
brightness -= (1 << i);
setBrightnessForLayerAddressValue(z, (ledNr*4)+i, true);
}
else{
setBrightnessForLayerAddressValue(z, (ledNr*4)+i, false);
}
}
}
void setBrightnessForLayerAddressValue(int layer, int address, bool value){
if(layer == 0){
brightnessMask_layer1[address] = value;
}
else if(layer == 1){
brightnessMask_layer2[address] = value;
}
else if(layer == 2){
brightnessMask_layer3[address] = value;
}
else if(layer == 3){
brightnessMask_layer4[address] = value;
}
}
// transform brighnesses to 4-bit BAM
void refresh(){
// Loop over each LED
for (int cycle = 0; cycle < 16; cycle++) {
for (int currentLed = 0; currentLed < NUMBER_OF_CONNECTED_LEDS; currentLed++) {
int maskPosition = currentLed * 4;
if (cycle == 1 && brightnessMask_layer1[maskPosition]) {
turnOnLed(currentLed, 0);
}
else if ((cycle == 2 || cycle == 3) && brightnessMask_layer1[maskPosition+1]) {
turnOnLed(currentLed, 0);
}
else if (cycle >= 4 && cycle <= 7 && brightnessMask_layer1[maskPosition+2]) {
turnOnLed(currentLed, 0);
}
else if (cycle >= 8 && cycle <= 15 && brightnessMask_layer1[maskPosition+3]) {
turnOnLed(currentLed, 0);
}
}
for (int currentLed = 0; currentLed < NUMBER_OF_CONNECTED_LEDS; currentLed++) {
int maskPosition = currentLed * 4;
if (cycle == 1 && brightnessMask_layer2[maskPosition]) {
turnOnLed(currentLed, 1);
}
else if ((cycle == 2 || cycle == 3) && brightnessMask_layer2[maskPosition+1]) {
turnOnLed(currentLed, 1);
}
else if (cycle >= 4 && cycle <= 7 && brightnessMask_layer2[maskPosition+2]) {
turnOnLed(currentLed, 1);
}
else if (cycle >= 8 && cycle <= 15 && brightnessMask_layer2[maskPosition+3]) {
turnOnLed(currentLed, 1);
}
}
for (int currentLed = 0; currentLed < NUMBER_OF_CONNECTED_LEDS; currentLed++) {
int maskPosition = currentLed * 4;
if (cycle == 1 && brightnessMask_layer3[maskPosition]) {
turnOnLed(currentLed, 2);
}
else if ((cycle == 2 || cycle == 3) && brightnessMask_layer3[maskPosition+1]) {
turnOnLed(currentLed, 2);
}
else if (cycle >= 4 && cycle <= 7 && brightnessMask_layer3[maskPosition+2]) {
turnOnLed(currentLed, 2);
}
else if (cycle >= 8 && cycle <= 15 && brightnessMask_layer3[maskPosition+3]) {
turnOnLed(currentLed, 2);
}
}
for (int currentLed = 0; currentLed < NUMBER_OF_CONNECTED_LEDS; currentLed++) {
int maskPosition = currentLed * 4;
if (cycle == 1 && brightnessMask_layer4[maskPosition]) {
turnOnLed(currentLed, 3);
}
else if ((cycle == 2 || cycle == 3) && brightnessMask_layer4[maskPosition+1]) {
turnOnLed(currentLed, 3);
}
else if (cycle >= 4 && cycle <= 7 && brightnessMask_layer4[maskPosition+2]) {
turnOnLed(currentLed, 3);
}
else if (cycle >= 8 && cycle <= 15 && brightnessMask_layer4[maskPosition+3]) {
turnOnLed(currentLed, 3);
}
}
clearLeds();
}
}
// turn on correct LED using 595's
void turnOnLed(int ledNr, int layer) {
digitalWrite(latchPin, LOW);
SPI.transfer(1<<layer);
if (ledNr >= 8) {
SPI.transfer(1<<ledNr-8);
SPI.transfer(0);
}
else {
SPI.transfer(0);
SPI.transfer(1<<ledNr);
}
digitalWrite(latchPin, HIGH);
}
// turn off all LEDs using 595's
void clearLeds() {
digitalWrite(latchPin, LOW);
SPI.transfer(0);
SPI.transfer(0);
SPI.transfer(0);
digitalWrite(latchPin, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment