Main

April 23, 2008

Code for Arduino

Here's the code I've written for the Arduino; I'll comment it as necessary.


#include

TLC5940 leds(1, 8);

void setup() {
Serial.begin(115200);
leds.init();
leds.clear();
leds.display();
}

void loop() {
rainbow();
}

void colorLED(char current, char color) {
if (color == 1)
fullLEDset(current, 255, 0, 0);
if (color == 2)
fullLEDset(current, 255, 255, 0);
if (color == 3)
fullLEDset(current, 0, 255, 0);
if (color == 4)
fullLEDset(current, 0, 255, 100);
if (color == 5)
fullLEDset(current, 0, 0, 255);
if (color == 6)
fullLEDset(current, 255, 0, 255);
if (color == 7)
fullLEDset(current, 255, 255, 255);
if (color == 8)
fullLEDset(current, 0, 0, 0);
leds.display();
}

// The full LED set function takes in a number for the LED to be changed, followed by the RED, GREEN, and BLUE values specified.

void fullLEDset(char LED, char red, char green, char blue) {
leds.setLED(LED*3 - 2, blue);
leds.setLED(LED*3 - 1, green);
leds.setLED(LED*3, red);
leds.display();
}

// A simple algorithm that colors lights around the outside in a circular fashion.
void circle() {
int current = 1;
int color = 1;
int i;
while(1) {
if (current == 4) {
current = 0;
color++;

for (i = 0; i < 256; i++) {
leds.setLED(15, i);
leds.setLED(14, i);
leds.setLED(13, i);
leds.display();
delay(2);
}
for (i = 255; i > -1; i--) {
leds.setLED(15, i);
leds.setLED(14, i);
leds.setLED(13, i);
leds.display();
delay(2);
}
}
if (color == 7)
color = 1;
current++;
colorLED(current, color);
delay(250);
}
}

// A fade in and out for each color.
void breathe() {
int intensity;
int color = 1;
int red, green, blue;
while(1) {
red = 0;
green = 0;
blue = 0;
if (color == 4)
color = 1;
if (color == 1)
red = 1;
if (color == 2)
green = 1;
if (color == 3)
blue = 1;
for (intensity = 5; intensity < 120; intensity++) {
fullLEDset(1, red*intensity, green*intensity, blue*intensity);
fullLEDset(2, red*intensity, green*intensity, blue*intensity);
fullLEDset(3, red*intensity, green*intensity, blue*intensity);
fullLEDset(4, red*intensity, green*intensity, blue*intensity);
fullLEDset(5, red*intensity, green*intensity, blue*intensity);
delay(5);
}
for (intensity = 120; intensity > 4; intensity--) {
fullLEDset(1, red*intensity, green*intensity, blue*intensity);
fullLEDset(2, red*intensity, green*intensity, blue*intensity);
fullLEDset(3, red*intensity, green*intensity, blue*intensity);
fullLEDset(4, red*intensity, green*intensity, blue*intensity);
fullLEDset(5, red*intensity, green*intensity, blue*intensity);
delay(10);
}
color++;
}
}

// Completely random and seizure-inducing.
void randomStuff() {
int r, i;
while (1) {
r = rand() / 4096;
i = rand() / 6553 + 1;
colorLED(i, r);
delay(1);
}
}

// Fades between all the colors in the rainbow.
void rainbow() {
int red;
int green;
int blue;

red = 255;
green = 0;
blue = 0;

while (1) {
for (green = 0; green < 255; green++) {
fullLEDset(1, red, green, blue);
fullLEDset(2, red, green, blue);
fullLEDset(3, red, green, blue);
fullLEDset(4, red, green, blue);
fullLEDset(5, red, green, blue);
delay(2);
}
delay(1000);
for (red = 255; red > 0; red--) {
fullLEDset(1, red, green, blue);
fullLEDset(2, red, green, blue);
fullLEDset(3, red, green, blue);
fullLEDset(4, red, green, blue);
fullLEDset(5, red, green, blue);
delay(2);
}
delay(1000);
for (blue = 0; blue < 255; blue++) {
fullLEDset(1, red, green, blue);
fullLEDset(2, red, green, blue);
fullLEDset(3, red, green, blue);
fullLEDset(4, red, green, blue);
fullLEDset(5, red, green, blue);
delay(2);
}
delay(1000);
for (green = 255; green > 0; green--) {
fullLEDset(1, red, green, blue);
fullLEDset(2, red, green, blue);
fullLEDset(3, red, green, blue);
fullLEDset(4, red, green, blue);
fullLEDset(5, red, green, blue);
delay(2);
}
delay(1000);
for (red = 0; red < 255; red++) {
fullLEDset(1, red, green, blue);
fullLEDset(2, red, green, blue);
fullLEDset(3, red, green, blue);
fullLEDset(4, red, green, blue);
fullLEDset(5, red, green, blue);
delay(2);
}
delay(1000);
for (blue = 255; blue > 0; blue--) {
fullLEDset(1, red, green, blue);
fullLEDset(2, red, green, blue);
fullLEDset(3, red, green, blue);
fullLEDset(4, red, green, blue);
fullLEDset(5, red, green, blue);
delay(2);
}
delay(1000);
}
}

