Close

Lightning Neopixel Code "Borrowed" and Mod'd from James Bruce

A project log for Storm in a Box

IONYC and VBGC Project for MFNYC

Vije MillerVije Miller 09/17/2018 at 16:290 Comments
// Lighting Cloud
// James Bruce

#include "FastLED.h"

#define NUM_LEDS 70
#define DATA_PIN 3

enum Mode {CLOUD, ACID, OFF, ON, RED, GREEN, BLUE, FADE};
Mode mode = CLOUD;
Mode lastMode = CLOUD;

int fade_h;
int fade_direction = 1;

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  Serial.begin(9600);
}

void loop() {

constant_lightning();

}

void single_colour(int H) {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CHSV( H, 255, 255);
  }
  if (lastMode != mode) {
    FastLED.show();
    lastMode = mode;
  }
  delay(50);
}

void colour_fade() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CHSV( fade_h, 255, 255);
  }
  if (fade_h > 254) {
    fade_direction = -1;
  }
  else if (fade_h < 0) {
    fade_direction = 1;
  }
  fade_h += fade_direction;
  FastLED.show();
  delay(100);
}

void reset() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CHSV( 0, 0, 0);
  }
  FastLED.show();
}

void acid_cloud() {
  for (int i = 0; i < NUM_LEDS; i++) {
    if (random(0, 100) > 90) {
      leds[i] = CHSV( random(0, 255), 255, 255);
    }
    else {
      leds[i] = CHSV(0, 0, 0);
    }
  }
  FastLED.show();
  delay(random(5, 100));
  reset();
  //}
}

void rolling() {
  for (int r = 0; r < random(2, 10); r++) {
    for (int i = 0; i < NUM_LEDS; i++) {
      if (random(0, 100) > 90) {
        leds[i] = CHSV( 0, 0, 255);
      }
      else {
        leds[i] = CHSV(0, 0, 0);
      }
    }
    FastLED.show();
    delay(random(5, 100));
    reset();
  }
}

void crack() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CHSV( 0, 0, 255);
  }
  FastLED.show();
  delay(random(10, 100));
  reset();
}

void thunderburst() {
  int rs1 = random(0, NUM_LEDS / 2);
  int rl1 = random(10, 20);
  int rs2 = random(rs1 + rl1, NUM_LEDS);
  int rl2 = random(10, 20);
  for (int r = 0; r < random(3, 6); r++) {
    for (int i = 0; i < rl1; i++) {
      leds[i + rs1] = CHSV( 0, 0, 255);
    }
    if (rs2 + rl2 < NUM_LEDS) {
      for (int i = 0; i < rl2; i++) {
        leds[i + rs2] = CHSV( 0, 0, 255);
      }
    }
    FastLED.show();
    delay(random(10, 50));
    reset();
    delay(random(10, 50));
  }
}

void constant_lightning() {
  switch (random(1, 10)) {
    case 1:
      thunderburst();
      delay(random(10, 500));
      Serial.println("Thunderburst");
      break;
    case 2:
      rolling();
      Serial.println("Rolling");
      break;
    case 3:
      crack();
      delay(random(50, 250));
      Serial.println("Crack");
      break;
  }
}

Discussions