// lightningbolt.ino
#define DEBUG 1 // print count
#include <FastLED.h> // https://github.com/FastLED/FastLED
#define CASES 9 // number of functions available
#define NUM_LEDS 49
#define MINUTES .167 // 10 seconds
// #define MINUTES 10
#define SECONDS 60
unsigned long timer, timeout = MINUTES * SECONDS * 1000UL; // 10 minutes timer
#define MATRIX_PIN 6 // Uno/Nano (up to 622 WS2812)
// #define MATRIX_PIN 0 // ATtiny88 (only 110 WS2812)
#define MAXBRIGHT 128
CRGB leds[NUM_LEDS];
// volatile bool button; // type volatile for interrupt handler
// int buttonPin = 2; // interrupt pin INT0
// bool buttonPressed, currentButtonState;
int count = 0; // sequence count
int color_stage = 0, color_stages = 6; // color stages for zero.h
int minimum = 0, maximum = 255, increas, decreas; // counters - misspelled to format words
unsigned long fadeTimer, fadeTimeout = 3900; // microseconds = 3.9 milliseconds
// #include "button.h"
// #include "marquee2.h"
// #include "marquee3.h"
// #include "rainbowfade.h"
// #include "runner.h"
// #include "sparkle.h"
// #include "sparklegap.h"
// #include "throb.h"
// #include "trace.h"
// #include "zap.h"
// #include "zero.h"
// #include "zzzt.h"
void setup() {
// configureButtonInterrupt();
if (DEBUG) {
Serial.begin(115200);
Serial.print(count);
}
randomSeed(analogRead(A0));
pinMode(LED_BUILTIN, OUTPUT);
FastLED.addLeds<WS2812B, MATRIX_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(MAXBRIGHT); // Adjust the brightness value as needed
FastLED.clear();
FastLED.show();
}
void loop() {
// readbutton();
heartbeat();
switch (count) {
case 0: zero(); break; // static roygcbm
case 1: trace(); break;
case 2: zap(); break;
case 3: marquee2(); break;
case 4: zzzt(); break;
case 5: throb(); break;
case 6: marquee3(); break;
case 7: sparklegap(); break;
case 8: runner(); break;
case 9: sparkle(); break;
default: break;
}
if (millis() - timer > timeout) {
timer = millis();
FastLED.clear();
FastLED.show();
count++;
if (count > CASES) {
Serial.print(".");
count = 1;
}
if (DEBUG)
Serial.print(count);
}
}
// void buttonISR() {
// noInterrupts(); // disable interrupts while in the ISR
// button = 1; // set the flag that the button was pressed
// interrupts(); // enable interrupts when leaving the ISR
// }
// void configureButtonInterrupt() {
// pinMode(buttonPin, INPUT_PULLUP);
// attachInterrupt(digitalPinToInterrupt(buttonPin), buttonISR, FALLING); // INT#, ISR, TRIGGER
// Serial.print("Press the button to see other animations.\nAnimation: ");
// }
void heartbeat() {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // heartbeat
}
void marquee2() {
int local = 200, dimmer = 1;
for (int i = 0; i < NUM_LEDS; i += 2) {
leds[i] = CRGB(MAXBRIGHT / dimmer, MAXBRIGHT / dimmer, MAXBRIGHT / dimmer);
}
FastLED.show();
delay(local);
FastLED.clear();
for (int i = 1; i < NUM_LEDS; i += 2) {
leds[i] = CRGB(MAXBRIGHT / dimmer, MAXBRIGHT / dimmer, MAXBRIGHT / dimmer);
}
FastLED.show();
delay(local);
FastLED.clear();
}
void marquee3() {
int delay1 = 125, dimmer = 1;
for (int j = 0; j < 3; j++) {
for (int i = 0; i < NUM_LEDS - 3; i += 3) {
leds[i + j] = CRGB(MAXBRIGHT / dimmer, MAXBRIGHT / dimmer, MAXBRIGHT / dimmer);
}
FastLED.show();
FastLED.clear();
delay(delay1);
}
}
void rainbowfade(int r, int g, int b) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(r, g, b);
}
}
void runner() {
int r = random(128, 256), g = random(128, 256), b = random(128, 256);
int x = random(2), y = random(2), z = random(2);
if (x == 0 && y == 0 && z == 0) x = 1, y = 1, z = 1; // if all multipliers are zero, change to "1" for light
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(r * x, g * y, b * z);
FastLED.show();
delay(1);
leds[i] = CRGB(0, 0, 0);
FastLED.show();
}
}
void sparkle() {
FastLED.clear();
int dimmer = 2, pix = random(NUM_LEDS);
leds[pix] = CRGB(MAXBRIGHT / dimmer, MAXBRIGHT / dimmer, MAXBRIGHT / dimmer);
if (pix < NUM_LEDS - 1)
leds[pix + 1] = CRGB(MAXBRIGHT / dimmer, MAXBRIGHT / dimmer, MAXBRIGHT / dimmer);
FastLED.show();
delay(random(50, 100));
FastLED.clear();
delay(random(25));
FastLED.show();
}
void sparklegap() { // 5 - one random gap in the pixels
int dimmer = 2;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(MAXBRIGHT / dimmer, MAXBRIGHT / dimmer, MAXBRIGHT / dimmer);
}
FastLED.show();
delay(random(100, 300));
int gap = 5;
int x = random(NUM_LEDS - gap);
for (int i = 0; i < gap; i++)
leds[x + i] = CRGB(0, 0, 0);
FastLED.show();
delay(random(100, 300));
leds[x] = CRGB(MAXBRIGHT / dimmer, MAXBRIGHT / dimmer, MAXBRIGHT / dimmer);
FastLED.show();
}
void throb() {
int delayer = 10, dimmer = 1;
for (int j = 25; j < MAXBRIGHT / dimmer; j++) { // fade-in quickly
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(j, j, j);
}
FastLED.show();
delay(delayer);
}
for (int j = MAXBRIGHT / dimmer; j > 25; j--) { // fade-out slowly
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(j, j, j);
}
FastLED.show();
delay(delayer);
}
}
void trace() {
for (int j = 0; j < 2; j++) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(j * MAXBRIGHT, j * MAXBRIGHT, j * MAXBRIGHT);
FastLED.show();
}
}
}
void zap() {
int half_leds = NUM_LEDS / 2;
for (int i = 0; i < half_leds + 1; i++) {
leds[half_leds + i] = CRGB(MAXBRIGHT, MAXBRIGHT, MAXBRIGHT);
leds[half_leds - i] = CRGB(MAXBRIGHT, MAXBRIGHT, MAXBRIGHT);
FastLED.show();
}
delay(random(10) * 100);
for (int i = 0; i < half_leds + 1; i++) {
leds[(NUM_LEDS - 1) - i] = CRGB(0, 0, 0);
leds[i] = CRGB(0, 0, 0);
FastLED.show();
}
delay(random(1000, 1500));
}
void zero() {
leds[0] = CRGB(255, 0, 0); // RED
leds[1] = CRGB(255, 128, 0); // org
leds[2] = CRGB(255, 255, 0); // YEL
leds[3] = CRGB(128, 255, 0); // yg
leds[4] = CRGB(0, 255, 0); // GRN
leds[5] = CRGB(0, 255, 128); // mnt
leds[6] = CRGB(0, 255, 255); // CYN
leds[7] = CRGB(0, 128, 255); // azr
leds[8] = CRGB(0, 0, 255); // BLU
leds[9] = CRGB(128, 0, 255); // IND
leds[10] = CRGB(255, 0, 255); // MAG
leds[11] = CRGB(255, 0, 128); // pnk
if (micros() - fadeTimer > fadeTimeout) {
fadeTimer = micros();
if (increas++ == maximum) { // increment step until counter is max
increas = minimum; // reset step counter
if (color_stage++ == color_stages) // increment stage until stage is max
color_stage = 1; // reset stage
}
decreas = maximum - increas; // invert counter to decrease values
switch (color_stage) { // blend colors one bit per step
case 0: leds[12] = CRGB(increas, minimum, minimum); break; // blk (0,0,0) to red (1,0,0) - blk one time only
case 1: leds[12] = CRGB(maximum, increas, minimum); break; // red (1,0,0) to yel (1,1,0)
case 2: leds[12] = CRGB(decreas, maximum, minimum); break; // yel (1,1,0) to grn (0,1,0)
case 3: leds[12] = CRGB(minimum, maximum, increas); break; // grn (0,1,0) to cyn (0,1,1)
case 4: leds[12] = CRGB(minimum, decreas, maximum); break; // cyn (0,1,1) to blu (0,0,1)
case 5: leds[12] = CRGB(increas, minimum, maximum); break; // blu (0,0,1) to mag (1,0,1)
case 6: leds[12] = CRGB(maximum, minimum, decreas); break; // mag (1,0,1) to red (1,0,0)
default: break;
}
FastLED.show();
}
}
void zzzt() {
int dimmer = 2;
for (int i = 0; i < NUM_LEDS; i++)
leds[i] = CRGB(MAXBRIGHT / dimmer, MAXBRIGHT / dimmer, MAXBRIGHT / dimmer);
FastLED.show();
delay(random(300, 3000));
for (int i = 0; i < NUM_LEDS; i++)
leds[i] = CRGB(0, 0, 0);
FastLED.show();
delay(random(50, 100));
}
/*
WS2812B - VCC - 1000uF ECAP(+) - Arduino VIN - Power supply (+)
- SIG - 300-500 Ohm resistor - Arduino SIG - x
- GND - 1000uF ECAP(-) - Arduino GND - Power supply (-)
+----------| USB |--------+ +-----+
| D13/SCK MISO/D12 | |E.CAP|
| 3.3V MOSI/D11~| |1k uF|
| Vref SS/D10~| +-----+
| A0 D9~| -| |+ +--------------+
| A1 NANO D8 | +----|-----+---------| VCC |
| A2 D7 | | +--+---------------| GND |
| A3 D6~|--|-|-----|470R Ohm|---| SIG WS2812B |
| A4/SDA D5~| | | +--------------+
| A5/SCL D4 | | |
| A6 INT1/D3~| | | +--------+
| A7 INT0/D2 |--|-|---| BUTTON |
| 5V GND |--|-|---| |=|
| RST RST | | | +--------+
+----| GND 5V DO GND TX1 | | |
| +--| Vin DI SCK RST RX1 | | |
| | +-------------------------+ | | +--------------+
| | | | | POWER SUPPLY |====||= MAINS
| +-------------------------------+-|---| VCC |====||= MAINS
+-----------------------------------+---| GND |
+--------------+
*/