Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 92c5e703eb
Fetching contributors…

Cannot retrieve contributors at this time

file 129 lines (102 sloc) 3.192 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
// 2009 Kenneth Finnegan
// kennethfinnegan.blogspot.com

// Pins.
#define clockpin 13 // CI
#define enablepin 10 // EI
#define latchpin 9 // LI
#define datapin 11 // DI

// Can't be higher than 255
#define MAXBRIGHT 1023
#define NumLEDs 48
#define FADENUM 4

// s[tate] t[arget] colors
int s[NumLEDs][3] = {0}, t[NumLEDs][3] = {0};

int LEDChannels[NumLEDs][3] = {0};
int SB_CommandMode;
int SB_RedCommand;
int SB_GreenCommand;
int SB_BlueCommand;
 
void setup() {

   // Make the random MORE RANDOM!
   randomSeed(analogRead(0));
  
   pinMode(datapin, OUTPUT);
   pinMode(latchpin, OUTPUT);
   pinMode(enablepin, OUTPUT);
   pinMode(clockpin, OUTPUT);
   SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0);
   digitalWrite(latchpin, LOW);
   digitalWrite(enablepin, LOW);
   
   SB_CommandMode = B01; // Write to current control registers
   for (int z = 0; z < NumLEDs; z++) SB_SendPacket();
   delayMicroseconds(15);
   digitalWrite(latchpin,HIGH); // latch data into registers
   delayMicroseconds(15);
   digitalWrite(latchpin,LOW);
}

 
void SB_SendPacket() {
 
    if (SB_CommandMode == B01) {
     SB_RedCommand = 90;
     SB_GreenCommand = 75;
     SB_BlueCommand = 75;
    }
 
    SPDR = SB_CommandMode << 6 | SB_BlueCommand>>4;
    while(!(SPSR & (1<<SPIF)));
    SPDR = SB_BlueCommand<<4 | SB_RedCommand>>6;
    while(!(SPSR & (1<<SPIF)));
    SPDR = SB_RedCommand << 2 | SB_GreenCommand>>8;
    while(!(SPSR & (1<<SPIF)));
    SPDR = SB_GreenCommand;
    while(!(SPSR & (1<<SPIF)));
 
}
 
void WriteLEDArray() {
 
    SB_CommandMode = B00; // Write to PWM control registers
    for (int h = 0;h<NumLEDs;h++) {
SB_RedCommand = LEDChannels[h][0];
SB_GreenCommand = LEDChannels[h][1];
SB_BlueCommand = LEDChannels[h][2];
SB_SendPacket();
    }
 
    delayMicroseconds(15);
    digitalWrite(latchpin,HIGH); // latch data into registers
    delayMicroseconds(15);
    digitalWrite(latchpin,LOW);

/*
SB_CommandMode = B01; // Write to current control registers
for (int z = 0; z < NumLEDs; z++) SB_SendPacket();
delayMicroseconds(15);
digitalWrite(latchpin,HIGH); // latch data into registers
delayMicroseconds(15);
digitalWrite(latchpin,LOW);
*/
}


void loop() {
  int i,j, offset;

  for (j = 0; j < NumLEDs; j++) {

    // check if this LED got to target - shift if not
    if(s[j][0]!=t[j][0] || s[j][1]!=t[j][1] || s[j][2] != t[j][2]) {
      for (i = 0; i<3; i++) {
        if (s[j][i] > t[j][i]) {
          s[j][i] = s[j][i] - 1;
        } else if (s[j][i] < t[j][i]) {
          s[j][i] = s[j][i] + 1;
        }
      }
      
    } else {

      // Select the next target color
      // Start from a random one of the three colors to prevent
      // the cycle from being red biased.
      offset = random(3);
      t[j][offset] = random(MAXBRIGHT);
      t[j][(offset+1)%3] = random(MAXBRIGHT - t[j][offset]);
      t[j][(offset+2)%3] = MAXBRIGHT - t[j][offset] - t[j][(offset+1)%3];
    }

    LEDChannels[j][0] = s[j][0];
    LEDChannels[j][1] = s[j][1];
    LEDChannels[j][2] = s[j][2];
  }
  
  WriteLEDArray();
  //delay(1);


  // Let the viewer enjoy the new color before
  // selecting the next target color.
  //delay(500);
}
Something went wrong with that request. Please try again.