// A five bit counter implemented with a cube.
void count() {
char counter = 0;
char temp;
while(1) {
for (counter = 0; counter < 32; counter++) {
temp = counter;
if (temp >= 16) {
fullLEDset(5, 255, 0, 0);
temp = temp - 16;
}
else
fullLEDset(5, 0, 0, 0);


if (temp >= 8) {
fullLEDset(4, 255, 0, 0);
temp = temp - 8;
}
else
fullLEDset(4, 0, 0, 0);

if (temp >= 4) {
fullLEDset(3, 255, 0, 0);
temp = temp - 4;
}
else
fullLEDset(3, 0, 0, 0);

if (temp >= 2) {
fullLEDset(2, 255, 0, 0);
temp = temp - 2;
}
else
fullLEDset(2, 0, 0, 0);

if (temp >= 1)
fullLEDset(1, 255, 0, 0);
else
fullLEDset(1, 0, 0, 0);

delay(1000);
}
}
}

// Police siren.
void spin() {
while(1) {
fullLEDset(1, 255, 0, 0);
fullLEDset(3, 0, 0, 255);
fullLEDset(5, 255, 255, 255);
delay(250);
fullLEDset(1, 0, 0, 0);
fullLEDset(3, 0, 0, 255);
fullLEDset(2, 255, 0, 0);
fullLEDset(4, 0, 0, 255);
fullLEDset(5, 0, 0, 0);
delay(250);
fullLEDset(2, 0, 0, 0);
fullLEDset(4, 0, 0, 0);
fullLEDset(1, 0, 0, 255);
fullLEDset(3, 255, 0, 0);
fullLEDset(5, 255, 255, 255);
delay(250);
fullLEDset(3, 0, 0, 0);
fullLEDset(1, 0, 0, 0);
fullLEDset(2, 0, 0, 255);
fullLEDset(4, 255, 0, 0);
fullLEDset(5, 0, 0, 0);
delay(250);
fullLEDset(4, 0, 0, 0);
fullLEDset(2, 0, 0, 0);
}
}

// A quick flashing algorithm. I made this to determine exactly the delay necessary to look like "flashing" to the human eye.
void flash() {
while(1) {
fullLEDset(1, 255, 0, 0);
fullLEDset(2, 255, 0, 255);
fullLEDset(3, 0, 255, 0);
fullLEDset(4, 0, 0, 255);
fullLEDset(5, 255, 255, 0);
delay(15);
fullLEDset(1, 0, 0, 0);
fullLEDset(2, 0, 0, 0);
fullLEDset(3, 0, 0, 0);
fullLEDset(4, 0, 0, 0);
fullLEDset(5, 0, 0, 0);
delay(15);
}
}

// Stress test which forces the LEDs to be mostly on for a long period.
void stress() {
int first = 255, second = 51, third = 102, fourth = 153, fifth = 204;
while(1) {
fullLEDset(1, first, first, first);
fullLEDset(2, second, second, second);
fullLEDset(3, third, third, third);
fullLEDset(4, fourth, fourth, fourth);
fullLEDset(5, fifth, fifth, fifth);
first--;
second--;
third--;
fourth--;
fifth--;
if (!first)
first = 255;
if (!second)
second = 255;
if (!third)
third = 255;
if (!fourth)
fourth = 255;
if (!fifth)
fifth = 255;

delay(5);
}
}

March 31, 2008

Demo Videos

Here is my youtube user page containing all videos we've recorded thus far.

http://youtube.com/AmishNinja999