Freetronics Cube & Ultrasonic Sensor

I got this Cube from a friend who bought from Freetronics at Sydney’s Mini Maker Faire last year. It is 4x4x4 LED matrix cube with onboard ATmega32u4 micro. It has Arduino bootloader and has various i/o breakout.
If you have one of these, you may try the Mega Colour Demo code by buxtronix.

IMG_3173         IMG_3230


In this challenge, I connected the cube to the HC-SR04 Ultrasonic sensor, which I used in the past. The distance of the nearby object is translated to the height (z-axis) of the cube.

My code is based on the Heartbeat code by Jonathan Oxer.

#include <SPI.h>
#include <Cube.h>
#include <Ultrasonic.h>  

byte xPos, yPos, zPos;
Cube cube;
Ultrasonic ultrasonic(A0,A1);
long measure;

void setup(void) {
  cube.begin(0, 115200); // Start on serial port 0 (USB) at 115200 baud
  pinMode(A0, OUTPUT);
  pinMode(A1, INPUT);
}

void loop() {
  int i = 0;
  int xPos = 0;
  int yPos = 0;
  int zPos = 0;
  byte rr;
  byte gg;
  byte bb;

  measure = (ultrasonic.Ranging(CM)) >> 1;
  Serial.println(measure);

  while(zPos < 4)
  {
    yPos = 3;
    while(yPos > -1)
    {
      xPos = 0;
      while(xPos < 4)
      {
        rr = random(0, 2) * 255;
        gg = random(0, 2) * 255;
         bb = random(0, 2) * 255;
        if(rr == 0 && gg == 0 && bb == 0) rr = 1;
        cube.set(xPos, yPos, zPos, RGB(rr, gg, bb));
        xPos++;
        i++;
        delay(4);
      }
      yPos--;
    }
    if (zPos >= measure)
    {
      break;
    }
    zPos++;
  }
  delay(100);
  while(zPos > -1)
  {
    xPos = 3;
    while(xPos > -1)
    {
      yPos = 3;
      while(yPos > -1)
      {
        cube.set(xPos, yPos, zPos, BLACK);
        yPos--;
        delay(4);
      }
      xPos--;
    }
    zPos--;
  }
}
This entry was posted in Arduino, Cube, My Projects, ultrasonic and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *