Freetronics Cube4

From Hamsterworks Wiki!

(Redirected from Freetronics Cube)
Jump to: navigation, search

This Arduino is a kit for a 4x4x4 RGB Cube. The manufacture's page is http://www.freetronics.com/products/cube4-4x4x4-rgb-led-cube#.UcH9yPk3CSo

Freetronics cube.jpg

If I make it do something nifty I'll add the source here.

Contents

Sort of random colour changes

/*
 * File:    MafRandomColours.ino
 * Version: 1.0
 * Author:  Mike Field <hamster@snap.net.nz>, 
  *
 *          Based on RandomColours.ino by Jonathan Oxer (jon@freetronics.com)
 *
 * License: GPLv3
 */

#include "SPI.h"
#include "Cube.h"

byte xPos;
byte yPos;
byte zPos;
byte rr;
byte gg;
byte bb;
rgb_t colour;

Cube cube;

void setup(void) {
  // Serial port options for control of the Cube using serial commands are:
  // 0: Control via the USB connector (most common).
  // 1: Control via the RXD and TXD pins on the main board.
  // -1: Don't attach any serial port to interact with the Cube.
  cube.begin(0, 115200); // Start on serial port 0 (USB) at 115200 baud
}

void loop(void) {
  static int i;
  xPos = random(4);
  yPos = random(4);
  zPos = random(4);
  if(i == 200)
  {
    i = 0;
    rr = random(0, 2) * 255;
    gg = random(0, 2) * 255;
    bb = random(0, 2) * 255;
  }  
  else
    i++;
  
  cube.set(xPos, yPos, zPos, RGB(rr, gg, bb));
  delay(20);
}

Rainbow

A spinning rainbow

/*
 * File:    Rainbow.ino
 * Version: 1.0
 * Author:  Mike Field<hamster@snap.net.nz>
 * License: GPLv3
 */

#include "SPI.h"
#include "Cube.h"

Cube cube;

void setup(void) {
  // Serial port options for control of the Cube using serial commands are:
  // 0: Control via the USB connector (most common).
  // 1: Control via the RXD and TXD pins on the main board.
  // -1: Don't attach any serial port to interact with the Cube.
  cube.begin(0, 115200); // Start on serial port 0 (USB) at 115200 baud
}

byte global_angle;

void colour(byte x,byte y, byte angle)
{
  byte r = 0, g = 0, b = 0;

  angle += global_angle;
  while(angle > 95) angle -= 95;

  // Calculate the red phase  
  if(angle < 32)      r = angle*4;
  else if(angle < 64) r = (63-angle) * 4;
  
  // Rotate through 120 degrees
  if(angle < 32) angle += 96;
  angle -= 32;

  // Calculate the green phase  
  if(angle < 32)      g = angle*4;
  else if(angle < 64) g = (63-angle) * 4;
  
  // Rotate through 120 degrees
  if(angle < 32) angle += 96;
  angle -= 32;

  // Calculate the blue phase  
  if(angle < 32)      b = angle*4;
  else if(angle < 64) b = (63-angle) * 4;
  
  cube.set(x, y, 0, RGB(r,   g,   b));
  cube.set(x, y, 1, RGB(r/2, g/2, b/2));
  cube.set(x, y, 2, RGB(r/4, g/4, b/4));
  cube.set(x, y, 3, RGB(r/8, g/8, b/8));
}

void loop(void) {
  
  colour(0,0, 0);
  colour(0,1, 8);
  colour(0,2,16);
  colour(0,3,24);
  colour(1,3,32);
  colour(2,3,40);
  colour(3,3,48);
  colour(3,2,56);
  colour(3,1,64);
  colour(3,0,72);
  colour(2,0,80);
  colour(1,0,88);

  colour(1,1, 0);
  colour(1,2, 24);
  colour(2,2, 48);
  colour(2,1, 72);
  
  if(global_angle == 95)
    global_angle = 0;
  else
    global_angle++;
    
  delay(60);
}

