mirror of
https://github.com/caperren/project_archives.git
synced 2025-11-08 13:31:14 +00:00
Github cleanup
This commit is contained in:
@@ -0,0 +1,265 @@
|
||||
void rainbow_cyclical() {
|
||||
FRAMES_PER_SECOND = 120;
|
||||
EVERY_N_MILLISECONDS( 20 ) {
|
||||
gHue++;
|
||||
}
|
||||
fill_rainbow( leds, NUM_LEDS, gHue, 7);
|
||||
}
|
||||
|
||||
void rainbow_full() {
|
||||
FRAMES_PER_SECOND = 30;
|
||||
EVERY_N_MILLISECONDS( 20 ) {
|
||||
gHue++;
|
||||
}
|
||||
|
||||
for (int i = 0 ; i < NUM_LEDS ; i++) {
|
||||
leds[i] = CHSV(gHue, 255, 255);
|
||||
}
|
||||
}
|
||||
|
||||
void rainbow_cyclical_WithGlitter()
|
||||
{
|
||||
// built-in FastLED rainbow, plus some random sparkly glitter
|
||||
rainbow_cyclical();
|
||||
addGlitter(25);
|
||||
}
|
||||
|
||||
void addGlitter( fract8 chanceOfGlitter)
|
||||
{
|
||||
if ( random8() < chanceOfGlitter) {
|
||||
leds[ random16(NUM_LEDS) ] += CRGB::White;
|
||||
}
|
||||
}
|
||||
|
||||
void confetti()
|
||||
{
|
||||
FRAMES_PER_SECOND = 60;
|
||||
EVERY_N_MILLISECONDS( 20 ) {
|
||||
gHue++;
|
||||
}
|
||||
// random colored speckles that blink in and fade smoothly
|
||||
fadeToBlackBy( leds, NUM_LEDS, 10);
|
||||
int pos = random16(NUM_LEDS);
|
||||
leds[pos] += CHSV( gHue + random8(64), 200, 255);
|
||||
}
|
||||
|
||||
void sinelon()
|
||||
{
|
||||
FRAMES_PER_SECOND = 120;
|
||||
EVERY_N_MILLISECONDS( 20 ) {
|
||||
gHue++;
|
||||
}
|
||||
// a colored dot sweeping back and forth, with fading trails
|
||||
fadeToBlackBy( leds, NUM_LEDS, 20);
|
||||
int pos = beatsin16(13, 0, NUM_LEDS);
|
||||
leds[pos] += CHSV( gHue, 255, 192);
|
||||
}
|
||||
|
||||
void nextPattern() {
|
||||
// add one to the current pattern number, and wrap around at the end
|
||||
gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
|
||||
all_black();
|
||||
}
|
||||
|
||||
void bpm()
|
||||
{
|
||||
FRAMES_PER_SECOND = 120;
|
||||
EVERY_N_MILLISECONDS( 20 ) {
|
||||
gHue++;
|
||||
}
|
||||
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
|
||||
uint8_t BeatsPerMinute = 62;
|
||||
CRGBPalette16 palette = PartyColors_p;
|
||||
uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
|
||||
for ( int i = 0; i < NUM_LEDS; i++) { //9948
|
||||
leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
|
||||
}
|
||||
}
|
||||
|
||||
void juggle() {
|
||||
FRAMES_PER_SECOND = 120;
|
||||
EVERY_N_MILLISECONDS( 20 ) {
|
||||
gHue++;
|
||||
}
|
||||
// eight colored dots, weaving in and out of sync with each other
|
||||
fadeToBlackBy( leds, NUM_LEDS, 20);
|
||||
byte dothue = 0;
|
||||
for ( int i = 0; i < 8; i++) {
|
||||
leds[beatsin16(i + 7, 0, NUM_LEDS)] |= CHSV(dothue, 200, 255);
|
||||
dothue += 32;
|
||||
}
|
||||
}
|
||||
|
||||
void northern_lights_cyclical() {
|
||||
static char nlight_dir = 1;
|
||||
const unsigned int nlight_max = 200;
|
||||
const unsigned int nlight_min = 95;
|
||||
FRAMES_PER_SECOND = 40;
|
||||
|
||||
if (gHue > nlight_min) {
|
||||
for (int i = 0 ; i < NUM_LEDS ; i++) {
|
||||
leds[i] = CHSV(constrain(gHue + i, nlight_min, nlight_max), 255, 255);
|
||||
}
|
||||
|
||||
if (gHue > nlight_max) {
|
||||
nlight_dir = 0;
|
||||
}
|
||||
} else {
|
||||
for (int i = 0 ; i < NUM_LEDS ; i++) {
|
||||
leds[i] = CHSV(constrain(gHue + i, nlight_min, nlight_max), 255, 255);
|
||||
}
|
||||
if ((gHue + NUM_LEDS) < nlight_min) {
|
||||
nlight_dir = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (nlight_dir) {
|
||||
gHue++;
|
||||
} else {
|
||||
gHue--;
|
||||
}
|
||||
|
||||
if (use_serial) {
|
||||
Serial.print("\tHue: ");
|
||||
Serial.println(gHue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void northern_lights_random() {
|
||||
const unsigned int nlight_max = 200;
|
||||
const unsigned int nlight_min = 95;
|
||||
FRAMES_PER_SECOND = 240;
|
||||
|
||||
int pos = random16(NUM_LEDS);
|
||||
leds[pos] = CHSV( nlight_min + random8(nlight_max - nlight_min), 255, 255);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void northern_lights() {
|
||||
static char nlight_dir = 1;
|
||||
const unsigned int nlight_max = 200;
|
||||
const unsigned int nlight_min = 95;
|
||||
FRAMES_PER_SECOND = 8;
|
||||
|
||||
if (gHue > nlight_max) {
|
||||
gHue = nlight_max;
|
||||
nlight_dir = 0;
|
||||
} else if (gHue < nlight_min) {
|
||||
gHue = nlight_min;
|
||||
nlight_dir = 1;
|
||||
}
|
||||
|
||||
for (int i = 0 ; i < NUM_LEDS ; i++) {
|
||||
leds[i] = CHSV(gHue, 255, 255);
|
||||
}
|
||||
|
||||
if (nlight_dir) {
|
||||
gHue++;
|
||||
} else {
|
||||
gHue--;
|
||||
}
|
||||
|
||||
if (use_serial) {
|
||||
Serial.print("Hue is: ");
|
||||
Serial.println(gHue);
|
||||
}
|
||||
}
|
||||
|
||||
void northern_lights_flowy() {
|
||||
const unsigned char min_hue = 95;
|
||||
const unsigned char max_hue = 200;
|
||||
const unsigned char color_points[] = {min_hue, 147, max_hue};
|
||||
const unsigned char step_size = 2;
|
||||
static float percent = 0.0;
|
||||
static char current_point = 0;
|
||||
static unsigned char current_led = 0;
|
||||
FRAMES_PER_SECOND = 120;
|
||||
//fill_gradient(leds, NUM_LEDS, CHSV(min_hue, 255, 255), CHSV(max_hue, 255, 255));
|
||||
|
||||
if (gHue > max_hue) {
|
||||
gHue = min_hue;
|
||||
} else if (gHue < min_hue) {
|
||||
gHue = min_hue;
|
||||
}
|
||||
|
||||
if (current_led > NUM_LEDS) {
|
||||
current_led = 0;
|
||||
}
|
||||
|
||||
leds[current_led] = CHSV(gHue, 255, 255);
|
||||
|
||||
current_led++;
|
||||
gHue += step_size;
|
||||
// for (int i = 0 ; i < NUM_LEDS ; i++) {
|
||||
// if ((percent + step_size) > 1.0) {
|
||||
// percent = 0;
|
||||
// current_point++;
|
||||
// if (current_point > ARRAY_SIZE(color_points)) {
|
||||
// current_point = 0;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// percent = (percent + step_size);
|
||||
// leds[i] = blend(CHSV(color_points[current_point % ARRAY_SIZE(color_points)], 255, 255), CHSV(color_points[(current_point + 1) % ARRAY_SIZE(color_points)], 255, 255), percent);
|
||||
// if (use_serial) {
|
||||
// Serial.print("Color one: ");
|
||||
// Serial.print(color_points[current_point % ARRAY_SIZE(color_points)]);
|
||||
// Serial.print("\tColor two: ");
|
||||
// Serial.print(color_points[(current_point + 1) % ARRAY_SIZE(color_points)]);
|
||||
// Serial.print("\tPercent of two: ");
|
||||
// Serial.println(percent);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
void christmas_no_yellow() {
|
||||
const char christmas_colors[] = {0, 96, 160}; //Red 0, Green 85, Blue 170, Yellow 43, Orange 21
|
||||
FRAMES_PER_SECOND = 30;
|
||||
//fadeToBlackBy( leds, NUM_LEDS, 10);
|
||||
int pos = random16(NUM_LEDS);
|
||||
leds[pos] = CHSV( christmas_colors[random8(ARRAY_SIZE(christmas_colors))], 255, 255);
|
||||
}
|
||||
|
||||
void christmas_with_yellow() {
|
||||
const char christmas_colors[] = {0, 96, 160, 64}; //Red 0, Green 85, Blue 170, Yellow 43, Orange 21
|
||||
FRAMES_PER_SECOND = 30;
|
||||
//fadeToBlackBy( leds, NUM_LEDS, 10);
|
||||
int pos = random16(NUM_LEDS);
|
||||
leds[pos] = CHSV( christmas_colors[random8(ARRAY_SIZE(christmas_colors))], 255, 255);
|
||||
}
|
||||
|
||||
void lightning() {
|
||||
static uint8_t frequency = 50; // controls the interval between strikes
|
||||
static uint8_t flashes = 8; //the upper limit of flashes per strike
|
||||
static unsigned int dimmer = 1;
|
||||
|
||||
static uint8_t ledstart; // Starting location of a flash
|
||||
static uint8_t ledlen;
|
||||
FRAMES_PER_SECOND = 120;
|
||||
|
||||
ledstart = random8(NUM_LEDS); // Determine starting location of flash
|
||||
ledlen = random8(NUM_LEDS - ledstart); // Determine length of flash (not to go beyond NUM_LEDS-1)
|
||||
for (int flashCounter = 0; flashCounter < random8(3, flashes); flashCounter++) {
|
||||
if (flashCounter == 0) dimmer = 5; // the brightness of the leader is scaled down by a factor of 5
|
||||
else dimmer = random8(1, 3); // return strokes are brighter than the leader
|
||||
fill_solid(leds + ledstart, ledlen, CHSV(255, 0, 255 / dimmer));
|
||||
FastLED.show(); // Show a section of LED's
|
||||
delay(random8(4, 10)); // each flash only lasts 4-10 milliseconds
|
||||
fill_solid(leds + ledstart, ledlen, CHSV(255, 0, 0)); // Clear the section of LED's
|
||||
FastLED.show();
|
||||
if (flashCounter == 0) delay (150); // longer delay until next flash after the leader
|
||||
delay(50 + random8(100)); // shorter delay between strokes
|
||||
} // for()
|
||||
delay(random8(frequency) * 100); // delay between strikes
|
||||
}
|
||||
|
||||
void all_black() {
|
||||
for (int i = 0 ; i < NUM_LEDS ; i++) {
|
||||
leds[i] = CRGB::Black;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
void attempt_serial_init() {
|
||||
Serial.begin(9600);
|
||||
|
||||
long start_time = millis();
|
||||
bool trying_serial = true;
|
||||
|
||||
while (trying_serial) {
|
||||
if (Serial) {
|
||||
Serial.println("[Serial Debugging Enabled]");
|
||||
Serial.println();
|
||||
delay(1000);
|
||||
use_serial = true;
|
||||
trying_serial = false;
|
||||
} else {
|
||||
if ((millis() - start_time) > serial_timeout) {
|
||||
Serial.end();
|
||||
use_serial = false;
|
||||
trying_serial = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void init_buttons() {
|
||||
pinMode(ENCODER_BUTTON_PIN, INPUT_PULLUP);
|
||||
pinMode(ON_OFF_BUTTON_PIN, INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(ENCODER_BUTTON_PIN), on_encoder_button_pressed, FALLING);
|
||||
attachInterrupt(digitalPinToInterrupt(ON_OFF_BUTTON_PIN), on_off_button_changed, CHANGE);
|
||||
if (use_serial) {
|
||||
Serial.println("Button Initialization Complete");
|
||||
}
|
||||
}
|
||||
|
||||
void init_lights() {
|
||||
FastLED.addLeds<WS2801, RGB>(leds, NUM_LEDS);
|
||||
FastLED.setBrightness(led_brightness);
|
||||
if (use_serial) {
|
||||
Serial.println("LED Initialization Complete");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
#define TEN_MIN_MILLIS 600000
|
||||
|
||||
void on_encoder_button_pressed() {
|
||||
if (!encoder_button_pressed) {
|
||||
encoder_button_pressed = true;
|
||||
}
|
||||
}
|
||||
|
||||
void on_off_button_changed() {
|
||||
static unsigned long start_time = millis();
|
||||
static bool button_down = false;
|
||||
//((long)(millis() - timer_end_time) >= 0)
|
||||
if (!button_down) {
|
||||
if (!digitalRead(ON_OFF_BUTTON_PIN)) {
|
||||
start_time = millis();
|
||||
button_down = true;
|
||||
delay(button_debounce);
|
||||
}
|
||||
|
||||
} else {
|
||||
if ((millis() - start_time) < timer_threshold) {
|
||||
if (!on_off_button_pressed) {
|
||||
on_off_button_pressed = true;
|
||||
}
|
||||
} else {
|
||||
if (leds_on) {
|
||||
if (!on_off_timer_pressed) {
|
||||
on_off_timer_pressed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
button_down = false;
|
||||
}
|
||||
}
|
||||
|
||||
void check_on_off() {
|
||||
if (on_off_button_pressed) {
|
||||
leds_on = !leds_on;
|
||||
delay(button_debounce);
|
||||
on_off_button_pressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
void check_brightness_change() {
|
||||
long encoder_position = myEnc.read();
|
||||
if (encoder_position != encoder_position_previous) {
|
||||
led_brightness += (encoder_position - encoder_position_previous);
|
||||
led_brightness = constrain(led_brightness, 2, 255);
|
||||
encoder_position_previous = encoder_position;
|
||||
FastLED.setBrightness(led_brightness);
|
||||
if (use_serial) {
|
||||
Serial.print("Brightness Changed To: ");
|
||||
Serial.println(led_brightness);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void freeze_encoder() {
|
||||
encoder_position_previous = myEnc.read();
|
||||
}
|
||||
|
||||
void check_mode_change() {
|
||||
if (encoder_button_pressed) {
|
||||
nextPattern();
|
||||
delay(button_debounce);
|
||||
encoder_button_pressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
void check_timer() {
|
||||
if (on_off_timer_pressed) {
|
||||
|
||||
delay(button_debounce);
|
||||
|
||||
int temp = 4;
|
||||
long ten_min_intervals = 1;
|
||||
bool edit_timer = true;
|
||||
|
||||
while ((!on_off_button_pressed) && edit_timer) {
|
||||
long encoder_position = myEnc.read();
|
||||
if (encoder_position != encoder_position_previous) {
|
||||
temp += (encoder_position - encoder_position_previous);
|
||||
temp = constrain(temp, 4, 255);
|
||||
ten_min_intervals = map(temp, 4, 255, 1, 50);
|
||||
encoder_position_previous = encoder_position;
|
||||
}
|
||||
|
||||
all_black();
|
||||
for (int i = 0 ; i < ten_min_intervals ; i++) {
|
||||
leds[i] = CRGB::Blue;
|
||||
}
|
||||
FastLED.show();
|
||||
|
||||
if (encoder_button_pressed) {
|
||||
run_timer = false;
|
||||
on_off_button_pressed = false;
|
||||
encoder_button_pressed = false;
|
||||
edit_timer = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (on_off_button_pressed) {
|
||||
timer_end_time = millis() + (TEN_MIN_MILLIS * ten_min_intervals);
|
||||
|
||||
if (use_serial) {
|
||||
Serial.print("Timer End Time: ");
|
||||
Serial.println(timer_end_time);
|
||||
}
|
||||
run_timer = true;
|
||||
on_off_button_pressed = false;
|
||||
}
|
||||
|
||||
on_off_timer_pressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
void check_timer_elapsed() {
|
||||
if (run_timer) {
|
||||
if ((long)(millis() - timer_end_time) >= 0) {
|
||||
leds_on = false;
|
||||
run_timer = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
#include <FastLED.h>
|
||||
#include <Encoder.h>
|
||||
|
||||
////////// LED Variables & Instantiations //////////
|
||||
const int NUM_LEDS = 50;
|
||||
CRGB leds[NUM_LEDS];
|
||||
int led_brightness = 255 / 2;
|
||||
volatile bool leds_on = true;
|
||||
|
||||
////////// Encoder Variables & Instantiations //////////
|
||||
Encoder myEnc(0, 1);
|
||||
long encoder_position_previous = 0;
|
||||
|
||||
////////// Button Variables & Instantiations //////////
|
||||
const int ENCODER_BUTTON_PIN = 2;
|
||||
const int ON_OFF_BUTTON_PIN = 3;
|
||||
|
||||
volatile bool encoder_button_pressed = false;
|
||||
volatile bool on_off_button_pressed = false;
|
||||
volatile bool on_off_timer_pressed = false;
|
||||
|
||||
|
||||
////////// Serial Variables & Instantiations //////////
|
||||
const int serial_timeout = 5000; //Timeout in ms for attempting serial debugging
|
||||
bool use_serial = false;
|
||||
|
||||
|
||||
////////// General Variables & Instantiations //////////
|
||||
const unsigned int button_debounce = 400;
|
||||
|
||||
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
|
||||
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
|
||||
|
||||
unsigned int FRAMES_PER_SECOND = 120;
|
||||
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
|
||||
|
||||
////////// Timer Variables & Instantiations //////////
|
||||
const unsigned int timer_threshold = 1000; //
|
||||
bool run_timer = false;
|
||||
unsigned long timer_end_time;
|
||||
|
||||
////////// Prototypes for display patterns //////////
|
||||
void rainbow_cyclical();
|
||||
void rainbow_cyclical_WithGlitter();
|
||||
void rainbow_full();
|
||||
void confetti();
|
||||
void sinelon();
|
||||
void bpm();
|
||||
void juggle();
|
||||
void northern_lights();
|
||||
void northern_lights_flowy();
|
||||
void northern_lights_cyclical();
|
||||
void northern_lights_random();
|
||||
void christmas_with_yellow();
|
||||
void lightning();
|
||||
|
||||
// List of patterns to cycle through. Each is defined as a separate function below.
|
||||
typedef void (*SimplePatternList[])();
|
||||
SimplePatternList gPatterns = {
|
||||
|
||||
northern_lights,
|
||||
northern_lights_flowy,
|
||||
northern_lights_cyclical,
|
||||
northern_lights_random,
|
||||
rainbow_cyclical,
|
||||
rainbow_cyclical_WithGlitter,
|
||||
rainbow_full,
|
||||
confetti,
|
||||
lightning,
|
||||
sinelon,
|
||||
juggle,
|
||||
christmas_with_yellow
|
||||
|
||||
};
|
||||
|
||||
void setup() {
|
||||
attempt_serial_init();
|
||||
init_buttons();
|
||||
init_lights();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
check_on_off();
|
||||
check_timer_elapsed();
|
||||
if (leds_on){
|
||||
check_timer();
|
||||
check_mode_change();
|
||||
check_brightness_change();
|
||||
gPatterns[gCurrentPatternNumber]();
|
||||
FastLED.show();
|
||||
FastLED.delay(1000/FRAMES_PER_SECOND);
|
||||
|
||||
} else {
|
||||
freeze_encoder();
|
||||
all_black();
|
||||
FastLED.show();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user