TLC5940 with led strips

Hi Everyone,

I am new to programming in general and thought that the arduio would be a fun way to learn.
So i have been playing about with different project when i came across the ambient light TV with the led strips, which i admit i copy the code as it is complicated well for me anyway. Did not really learn that much!

so i have now decide after using the tv code to try and do something else with them.
So the idea is to use a sonic distance measuring device so when any one walks in the room the led randomly change so i have changed the code and the sonic detection is working but when I use random(256); the lights flicker on for a about half a second and then go off until the next loop

i have pasted the code here and was wondering if anyone could give me some pointers as to what the problem could be

Thanks in advance


/* TLC5940 16-port LED driver
 = Paden Hogeland - Adjusted for use of 4 RGB LED's
 * Peter Mackey  June 2007  Pratt Digital Arts
 * direct adressing to PORTB, smooth flickerless fading (thanks to Amp on the Arduino forum)
 * additional logic from David Cuartielles's & Marcus Hannerstig's LEDdriver demo
 = see the TLC5940 data sheet for the logic behind these pulse sequences
 #include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

// Name the Arduino pins to the TLC5940 pin names
#define VPRG 2 
#define SIN 3 
#define SCLK 7 
#define XLAT 4
#define BLANK 5
#define DCPRG 6
#define GSCLK 8 //note: but using PORTB method

#define MSINTRVL 0 // could be used to delay updating of incrementFades()
#define FADEMIN 0 // lowest fade level LEDs will reach (min would be 0, max 4065)
#define FADEINCR 64   // determines how many steps it takes to run the desired range (lower=longer)

// Array to hold the port values 0 min - 4095 max
int port[] = {
int faderNdx = 0;  //counter used in this fading sequence

int fadeState[] = {
  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //stores the direction of fading for each port 1,0,-1
//start with first port

int next; //used for limit checking in fading function
float prevMillis; //used for a timing delay

int myword[] = {0,0,0,0,0,0,0,0,0,0,0,0}; //temp storage for reversing bits in a word (for greyscale setting)
// Declare the RGB varables
double red1,green1,blue1,red2,green2,blue2,red3,green3,blue3,red4,green4,blue4;
boolean update;

// Set up 
void setup() {
  // Assign the output pins
  pinMode(VPRG, OUTPUT);    
  pinMode(SIN, OUTPUT);    
  pinMode(SCLK, OUTPUT); 
  pinMode(XLAT, OUTPUT);    
  pinMode(BLANK, OUTPUT);    
  pinMode(DCPRG, OUTPUT);    
  pinMode(GSCLK, OUTPUT);       // Could also set DDRB directly

  // Initalize Serial Connection
  // Initalize unused TLC5940 pins 
  port[13] = 0;
  port[14] = 0;
  port[15] = 0;
  port[0] = 0;
  preset();    //input ?Dot Correction? data

// Main Loop
void loop () {
  unsigned int uS =; // Send ping, get ping time in microseconds (uS).

  if (uS <60)
  // If have passed the interval
  if (millis() > (prevMillis+MSINTRVL)){
    // Set the RGB values
    // Set new previous time

// Set the RGB values
void setRGB() {
  // If correct amount of serial is avaiable
  //if(Serial.available() >= 13 ){
    // If we are at the first chunk of data
    //if( == 0xff){
      // Set the Left Side
      red1 = random(256);
      blue1 = random(256);
      green1 = random(256);
      // Get the percentage of each value then update port accordinly
      port[10] = (4095)*(red1/255);
      port[11] = (4095)*(blue1/255);
      port[12] = (4095)*(green1/255);

      // Set the Top Left Side
      red2 = random(256);
      blue2 = random(256);
      green2 = random(256);}// Get the percentage of each value then update port accordinly
      port[7] = (4095)*(red2/255);
      port[8] = (4095)*(green2/255);
      port[9] = (4095)*(blue2/255);
      // Set the Top Right Side
      red3 = random(256);
      blue3 = random(256);
      green3 = random(256);}
      // Get the percentage of each value then update port accordinly
      port[4] = (4095)*(red3/255);
      port[5] = (4095)*(green3/255);
      port[6] = (4095)*(blue3/255);
      // Set the Right Sidered = random(256);
      blue4 = random(256);
      green4 = random(256);
      red4=random (256);}
      // Get the percentage of each value then update port accordinly
      port[1] = (4095)*(red4/255);
      port[2] = (4095)*(blue4/255);
      port[3] = (4095)*(green4/255);


//=======5940 control======================================
void setGreys()  {
  // For the each port (12 bit word * 16 ports =192 bits in this loop)...
  for (int i=15; i>=0; i--) { 
    // Initalize the data
    int datb = port[i];    

    // Load fade level bits into the temp array BACKWARDS
    for (int j=11; j>=0; j--) { 
      myword[j]=(datb & 1); //& bitwise AND	
      datb >>= 1;        //shift right and assign
      // (maybe there's a slicker way to do this!? but this works...)
    // Send the data to the 5940
    for (int j=0; j<12; j++) { 
  digitalWrite(XLAT, HIGH);
  digitalWrite(XLAT, LOW);

void feedPorts() { 
  // The actual sequencing of the PWM data into the LEDs, must do constantly...
  digitalWrite(BLANK, HIGH);    
  digitalWrite(BLANK, LOW);	//=all outputs ON, start PWM cycle

  for (int i=0; i<4096; i++) {

//DOT once
void preset() {
  //Input ?DotCorrex? Data
  //16 outputs, 64 posssible levels of adjustment, 6 bits/chan = 96 bits total
  //[use if any LEDs in array are physically too bright]

  digitalWrite(DCPRG, HIGH);	//leaving it H is my arbitrary choice (="write to register not EEPROM")  
  digitalWrite(VPRG, HIGH);     //=inputting data into dot correx register

  digitalWrite(BLANK, HIGH);	//=all outputs off, when this goes high it resets the greyscale counter
  digitalWrite(SIN, LOW);       //to start dot correction
  digitalWrite(XLAT, LOW);	

  //begin loading in the dot correx data, most significant bit first...
  //but here we are not correcting anything, so LSB is going first!
  for (int i=0; i<16; i++) { //16 ports
    for (int j=0; j<6; j++) { //6 bits of data for each port
      digitalWrite(SIN,	HIGH);	//for now, 111111 for everybody
      digitalWrite(SIN,	LOW);

  //----doing the FIRST GREYSCALE SETTING here becuz of the unique 193rd clock pulse

  digitalWrite(XLAT, HIGH);   //latch the dot data into the dot correx register
  digitalWrite(XLAT, LOW);
  digitalWrite(VPRG, LOW);    //entering greyscale mode

  for (int i=0; i<16; i++) { //16 ports
    int datb = 4095;        //using same fade level for all ports this first time

    for (int j=0; j<12; j++) { //data for each port, all the same value to start
      digitalWrite(SIN,	datb & 01);	
  digitalWrite(XLAT, HIGH);  //latch the greyscale data 
  digitalWrite(XLAT, LOW);
  pulseSCLK();               //193rd clock pulse only need to do the FIRST time after dot correx

  digitalWrite(BLANK, LOW);  //=all outputs ON, start PWM cycle... moved here

//SCLK used in dot correx and greyscale setting
void pulseSCLK() {
  digitalWrite(SCLK, HIGH);
  digitalWrite(SCLK, LOW);

void pulseGSCLK() {
  //ultra fast pulse trick, using digitalWrite caused flickering
  PORTB=0x01; //bring PORTB0 high (pin 8), other ports go low [0x01 does only pin 8, 0x21 also lifts pin 13]
  //16nanosecs is the min pulse width for the 5940, but no pause seems needed here
  PORTB=0x20;  //keep pin13 high [0x00 would be all low]