This site is archival. For maintained and responsive content, visit efuller.net.
© 2008 Elizabeth

Life Dress: LED Drivers and Lilypads

The Life Dress has a maintained page at efuller.net/life-dress-f2.

LED Drivers

Well, a large portion of today was spent trying to resolve issues with the communications between my LED drivers. At first, I could get the first driver to work, but not the second. Then neither would work. Finally, I un-wired both of them and started from fresh. And, guess what? They worked! Go figure.


It was surprisingly simple to integrate the LED driver code into my Life code. I had already set up a write out section that wrote the the screen. I just inserted statements to turn on and off lights and then had them write out when all the LEDs had been set. It worked first time around! I wish that happened more often.

//Daniel Shiffman, Nature of Code, 3/1/2005
//http://stage.nyu.edu/nature

//a basic implementation of John Conway's Game of Life CA

/*
* Alex Leone - acleone ~AT~ u.washington.edu
* 08-08-08 - v001
*
* Multiple TLC5940's daisy-chained:
* Tlc.begin(period, chips, PWM data array);
*
* Tlc.analogWrite(channel (0-15), value (0-4095), chip (0-n) = 0);
* Tlc.analogWriteAll(value (0-4095), chip(0-n) = -1); // if chip isn't specified, write to all chips
*
* Pin setup:
* ------------ ---u----
* ARDUINO 13| LED1 |1 28| LED 0
* 12|-> SIN, serial data line LED2 |2 27|-> GND
* 11| LED3 |3 26|-> SIN (pin 12)
* 10|-> BLANK LED4 |4 25|-> SCLK (pin 8)
* 9| . |5 24|-> XLAT (pin 7)
* 8|-> SCLK, serial clock . |6 23|-> BLANK (pin 10)
* 7|-> XLAT, serial data latch . |7 22|-> GND
* 6| . |8 21|-> VCC (+5V)
* 5| . |9 20|-> 1.5K Resistor -> GND
* 4| . |10 19|-> +5V
* 3|-> GSCLK, greyscale PWM clock . |11 18|-> GSCLK (pin 3)
* 2| . |12 17|-> SIN of the next chip
* 1| . |13 16|
* 0| LED14|14 15| LED 15
* ------------ --------
*
* Put the longer leg of the LEDs in the +5V and the shorter leg in LED(0-15).
* The 1.5K resistor will let ~20mA through each output pin. See http://www.ti.com/lit/gpn/tlc5940
*
* TO DAISY-CHAIN:
* 1. Hook everything up identically on both chips. Each chip should have it's own 1.5K or whatever resistor
* to ground.
* 2. Hook SOUT (pin 17) on the first chip to SIN of the second chip (don't hook SIN on the second chip to the arduino).
* The third chip's SIN would be hooked to the SOUT of the second chip, etc...
*/

#include

unsigned int PWMData[] =
{0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // first chip
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}; // second chip

int cellsize = 2;
const int COLS = 4;
const int ROWS = 8;
//game of life board
boolean old_board[COLS][ROWS];
boolean new_board[COLS][ROWS]; // !! there is a temp value in the redraw code that needs to be changed too!!

void setup()
{
// configure the serial connection:
Serial.begin(9600);
//Serial.println("hello");
initBoard();
//Serial.println("ready");
Tlc.begin(10, 2, PWMData); // 10 ms PWM, 2 chips, PWM data array
Tlc.analogWriteAll(4095, 0); // turn everything on the first chip on
delay(2000);
Tlc.analogWriteAll(4095, 1); // turn everything on the second chip on
delay(2000);
Tlc.analogWriteAll(0); // Turn everything off on both chips
}

void loop()
{
//Serial.println("loop");
calculate();
/*while(Serial.available() > 0) {
int xToLive = Serial.read();
int yToLive = Serial.read();
new_board[xToLive][yToLive] = 1;
}*/
sendData();
delay(1000);
}

