Neopixel Array

Schematic of Neopixel Array
// iCircuit playback timing from the working 8 kHz experiment.
// A zero is high for 375 us, a one for 750 us, and each bit takes 1250 us.
const int dataPin = 6;
const int clockPin = 5;
const int latchClocks = 3200;

int idleClocks = 3200;
int red = 0;
int green = 0;
int blue = 0;
int channel = 0;
int bitMask = 128;
int bitPhase = 0;
int highClocks = 0;
int blinkState = HIGH;
bool sending = false;
bool previousClock = false;

int pixel = 0;
int prepared = 0;
int stateA = 63;
int stateB = 178;
int stateC = 193;
int frameParity = 0;
int redTint = 0;
int greenTint = 0;
int blueTint = 0;
// One RGB332 code per pixel. Fine channel offsets are reconstructed.
unsigned char frameColors[600];

void prepareColor() {
  stateA = (stateA * 29) % 251;
  stateB = (stateB * 37) % 241;
  stateC = (stateC * 37) % 239;
  if (prepared == 0) {
    redTint = stateA & 15;
    greenTint = stateB & 15;
    blueTint = (stateA + stateB) & 31;
  }
  int color = (stateA + 3 * stateB + 5 * stateC) & 255;
  int dominant = (stateA + stateB + stateC) % 3;
  if (dominant == 0) color = color | 224;
  else if (dominant == 1) color = color | 28;
  else color = color | 3;
  if (color == 255) color = 254;
  frameColors[prepared] = color;
  prepared++;
}

void loadPixel() {
  int color = frameColors[pixel];
  int r = color & 224;
  int g = color & 28;
  int b = color & 3;
  red = r == 224 ? 255 : r + 2 * ((pixel + redTint) & 15) + frameParity;
  green = g == 28 ? 255 : 8 * g + 2 * (((pixel >> 4) + greenTint) & 15) + frameParity;
  blue = b == 3 ? 255 : 64 * b + 2 * ((pixel + blueTint) & 31) + frameParity;
  channel = 0;
  bitMask = 128;
}

void beginFrame() {
  pixel = 0;
  loadPixel();
}

void startBit() {
  int value = green;
  if (channel == 1) value = red;
  if (channel == 2) value = blue;
  highClocks = (value & bitMask) ? 6 : 3;
  bitPhase = 0;
  digitalWrite(dataPin, HIGH);
}

void setup() {
  pinMode(clockPin, INPUT);
  pinMode(dataPin, OUTPUT);
  digitalWrite(dataPin, LOW);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}

void loop() {
  bool clockHigh = digitalRead(clockPin) == HIGH;
  bool risingEdge = clockHigh && !previousClock;
  previousClock = clockHigh;

  unsigned long now = millis();
  int nextBlink = (now % 1500UL < 1000UL) ? HIGH : LOW;
  if (nextBlink != blinkState) {
    digitalWrite(13, nextBlink);
    blinkState = nextBlink;
  }

  if (!risingEdge) return;
  if (!sending) {
    if (idleClocks > 0) {
      if (prepared < 600) prepareColor();
      idleClocks--;
      return;
    }
    beginFrame();
    sending = true;
    startBit();
    return;
  }

  bitPhase++;
  if (bitPhase == highClocks) digitalWrite(dataPin, LOW);
  if (bitPhase < 10) return;

  bitMask = bitMask >> 1;
  if (bitMask == 0) {
    bitMask = 128;
    channel++;
    if (channel == 3) {
      if (pixel == 599) {
        sending = false;
        idleClocks = latchClocks;
        prepared = 0;
        frameParity = 1 - frameParity;
        return;
      }
      pixel++;
      loadPixel();
    }
  }
  startBit();
}