splats

Splats 2x2 cubes of a random colour in the Cube4

/*
 * File:    splats.ino
 * Version: 1.0
 * Author:  Mike Field <hamster@snap.net.nz>
 * License: GPLv3
 */

#include "SPI.h"
#include "Cube.h"

Cube cube;

void setup(void) {
  // -1: Don't attach any serial port to interact with the Cube.
  cube.begin(-1, 115200); // Start on serial port 0 (USB) at 115200 baud
}

void mySet(byte x, byte y, byte z, rgb_t c)
{
  cube.set(x&3, y&3, z&3, c);
}

void loop(void) {
  byte x, y, z;
  byte r, g, b;
  x = random(4);
  y = random(4);
  z = random(4);
  r = random(0, 2) * 255;
  g = random(0, 2) * 255;
  b = random(0, 2) * 255;
  
  mySet(x+0,y+0,z+0, RGB(r, g, b));
  mySet(x+0,y+0,z+1, RGB(r, g, b));
  mySet(x+0,y+1,z+0, RGB(r, g, b));
  mySet(x+0,y+1,z+1, RGB(r, g, b));
  mySet(x+1,y+0,z+0, RGB(r, g, b));
  mySet(x+1,y+0,z+1, RGB(r, g, b));
  mySet(x+1,y+1,z+0, RGB(r, g, b));
  mySet(x+1,y+1,z+1, RGB(r, g, b));
  
  delay(2000);
}

Rainbow2

/*
 * File:    Rainbow2.ino
 * Version: 1.0
 * Author:  Mike Field<hamster@snap.net.nz>
 * License: GPLv3
 */

#include "SPI.h"
#include "Cube.h"

Cube cube;

void setup(void) {
  // Serial port options for control of the Cube using serial commands are:
  // 0: Control via the USB connector (most common).
  // 1: Control via the RXD and TXD pins on the main board.
  // -1: Don't attach any serial port to interact with the Cube.
  cube.begin(0, 115200); // Start on serial port 0 (USB) at 115200 baud
}

byte global_angle1;
byte global_angle2;
byte global_angle3;

void colour(byte x,byte y, byte a)
{
  byte r = 0, g = 0, b = 0;
  byte angle;

  angle = global_angle1/2 + a;
  while(angle > 95) angle -= 95;
  if(angle < 32)      r = angle*4;
  else if(angle < 64) r = (63-angle) * 4;
  
  angle = global_angle2/2 + a;
  while(angle > 95) angle -= 95;
  if(angle < 32)      g = angle*4;
  else if(angle < 64) g = (63-angle) * 4;
  
  angle = global_angle3/2 + a;
  while(angle > 95) angle -= 95;
  if(angle < 32)      b = angle*4;
  else if(angle < 64) b = (63-angle) * 4;
  
  cube.set(x, y, 0, RGB(r,   g,   b));
  cube.set(x, y, 1, RGB(r/2, g/2, b/2));
  cube.set(x, y, 2, RGB(r/4, g/4, b/4));
  cube.set(x, y, 3, RGB(r/8, g/8, b/8));
}

void loop(void) {
  
  colour(0,0, 0);
  colour(0,1, 8);
  colour(0,2,16);
  colour(0,3,24);
  colour(1,3,32);
  colour(2,3,40);
  colour(3,3,48);
  colour(3,2,56);
  colour(3,1,64);
  colour(3,0,72);
  colour(2,0,80);
  colour(1,0,88);

  colour(1,1, 0);
  colour(1,2, 24);
  colour(2,2, 48);
  colour(2,1, 72);
  
  if(global_angle1 > 191-3)
    global_angle1 -= 192-3;
  else
    global_angle1 += 3;

  if(global_angle2 > 191-4)
    global_angle2 -= 192-4;
  else
    global_angle2 += 3;
    
  if(global_angle3 > 191-5)
    global_angle3 -= 192-5;
  else
    global_angle3 += 5;
    
  delay(20);
}

Personal tools