void calculate()
{
int alive = 1;
int dead = 0;
//loop through every spot in our 2D array and check spots neighbors
for (int x = 0; x < COLS;x++) {
for (int y = 0; y < ROWS;y++) {
int nb = 0;
//Note the use of mod ("%") below to ensure that cells on the edges have "wrap-around" neighbors
//above row
if (old_board[(x+COLS-1) % COLS ][(y+ROWS-1) % ROWS ] == 1) { nb++; }
if (old_board[ x ][(y+ROWS-1) % ROWS ] == 1) { nb++; }
if (old_board[(x+1) % COLS ][(y+ROWS-1) % ROWS ] == 1) { nb++; }
//middle row
if (old_board[(x+COLS-1) % COLS ][ y ] == 1) { nb++; }
if (old_board[(x+1) % COLS ][ y ] == 1) { nb++; }
//bottom row
if (old_board[(x+COLS-1) % COLS ][(y+1) % ROWS ] == 1) { nb++; }
if (old_board[ x ][(y+1) % ROWS ] == 1) { nb++; }
if (old_board[(x+1) % COLS ][(y+1) % ROWS ] == 1) { nb++; }

//RULES OF "LIFE" HERE
if ((old_board[x][y] == 1) && (nb < 2)) { new_board[x][y] = 0; } //loneliness
else if ((old_board[x][y] == 1) && (nb > 3)) { new_board[x][y] = 0; } //overpopulation
else if ((old_board[x][y] == 0) && (nb == 3)) { new_board[x][y] = 1; } //reproduction
else { new_board[x][y] = old_board[x][y]; } //stasis
}
}
}
void sendData()
{
int ctr = 0;
int value; // value to send to LED, on or off
//RENDER game of life based on "new_board" values
//Serial.println("calling redraw");
for ( int i = 0; i < COLS; i++) {
for ( int j = 0; j < ROWS; j++) {
// write old board like new board
old_board[i][j] = new_board[i][j];
// send board settings to Processing
if ((new_board[i][j] == 1)) {
// send living cell
Serial.print(1, DEC);
Serial.print(",");
value = 4095;
}
else
{
// send empty cell
Serial.print(0, DEC);
Serial.print(",");
value = 0;
}
Tlc.setPWM(ctr % 16, value, ctr / 16);
ctr++;
}
}
Tlc.updatePWMs();
//Serial.print(ctr);
Serial.print("\n");
//delay(100);
}

//init board with random "alive" squares
void initBoard() {
//Serial.println("call initBoard");
for (int i =0;i < COLS;i++) {
for (int j =0;j < ROWS;j++) {
if (int(random(2)) == 0) {
old_board[i][j] = 1;
} else {
old_board[i][j] = 0;
}
}
}
}

Lilypad

Next step, as I saw it, was to transfer the working code to my Lilypad and make it work through that. Unfortunately, I’ve been getting an odd error when uploading: avrdude: stk500_recv(): programmer is not responding. At one point I was even short circuited the USB board because the pins on the Lilypad do not exactly match up with the Arduino. I’ve tried switching the wires but it still won’t upload. The irony is that, in the process of error checking, I changed so many things and altered so many wires that, when I found the solution to my original problem, I had created so many other problems that the solution showed no positive result. Basically, I needed to reset the lilypad right before loading new code.

Related posts:

  1. Physical Computing Final: Life (week 1, Monday) Earlier in Life… I have already written code to run...
  2. Life Dress [Matrix] The Life Dress has a maintained page at efuller.net/life-dress-f3. After...
  3. Life Dress [Multiplexers v2] The Life Dress has a maintained page at efuller.net/life-dress-f3. Contents...
  4. Life: Drivers and Materials The Life Dress has a maintained page at efuller.net/life-dress-f2. After...
  5. Life Dress: Final Presentation The Life Dress has a maintained page at efuller.net/life-dress-f2. The...

One Comment

  1. Posted November 29, 2008 at 2:42 pm | #

    Nice post u have here :D Added to my RSS reader

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>