Skip to content

Instantly share code, notes, and snippets.

@bnolan
Created January 18, 2021 07:57
Show Gist options
  • Save bnolan/4601497be80a17d6ba9bf4c51e668d4f to your computer and use it in GitHub Desktop.
Save bnolan/4601497be80a17d6ba9bf4c51e668d4f to your computer and use it in GitHub Desktop.
triangle controller
#include "FastLED.h"
#include <EEPROM.h>
// How many leds in your strip?
#define NUM_LEDS PER_PIN * 2
#define PER_PIN SEGMENT * 3
#define SEGMENT 20 * 5
#define NUM_SEGMENTS 3 * 2
// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 3
#define MODES 10
#define ANALOG_1 A2
#define ANALOG_2 A3
// Define the array of leds
CRGB leds[NUM_LEDS];
#define eeAddress 0 //Location we want the data to be put.
byte mode = 0;
byte rate = 0;
byte brightness = 255;
void setup() {
//One simple call, with the address first and the object second.
mode = EEPROM.read(eeAddress);
mode = (mode + 1) % MODES;
EEPROM.put(eeAddress, mode);
FastLED.addLeds<WS2811, DATA_PIN + 0, RGB>(leds + PER_PIN * 0, PER_PIN);
FastLED.addLeds<WS2811, DATA_PIN + 1, RGB>(leds + PER_PIN * 1, PER_PIN);
}
byte segment = 0;
bool trigger;
byte beat;
byte lastBeat;
void mode0 () {
if (trigger) {
segment = random8() % NUM_SEGMENTS;
return;
}
fill_solid(leds + SEGMENT * segment, SEGMENT, CRGB::White);
}
void mode1 () {
for (byte i =0; i < rate; i++) {
int index = random16(NUM_LEDS);
leds[index] = CRGB::White;
}
}
void mode2 () {
if (trigger) {
segment = (segment + 1) % NUM_SEGMENTS;
}
fill_solid(leds + SEGMENT * segment, SEGMENT, CRGB::White);
}
void mode3 () {
if (beat < 32) {
fill_solid(leds, NUM_LEDS, CRGB::White);
}
}
CRGB colors[] = { CRGB::Red, CRGB::Green, CRGB::Blue };
byte offset = 0;
void mode4 () {
if (trigger) {
offset++;
}
fill_solid(leds + SEGMENT * 0, SEGMENT, colors[(0 + offset) % 3]);
fill_solid(leds + SEGMENT * 1, SEGMENT, colors[(1 + offset) % 3]);
fill_solid(leds + SEGMENT * 2, SEGMENT, colors[(2 + offset) % 3]);
}
void mode5 () {
int offset = map(beat, 0, 255, 0, PER_PIN);
for (int i = offset; i = offset + 20; i++) {
leds[i % NUM_LEDS] = CRGB::White;
}
}
void mode6 () {
fill_rainbow(leds, NUM_LEDS, beat);
}
void mode7 () {
if (beat < 85) {
fill_solid(leds, NUM_LEDS, CRGB::Red);
} else if (beat < 170) {
fill_solid(leds, NUM_LEDS, CRGB::Green);
} else {
fill_solid(leds, NUM_LEDS, CRGB::Blue);
}
}
void loop() {
fill_solid(leds, NUM_LEDS, CRGB::Black);
int value = analogRead(ANALOG_1);
rate = map(value, 0, 1023, 0, 255);
value = analogRead(ANALOG_2);
brightness = map(value, 0, 1023, 0, 255);
FastLED.setBrightness(brightness);
beat = beat8(rate);
if (beat > 128 && lastBeat < 128) {
trigger = true;
}
lastBeat = beat;
switch (mode) {
case 0:
mode0();
break;
case 1:
mode1();
break;
case 2:
mode2();
break;
case 3:
mode3();
break;
case 4:
mode4();
break;
case 5:
mode5();
break;
case 6:
mode6();
break;
case 7:
mode7();
break;
}
FastLED.show();
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment