Soundboks LED


The whole circuit is powered directly from the Soundboks PCB power domain. The cables from the switch are connected to digital pin 2, so the Arduino can automatically turn on the light in the overdrive mode (position 2). A blue

#include < Adafruit_NeoPixel.h>

#define PIN 5
#define SWITCH 2
#define LED_COUNT 57

Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);

uint8_t current_state = 0;
uint8_t rgb_values[3];
uint8_t state;

void setup() {
  Serial.begin(9600);
  pinMode(SWITCH, INPUT);
  strip.begin();
  current_state = 'e';
  state = 0;
}

void loop() {
  if (!switchOn()) {
    turnOff();
    if (state) {
      state = 0;
    }
    return;
  }

  if (!state) {
    state = 1;
    rainbowCycleAnimation(15);
  }

  while (Serial.available()) {
    uint8_t character = Serial.read();
    switch (character) {
      case 'x':
        turnOff();
        break;
      case 'a': break;
      case 'b': break;
      case 'c': break;
      case 'd': break;
      case 'e': break;
      case 'f': break;
    };
    current_state = character;
  }
  switch (current_state) {
    case 'x':
      break;
    case 'a':
      colorWipeRandom(20);
      break;
    case 'b':
      colorWipeGYR(10);
      break;
    case 'c':
      theaterChaseRandom(20);
      break;
    case 'd':
      theaterChaseRainbow(20);
      break;
    case 'e':
      rainbowCycle(10);
      break;
    case 'f':
      rainbow(20);
      break;
  }
}

uint8_t switchOn() {
  return digitalRead(SWITCH);
}

uint8_t animationEnd() {
  return !switchOn() || Serial.available();
}

void turnOff() {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, strip.Color(0, 0, 0));
  }
  strip.show();
}

void changeColor(uint32_t c) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
  }
  strip.show();
}

void colorWipeRandom(uint8_t wait) {
  uint32_t c = strip.Color(random(255), random(255), random(255));
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    delay(wait);
    strip.show();
    delay(wait);
  }
}

void colorWipeGYR(uint8_t wait) {
  uint32_t c[3] = {strip.Color(0, 255, 0), strip.Color(255, 255, 0), strip.Color(255, 0, 0)};
  for (uint8_t j = 0; j < 3; j++) {
    for (uint16_t i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, c[j]);
      delay(wait);
      strip.show();
      delay(wait);
    }
    if (animationEnd()) {
      return;
    }
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for (j = 0; j < 256; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i + j) & 255));
    }
    strip.show();
    delay(wait);
    if (animationEnd()) {
      return;
    }
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
    if (animationEnd()) {
      return;
    }
  }
}

void rainbowCycleAnimation(uint8_t wait) {
  uint16_t i;

  for (i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, Wheel((i) & 255));
    delay(wait);
    strip.show();
    delay(wait);
  }
}

void theaterChaseRandom(uint8_t wait) {
  for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
    for (int q = 0; q < 3; q++) {
      for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, strip.Color(random(255), random(255), random(255))); //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, 0);      //turn every third pixel off
      }
    }
    if (animationEnd()) {
      return;
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j = 0; j < 256; j++) {   // cycle all 256 colors in the wheel
    for (int q = 0; q < 3; q++) {
      for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, 0);      //turn every third pixel off
      }
    }
    if (animationEnd()) {
      return;
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}4974