struct pixel {
int r;
int g;
int b;
};
static int8_t left, right;
static struct pixel leftc, rightc, ltrailc, rtrailc;
static int8_t leftv, rightv;
static bool started=
false;
int dataPin = 6;
int clkPin = 5;
#define CHIPS 50
#define DELAY 1
static struct pixel strip[CHIPS+2];
static struct pixel colours[9] = {
{
254, 0, 0 }
,
// red
{
254, 100, 0 }
,
// orange
{
254, 254, 0 }
,
// yellow
{
0, 254, 0 }
,
// green
{
0, 100, 254 }
,
// blue
{
50, 0, 254 }
,
// indigo
{
100, 0, 254 }
,
// violet
{
254, 254, 254 }
,
// white
{
0,0,0 }
,
};
void setup() {
char c;
// blank the strip
for (c=0; c<CHIPS; c++) {
strip[c].r = 0;
strip[c].b = 0;
strip[c].g = 0;
}
ltrailc=colours[8];
rtrailc=colours[8];
generate_left_wave();
generate_right_wave();
started =
true;
pinMode(dataPin,
OUTPUT);
pinMode(clkPin,
OUTPUT);
Serial.
begin(38400);
}
void slowShiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder,
byte val)
{
int i;
for (i = 0; i < 8; i++) {
if (bitOrder ==
LSBFIRST) {
digitalWrite(dataPin, !!(val & (1 << i)));
delayMicroseconds(DELAY);
}
else {
digitalWrite(dataPin, !!(val & (1 << (7 - i))));
delayMicroseconds(DELAY);
}
digitalWrite(clockPin,
HIGH);
delayMicroseconds(DELAY);
digitalWrite(clockPin,
LOW);
delayMicroseconds(DELAY);
}
}
void writeByte(
byte b)
{
slowShiftOut(dataPin, clkPin,
MSBFIRST, b);
}
void writeGuard(
void)
{
digitalWrite(clkPin, 1);
digitalWrite(dataPin, 0);
delayMicroseconds(DELAY);
digitalWrite(clkPin, 0);
delayMicroseconds(DELAY);
//writeByte(0x00);
writeByte(0xff);
writeByte(0xff);
writeByte(0xff);
}
void writeFrame(struct pixel p)
{
writeByte(p.g);
writeByte(p.b);
writeByte(p.r);
}
void pushStrip(
void)
{
char c;
for (c=0; c<CHIPS; c++)
writeFrame(strip[c+1]);
writeGuard();
}
void generate_left_wave(
void)
{
left = 1;
leftc = colours[
random(8)];
leftv = 1;
}
void generate_right_wave(
void)
{
right = CHIPS-1;
rightc = colours[
random(8)];
rightv = -1;
}
void avgcolour (struct pixel *a, struct pixel *b, struct pixel *dest)
{
dest->r = (a->r + b->r) / 2;
dest->g = (a->g + b->g) / 2;
dest->b = (a->b + b->b) / 2;
}
void dim(struct pixel *x)
{
x->r = x->r * 0.5;
x->g = x->g * 0.5;
x->b = x->b * 0.5;
}
void loop()
{
int i;
if (!started) {
// invent some waves
generate_left_wave();
generate_right_wave();
started =
true;
}
for (i=0; i<CHIPS; i++) {
dim(&strip[i]);
}
left += leftv;
right += rightv;
Serial.
print(
"Left=");
Serial.
println(left,
DEC);
if (left == CHIPS || left <= 0) {
generate_left_wave();
Serial.
println(
"Left.");
ltrailc = colours[8];
}
if (right < 0 || right == CHIPS) {
generate_right_wave();
Serial.
println(
"Right.");
rtrailc = colours[8];
}
strip[left-leftv] = ltrailc;
strip[left] = leftc;
strip[right-rightv] = rtrailc;
strip[right] = rightc;
if (
abs(left-right)<2) {
leftv = -leftv;
rightv = -rightv;
avgcolour(&leftc, &rightc, <railc);
rtrailc = ltrailc;
strip[left].r = 254;
strip[left].g = 254;
strip[left].b = 254;
}
if (left <= 0 && leftv < 0)
generate_left_wave();
if (right >= CHIPS && rightv > 0)
generate_right_wave();
for (i=0; i<5; i++) pushStrip();
}