From 2cbe1feb9b74040e7fa4d3740bd4d159b12643dd Mon Sep 17 00:00:00 2001 From: OroArmor Date: Wed, 31 Jul 2019 15:51:22 -0700 Subject: [PATCH 1/8] Rewriting code as an OOP --- PitLED/LEDFunction.cpp | 21 +++ PitLED/LEDFunction.h | 11 ++ PitLED/PitLED.cpp | 40 ++++++ PitLED/PitLED.h | 18 +++ PitLED/PitLED.ino | 284 +++++++++++++++++++++++++++++++++++++++ PitLED/SwirlFunction.cpp | 41 ++++++ PitLED/SwirlFunction.h | 14 ++ 7 files changed, 429 insertions(+) create mode 100644 PitLED/LEDFunction.cpp create mode 100644 PitLED/LEDFunction.h create mode 100644 PitLED/PitLED.cpp create mode 100644 PitLED/PitLED.h create mode 100644 PitLED/PitLED.ino create mode 100644 PitLED/SwirlFunction.cpp create mode 100644 PitLED/SwirlFunction.h diff --git a/PitLED/LEDFunction.cpp b/PitLED/LEDFunction.cpp new file mode 100644 index 0000000..01f40fa --- /dev/null +++ b/PitLED/LEDFunction.cpp @@ -0,0 +1,21 @@ +#include "Arduino.h" +#include "PitLED.h" +#include + +void LEDFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { + for (int i = 1; i < numStrands; i++) { + for (int j = 0; j < numLEDsPerStrand; j++) { + leds[numLEDsPerStrand * i + j] = leds[j]; + } + } + FastLED.show(); +} + +void LEDFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB colors[], int d) { + Serial.println("Running base red function"); + for (int i = 0; i < numLeds; i++) { + leds[i] = CRGB::Red; + } + show(leds, numLeds, numStrands); + delay(1000); +} diff --git a/PitLED/LEDFunction.h b/PitLED/LEDFunction.h new file mode 100644 index 0000000..4fabd1f --- /dev/null +++ b/PitLED/LEDFunction.h @@ -0,0 +1,11 @@ +#include +#ifndef LEDFUNC_H +#define LEDFUNC_H + +class LEDFunction { + public: + virtual void execute(CRGB* leds, int numLeds,int numStrands, CRGB colors[], int d); + void show(CRGB* leds, int numLeds,int numStrands); +}; + +#endif diff --git a/PitLED/PitLED.cpp b/PitLED/PitLED.cpp new file mode 100644 index 0000000..a8a5f4e --- /dev/null +++ b/PitLED/PitLED.cpp @@ -0,0 +1,40 @@ +#include + +#include "PitLED.h" +#include "LEDFunction.h" + + +PitLED::PitLED(int strands, int ledsPerStrand) { + numStrands = strands; + numLEDsPerStrand = ledsPerStrand; + init(); +} + +void PitLED::init() { + leds = new CRGB[numLEDsPerStrand * numStrands]; + FastLED.addLeds(leds, (int) numLEDsPerStrand * numStrands); + for (int i = 0; i < numLEDsPerStrand * numStrands; i++) { + leds[i] = CRGB::Black; + } + FastLED.show(); + delay(1000); + for (int i = 0; i < numLEDsPerStrand * numStrands; i++) { + leds[i] = CRGB::Red; + } + FastLED.show(); + delay(1000); + for (int i = 0; i < numLEDsPerStrand * numStrands; i++) { + leds[i] = CRGB::Black; + } + FastLED.show(); + delay(1000); +} + +PitLED::~PitLED() { + delete leds; +} + +void PitLED::runFunction(LEDFunction *function, int d) { + CRGB colors[] = {CRGB::Red, CRGB::Green, CRGB::Orange}; + function->execute(leds, numLEDsPerStrand, numStrands, colors, d); +} diff --git a/PitLED/PitLED.h b/PitLED/PitLED.h new file mode 100644 index 0000000..d53bb4e --- /dev/null +++ b/PitLED/PitLED.h @@ -0,0 +1,18 @@ +#include +#include "LEDFunction.h" + +#ifndef PitLED_H +#define PitLED_H + +class PitLED { + int numStrands, numLEDsPerStrand; + CRGB* leds; + public: + PitLED(int strands, int ledsPerStrand); + void init(); + void runFunction(LEDFunction *function, int d); + void show(); + ~PitLED(); +}; + +#endif diff --git a/PitLED/PitLED.ino b/PitLED/PitLED.ino new file mode 100644 index 0000000..c826c10 --- /dev/null +++ b/PitLED/PitLED.ino @@ -0,0 +1,284 @@ +//#include + +#include +#include "PitLED.h" +#include "SwirlFunction.h" +#include "LEDFunction.h" +#define NUM_LEDS 144 +#define DATA_PIN 5 +#define NUM_STRANDS 15 + +CRGB leds[NUM_LEDS * NUM_STRANDS]; + +CRGB CONSTCOLORS[8] = { + CRGB::Red, CRGB::Green, CRGB::Blue, CRGB::Yellow, CRGB::Orange, CRGB::Cyan, CRGB::Purple, CRGB::White +}; + + +CRGB GOODCOLORS[6][3] = { + {CRGB::Green, CRGB::Blue, NULL}, {CRGB::Red, CRGB::Black, NULL}, {CRGB::Red, CRGB::White, CRGB::Black }, {CRGB::White, CRGB::Black, NULL}, {CRGB::Purple, CRGB::Yellow, NULL}, {CRGB::Red, CRGB::White, CRGB::Blue} +}; + +void setup() +{ + Serial.begin(9600); + Serial.println("Starting program..."); + randomSeed(analogRead(0)); + // startup(CRGB::Red, 300, 60); + + PitLED test(NUM_STRANDS, NUM_LEDS); + SwirlFunction* swirlFunction = new SwirlFunction(2); + + test.runFunction(swirlFunction, 20); +} + +void show() { + for (int i = 1; i < NUM_STRANDS; i++) { + for (int j = 0; j < NUM_LEDS; j++) { + leds[NUM_LEDS * i + j] = leds[j]; + } + } + FastLED.show(); +} + + + + +void loop() +{ + // pickRandomFunction(); +} + +void fillAll(CRGB color) { + for (int i = 0; i < NUM_LEDS; i++) { + leds[i] = color; + } + show(); +} + +void pickRandomFunction() { + int randomPick = random(0, 7); + // int randomPick = 2; // 2 is stuck, 3 works, 4 crashes after a while (turns black), 5 and 6 work + if (randomPick == 0) { + HSVFill(30); + } + + + else if (randomPick == 1) { + HSVSwirl(30, (int)random(1, 3)); + } + + + + else if (randomPick == 2) { + CRGB colors[3] = GOODCOLORS[random(sizeof(GOODCOLORS) / sizeof(GOODCOLORS[0]))]; + if (colors[2]) { + newSwirl(colors, 3, 1, random(2, 6)); + } else { + newSwirl(colors, 2, 1, random(2, 6)); + } + } + + + + else if (randomPick == 3) { + CRGB color = CONSTCOLORS[random(sizeof(CONSTCOLORS))]; + sweep(color.red, color.green, color.blue, 30, random(0, 5), 0); + } + + + else if (randomPick == 4) { + CRGB color = CONSTCOLORS[random(sizeof(CONSTCOLORS))]; + randomFade(color.red, color.green, color.blue, 30); + } + + + else if (randomPick == 5) { + CRGB color = CONSTCOLORS[random(sizeof(CONSTCOLORS))]; + rain(color.red, color.green, color.blue, 30, 0); + } + + else if (randomPick == 6) { + CRGB color = CONSTCOLORS[random(sizeof(CONSTCOLORS))]; + smoothFade(color, 30); + } +} + + +void HSVFill(int d) { + for (int i = 0; i < 360; i++) { + CRGB color; + hsv2rgb_spectrum(CHSV(i, 255, 255 / 2), color); + for (int j = 0; j < NUM_LEDS; j++) { + leds[j] = color; + } + show(); + delay(d); + } +} + + +void HSVSwirl(int d, int repeat) { // colors to display, delay between movement, number of times each color shows up. try to make colors * repeat = 144. + for (int i = 0; i < NUM_LEDS; i++) { // one rotation + for (int k = 0 ; k < repeat; k++) { + for (int j = 0; j < NUM_LEDS / repeat; j += 1) { //this loop jumps to the start of each new color segment by that length + CRGB temp = CRGB(0, 0, 0); + hsv2rgb_spectrum(CHSV((double)map(j, 0, NUM_LEDS / repeat, 0, 255), 255, 255), temp); + leds[(i + j + k * NUM_LEDS / repeat) % NUM_LEDS] = temp; + } + } + show(); + delay(d); + } +} + + + +void swirl( CRGB colors[], int numColors, int d, int repeat) { // colors to display, delay between movement, number of times each color shows up. try to make colors * repeat = 144. + + + for (int i = 0; i < NUM_LEDS; i++) { // one rotation + int currentColor = 0; + for (int j = 0; j < NUM_LEDS; j += NUM_LEDS / (numColors * repeat)) { //this loop jumps to the start of each new color segment by that length + for (int k = 0; k < NUM_LEDS / (numColors * repeat) ; k++) { // display each led with correct color + leds[(i + j + k) % NUM_LEDS] = colors[currentColor % numColors]; + } + currentColor++; + } + show(); + delay(d); + } +} + +void newSwirl(CRGB colors[], int numColors, int d, int repeat) { + int segmentLength = (int) floor(NUM_LEDS / (numColors * repeat)); + for (int startingLEDIndex = 0; startingLEDIndex < NUM_LEDS; startingLEDIndex++) { // The index of the LED where the first color segment starts. + int colorIndex = 0; // The index of the current color we're using. + int lastTransitionLEDIndex = startingLEDIndex; // The index of the LED in between the segments (where the strip changes color). + for (int i = startingLEDIndex; i < startingLEDIndex + NUM_LEDS; i++) { + int scaledIndex = i % NUM_LEDS; // Rescale so we loop back around to the beginning if startingLEDIndex is greater than zero. + if (i - lastTransitionLEDIndex == segmentLength) { // New segment + colorIndex = (colorIndex + 1) % numColors; + lastTransitionLEDIndex = i; + } + leds[scaledIndex] = colors[colorIndex]; + } + show(); + delay(d); + } +} + +void sweep(byte red, byte green, byte blue, int d, int times, int startPoint) +{ + for (int dot = 0; dot < NUM_LEDS / times; dot++) + { + for (int i = 0; i < times; i++) + { + leds[(dot + i * NUM_LEDS / times + startPoint) % NUM_LEDS] = CRGB( red, green, blue); // set this pixel to color, mod is to be safe + } + show(); + delay(d); + } +} + +void randomFade(byte red, byte green, byte blue, int d) +{ + CRGB newColor = CRGB(red, green, blue); + for (int dotRemaining = 0; dotRemaining < NUM_LEDS; dotRemaining++) + { + int randomSelection = random(NUM_LEDS); + if (leds[randomSelection] == newColor) { + dotRemaining --; + } else { + leds[randomSelection] = CRGB( red, green, blue); // set this pixel to color + show(); + delay(d); + } + } +} + +void rain(byte red, byte green, byte blue, int d, int center) +{ + for (int dot = 0; dot < NUM_LEDS / 2; dot++) + { + leds[(center + dot) % NUM_LEDS] = CRGB( red, green, blue); // set this pixel to color + if (center - dot < 0) { + leds[(center - dot + NUM_LEDS) % NUM_LEDS] = CRGB(red, green, blue); + } else { + leds[(center - dot) % NUM_LEDS] = CRGB(red, green, blue); + } + + show(); + delay(d); + } +} + +double lerp(int from, int to, double percent) { + return from + (to - from) * percent; +} + +CRGB lerp(CRGB from, CRGB to, double percent) { + return CRGB( + (int)lerp(from.red, to.red, percent), + (int)lerp(from.green, to.green, percent), + (int)lerp(from.blue, to.blue, percent) + ); +} + +void smoothFade(CRGB toColor, int d) { + CRGB oldLeds[NUM_LEDS]; + for (int i = 0; i < NUM_LEDS; i++) { + oldLeds[i] = leds[i]; + } + + for (double i = 0; i < 1; i += 0.01) { + for (int j = 0; j < NUM_LEDS; j++) { + leds[j] = lerp(oldLeds[j], toColor, i); + } + delay(d); + show(); + } +} + + + +void startup(CRGB color, int startDelay, int minDelay) { + int d = startDelay; + int dSub = (startDelay - minDelay) / (5 * NUM_STRANDS); + fillAll(CRGB::Black); + for (int j = 0; j < NUM_STRANDS; j++) { + for (int i = 61; i < 83; i++) { + leds[i + j * 144] = color; + } + FastLED.show(); + delay(d); + d -= dSub; + for (int i = 0; i < 12; i++) { + leds[83 + i + j * 144] = color; + leds[62 - i + j * 144] = color; + } + FastLED.show(); + delay(d); + d -= dSub; + for (int i = 0; i < 24; i++) { + leds[96 + i + j * 144] = color; + leds[49 - i + j * 144] = color; + } + FastLED.show(); + delay(d); + d -= dSub; + for (int i = 0; i < 12; i++) { + leds[121 + i + j * 144] = color; + leds[22 - i + j * 144] = color; + } + FastLED.show(); + delay(d); + d -= dSub; + for (int i = 134; i < 155; i++) { + leds[i % 144 + j * 144] = color; + } + FastLED.show(); + delay(d); + d -= dSub; + } +} diff --git a/PitLED/SwirlFunction.cpp b/PitLED/SwirlFunction.cpp new file mode 100644 index 0000000..620dfaf --- /dev/null +++ b/PitLED/SwirlFunction.cpp @@ -0,0 +1,41 @@ +#include "SwirlFunction.h" +#include "Arduino.h" +#include "PitLED.h" +#include + +SwirlFunction::SwirlFunction(int _repeats) { + repeat = _repeats; +} +void SwirlFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB colors[], int d) { + Serial.println("Running swirl function"); + int numColors = 3; + Serial.println(sizeof(colors)); + Serial.println(sizeof(colors[0])); + Serial.println(sizeof(colors[1])); + Serial.println(sizeof(colors[2])); + int segmentLength = (int) floor(numLeds / (numColors * repeat)); + for (int startingLEDIndex = 0; startingLEDIndex < numLeds; startingLEDIndex++) { // The index of the LED where the first color segment starts. + int colorIndex = 0; // The index of the current color we're using. + int lastTransitionLEDIndex = startingLEDIndex; // The index of the LED in between the segments (where the strip changes color). + for (int i = startingLEDIndex; i < startingLEDIndex + numLeds; i++) { + int scaledIndex = i % numLeds; // Rescale so we loop back around to the beginning if startingLEDIndex is greater than zero. + if (i - lastTransitionLEDIndex == segmentLength) { // New segment + colorIndex = (colorIndex + 1) % numColors; + lastTransitionLEDIndex = i; + } + leds[scaledIndex] = colors[colorIndex]; + } + show(leds, numLeds, numStrands); + delay(d); + } + Serial.println("Swirl function completed"); +} +void SwirlFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { + Serial.println(numStrands); + for (int i = 1; i < numStrands; i++) { + for (int j = 0; j < numLEDsPerStrand; j++) { + leds[numLEDsPerStrand * i + j] = leds[j]; + } + } + FastLED.show(); +} diff --git a/PitLED/SwirlFunction.h b/PitLED/SwirlFunction.h new file mode 100644 index 0000000..190563b --- /dev/null +++ b/PitLED/SwirlFunction.h @@ -0,0 +1,14 @@ +#include +#include "LEDFunction.h" +#ifndef SWIRL_H +#define SWIRL_H + +class SwirlFunction: public LEDFunction { + int repeat; + public: + SwirlFunction(int repeats); + void execute(CRGB* leds, int numLeds, int numStrands, CRGB colors[], int d); + void show(CRGB* leds, int numLeds, int numStrands); +}; + +#endif From 2b720565253fddccceed80607b7a3c29d905a17b Mon Sep 17 00:00:00 2001 From: OroArmor Date: Sat, 3 Aug 2019 07:34:08 -0700 Subject: [PATCH 2/8] Finished code optimization --- PitLED/HSVFillFunction.cpp | 31 ++++ PitLED/HSVFillFunction.h | 12 ++ PitLED/HSVSwirlFunction.cpp | 34 ++++ PitLED/HSVSwirlFunction.h | 14 ++ PitLED/LEDFunction.cpp | 4 +- PitLED/LEDFunction.h | 2 +- PitLED/PitLED.cpp | 9 +- PitLED/PitLED.ino | 285 +++------------------------------- PitLED/RainFunction.cpp | 36 +++++ PitLED/RainFunction.h | 13 ++ PitLED/RandomFadeFunction.cpp | 40 +++++ PitLED/RandomFadeFunction.h | 13 ++ PitLED/SmoothFadeFunction.cpp | 46 ++++++ PitLED/SmoothFadeFunction.h | 13 ++ PitLED/StartupFunction.cpp | 58 +++++++ PitLED/StartupFunction.h | 14 ++ PitLED/SweepFunction.cpp | 32 ++++ PitLED/SweepFunction.h | 14 ++ PitLED/SwirlFunction.cpp | 62 +++++++- PitLED/SwirlFunction.h | 6 +- 20 files changed, 462 insertions(+), 276 deletions(-) create mode 100644 PitLED/HSVFillFunction.cpp create mode 100644 PitLED/HSVFillFunction.h create mode 100644 PitLED/HSVSwirlFunction.cpp create mode 100644 PitLED/HSVSwirlFunction.h create mode 100644 PitLED/RainFunction.cpp create mode 100644 PitLED/RainFunction.h create mode 100644 PitLED/RandomFadeFunction.cpp create mode 100644 PitLED/RandomFadeFunction.h create mode 100644 PitLED/SmoothFadeFunction.cpp create mode 100644 PitLED/SmoothFadeFunction.h create mode 100644 PitLED/StartupFunction.cpp create mode 100644 PitLED/StartupFunction.h create mode 100644 PitLED/SweepFunction.cpp create mode 100644 PitLED/SweepFunction.h diff --git a/PitLED/HSVFillFunction.cpp b/PitLED/HSVFillFunction.cpp new file mode 100644 index 0000000..48daaf8 --- /dev/null +++ b/PitLED/HSVFillFunction.cpp @@ -0,0 +1,31 @@ +#include "Arduino.h" +#include "PitLED.h" +#include "HSVFillFunction.h" +#include + +HSVFillFunction::HSVFillFunction(){ + +} + +void HSVFillFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { + Serial.println("Starting HSVFill function"); + for (int i = 0; i < 255; i++) { + CRGB color; + hsv2rgb_rainbow(CHSV(i, 255, 255), color); + for (int j = 0; j < numLeds; j++) { + leds[j] = color; + } + show(leds, numLeds, numStrands); + FastLED.delay(d); + } + Serial.println("HSVFill function finished"); +} + +void HSVFillFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { + for (int i = 1; i < numStrands; i++) { + for (int j = 0; j < numLEDsPerStrand; j++) { + leds[numLEDsPerStrand * i + j] = leds[j]; + } + } + FastLED.show(); +} diff --git a/PitLED/HSVFillFunction.h b/PitLED/HSVFillFunction.h new file mode 100644 index 0000000..2cd92d4 --- /dev/null +++ b/PitLED/HSVFillFunction.h @@ -0,0 +1,12 @@ +#include +#ifndef HSVFILLFUNC_H +#define HSVFILLFUNC_H + +class HSVFillFunction: public LEDFunction { + public: + HSVFillFunction(); + virtual void execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d); + void show(CRGB* leds, int numLeds, int numStrands); +}; + +#endif diff --git a/PitLED/HSVSwirlFunction.cpp b/PitLED/HSVSwirlFunction.cpp new file mode 100644 index 0000000..99e5ab1 --- /dev/null +++ b/PitLED/HSVSwirlFunction.cpp @@ -0,0 +1,34 @@ +#include "HSVSwirlFunction.h" +#include "Arduino.h" +#include "PitLED.h" +#include + + + + +HSVSwirlFunction::HSVSwirlFunction(int _repeats) { + repeat = _repeats; +} +void HSVSwirlFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { + Serial.println("Running HSVSwirl function"); + for (int i = 0; i < numLeds; i++) { // one rotation + for (int k = 0 ; k < repeat; k++) { + for (int j = 0; j < numLeds/ repeat; j += 1) { //this loop jumps to the start of each new color segment by that length + CRGB temp = CRGB(0, 0, 0); + hsv2rgb_rainbow(CHSV((double)map(j, 0, numLeds / repeat, 0, 255), 255, 255), temp); + leds[(i + j + k * numLeds / repeat) % numLeds] = temp; + } + } + show(leds, numLeds, numStrands); + FastLED.delay(d); + } + Serial.println("HSVSwirl function completed"); +} +void HSVSwirlFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { + for (int i = 1; i < numStrands; i++) { + for (int j = 0; j < numLEDsPerStrand; j++) { + leds[numLEDsPerStrand * i + j] = leds[j]; + } + } + FastLED.show(); +} diff --git a/PitLED/HSVSwirlFunction.h b/PitLED/HSVSwirlFunction.h new file mode 100644 index 0000000..d25f681 --- /dev/null +++ b/PitLED/HSVSwirlFunction.h @@ -0,0 +1,14 @@ +#include +#include "LEDFunction.h" +#ifndef HSVSWIRL_H +#define HSVSWIRL_H + +class HSVSwirlFunction: public LEDFunction { + int repeat; + public: + HSVSwirlFunction(int _repeats); + void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); + void show(CRGB * leds, int numLeds, int numStrands); +}; + +#endif diff --git a/PitLED/LEDFunction.cpp b/PitLED/LEDFunction.cpp index 01f40fa..81cf234 100644 --- a/PitLED/LEDFunction.cpp +++ b/PitLED/LEDFunction.cpp @@ -11,10 +11,10 @@ void LEDFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { FastLED.show(); } -void LEDFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB colors[], int d) { +void LEDFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { Serial.println("Running base red function"); for (int i = 0; i < numLeds; i++) { - leds[i] = CRGB::Red; + leds[i] = color; } show(leds, numLeds, numStrands); delay(1000); diff --git a/PitLED/LEDFunction.h b/PitLED/LEDFunction.h index 4fabd1f..999beea 100644 --- a/PitLED/LEDFunction.h +++ b/PitLED/LEDFunction.h @@ -4,7 +4,7 @@ class LEDFunction { public: - virtual void execute(CRGB* leds, int numLeds,int numStrands, CRGB colors[], int d); + virtual void execute(CRGB* leds, int numLeds,int numStrands, CRGB color, int d); void show(CRGB* leds, int numLeds,int numStrands); }; diff --git a/PitLED/PitLED.cpp b/PitLED/PitLED.cpp index a8a5f4e..6afbe4e 100644 --- a/PitLED/PitLED.cpp +++ b/PitLED/PitLED.cpp @@ -4,6 +4,11 @@ #include "LEDFunction.h" +#define NUMCOLORS 8 +CRGB CONSTCOLORS[NUMCOLORS] = { + CRGB::Red, CRGB::Green, CRGB::Blue, CRGB::Yellow, CRGB::Orange, CRGB::Cyan, CRGB::Purple, CRGB::White +}; + PitLED::PitLED(int strands, int ledsPerStrand) { numStrands = strands; numLEDsPerStrand = ledsPerStrand; @@ -35,6 +40,6 @@ PitLED::~PitLED() { } void PitLED::runFunction(LEDFunction *function, int d) { - CRGB colors[] = {CRGB::Red, CRGB::Green, CRGB::Orange}; - function->execute(leds, numLEDsPerStrand, numStrands, colors, d); + CRGB color = CONSTCOLORS[random(NUMCOLORS)]; + function->execute(leds, numLEDsPerStrand, numStrands, color, d); } diff --git a/PitLED/PitLED.ino b/PitLED/PitLED.ino index c826c10..4f6822d 100644 --- a/PitLED/PitLED.ino +++ b/PitLED/PitLED.ino @@ -1,284 +1,49 @@ +#include +#include + //#include #include #include "PitLED.h" #include "SwirlFunction.h" +#include "HSVFillFunction.h" +#include "HSVSwirlFunction.h" +#include "SweepFunction.h" +#include "RandomFadeFunction.h" +#include "RainFunction.h" +#include "SmoothFadeFunction.h" +#include "StartupFunction.h" #include "LEDFunction.h" #define NUM_LEDS 144 #define DATA_PIN 5 #define NUM_STRANDS 15 CRGB leds[NUM_LEDS * NUM_STRANDS]; - -CRGB CONSTCOLORS[8] = { - CRGB::Red, CRGB::Green, CRGB::Blue, CRGB::Yellow, CRGB::Orange, CRGB::Cyan, CRGB::Purple, CRGB::White -}; - - -CRGB GOODCOLORS[6][3] = { - {CRGB::Green, CRGB::Blue, NULL}, {CRGB::Red, CRGB::Black, NULL}, {CRGB::Red, CRGB::White, CRGB::Black }, {CRGB::White, CRGB::Black, NULL}, {CRGB::Purple, CRGB::Yellow, NULL}, {CRGB::Red, CRGB::White, CRGB::Blue} -}; - +PitLED pitLed(NUM_STRANDS, NUM_LEDS); void setup() { Serial.begin(9600); Serial.println("Starting program..."); + StartupFunction * startupFunction = new StartupFunction(60); + pitLed.runFunction( startupFunction, 300); randomSeed(analogRead(0)); - // startup(CRGB::Red, 300, 60); - - PitLED test(NUM_STRANDS, NUM_LEDS); - SwirlFunction* swirlFunction = new SwirlFunction(2); - - test.runFunction(swirlFunction, 20); -} - -void show() { - for (int i = 1; i < NUM_STRANDS; i++) { - for (int j = 0; j < NUM_LEDS; j++) { - leds[NUM_LEDS * i + j] = leds[j]; - } - } - FastLED.show(); } - - - void loop() { - // pickRandomFunction(); -} - -void fillAll(CRGB color) { - for (int i = 0; i < NUM_LEDS; i++) { - leds[i] = color; - } - show(); -} - -void pickRandomFunction() { - int randomPick = random(0, 7); - // int randomPick = 2; // 2 is stuck, 3 works, 4 crashes after a while (turns black), 5 and 6 work - if (randomPick == 0) { - HSVFill(30); - } - - - else if (randomPick == 1) { - HSVSwirl(30, (int)random(1, 3)); - } - - - - else if (randomPick == 2) { - CRGB colors[3] = GOODCOLORS[random(sizeof(GOODCOLORS) / sizeof(GOODCOLORS[0]))]; - if (colors[2]) { - newSwirl(colors, 3, 1, random(2, 6)); - } else { - newSwirl(colors, 2, 1, random(2, 6)); - } - } - - - - else if (randomPick == 3) { - CRGB color = CONSTCOLORS[random(sizeof(CONSTCOLORS))]; - sweep(color.red, color.green, color.blue, 30, random(0, 5), 0); - } - - - else if (randomPick == 4) { - CRGB color = CONSTCOLORS[random(sizeof(CONSTCOLORS))]; - randomFade(color.red, color.green, color.blue, 30); - } - - - else if (randomPick == 5) { - CRGB color = CONSTCOLORS[random(sizeof(CONSTCOLORS))]; - rain(color.red, color.green, color.blue, 30, 0); - } - - else if (randomPick == 6) { - CRGB color = CONSTCOLORS[random(sizeof(CONSTCOLORS))]; - smoothFade(color, 30); - } -} - - -void HSVFill(int d) { - for (int i = 0; i < 360; i++) { - CRGB color; - hsv2rgb_spectrum(CHSV(i, 255, 255 / 2), color); - for (int j = 0; j < NUM_LEDS; j++) { - leds[j] = color; - } - show(); - delay(d); - } -} - - -void HSVSwirl(int d, int repeat) { // colors to display, delay between movement, number of times each color shows up. try to make colors * repeat = 144. - for (int i = 0; i < NUM_LEDS; i++) { // one rotation - for (int k = 0 ; k < repeat; k++) { - for (int j = 0; j < NUM_LEDS / repeat; j += 1) { //this loop jumps to the start of each new color segment by that length - CRGB temp = CRGB(0, 0, 0); - hsv2rgb_spectrum(CHSV((double)map(j, 0, NUM_LEDS / repeat, 0, 255), 255, 255), temp); - leds[(i + j + k * NUM_LEDS / repeat) % NUM_LEDS] = temp; - } - } - show(); - delay(d); - } -} - - - -void swirl( CRGB colors[], int numColors, int d, int repeat) { // colors to display, delay between movement, number of times each color shows up. try to make colors * repeat = 144. - - - for (int i = 0; i < NUM_LEDS; i++) { // one rotation - int currentColor = 0; - for (int j = 0; j < NUM_LEDS; j += NUM_LEDS / (numColors * repeat)) { //this loop jumps to the start of each new color segment by that length - for (int k = 0; k < NUM_LEDS / (numColors * repeat) ; k++) { // display each led with correct color - leds[(i + j + k) % NUM_LEDS] = colors[currentColor % numColors]; - } - currentColor++; - } - show(); - delay(d); - } -} - -void newSwirl(CRGB colors[], int numColors, int d, int repeat) { - int segmentLength = (int) floor(NUM_LEDS / (numColors * repeat)); - for (int startingLEDIndex = 0; startingLEDIndex < NUM_LEDS; startingLEDIndex++) { // The index of the LED where the first color segment starts. - int colorIndex = 0; // The index of the current color we're using. - int lastTransitionLEDIndex = startingLEDIndex; // The index of the LED in between the segments (where the strip changes color). - for (int i = startingLEDIndex; i < startingLEDIndex + NUM_LEDS; i++) { - int scaledIndex = i % NUM_LEDS; // Rescale so we loop back around to the beginning if startingLEDIndex is greater than zero. - if (i - lastTransitionLEDIndex == segmentLength) { // New segment - colorIndex = (colorIndex + 1) % numColors; - lastTransitionLEDIndex = i; - } - leds[scaledIndex] = colors[colorIndex]; - } - show(); - delay(d); - } -} - -void sweep(byte red, byte green, byte blue, int d, int times, int startPoint) -{ - for (int dot = 0; dot < NUM_LEDS / times; dot++) - { - for (int i = 0; i < times; i++) - { - leds[(dot + i * NUM_LEDS / times + startPoint) % NUM_LEDS] = CRGB( red, green, blue); // set this pixel to color, mod is to be safe - } - show(); - delay(d); - } -} - -void randomFade(byte red, byte green, byte blue, int d) -{ - CRGB newColor = CRGB(red, green, blue); - for (int dotRemaining = 0; dotRemaining < NUM_LEDS; dotRemaining++) - { - int randomSelection = random(NUM_LEDS); - if (leds[randomSelection] == newColor) { - dotRemaining --; - } else { - leds[randomSelection] = CRGB( red, green, blue); // set this pixel to color - show(); - delay(d); - } - } -} - -void rain(byte red, byte green, byte blue, int d, int center) -{ - for (int dot = 0; dot < NUM_LEDS / 2; dot++) - { - leds[(center + dot) % NUM_LEDS] = CRGB( red, green, blue); // set this pixel to color - if (center - dot < 0) { - leds[(center - dot + NUM_LEDS) % NUM_LEDS] = CRGB(red, green, blue); - } else { - leds[(center - dot) % NUM_LEDS] = CRGB(red, green, blue); - } - - show(); - delay(d); - } -} - -double lerp(int from, int to, double percent) { - return from + (to - from) * percent; -} - -CRGB lerp(CRGB from, CRGB to, double percent) { - return CRGB( - (int)lerp(from.red, to.red, percent), - (int)lerp(from.green, to.green, percent), - (int)lerp(from.blue, to.blue, percent) - ); -} - -void smoothFade(CRGB toColor, int d) { - CRGB oldLeds[NUM_LEDS]; - for (int i = 0; i < NUM_LEDS; i++) { - oldLeds[i] = leds[i]; - } - - for (double i = 0; i < 1; i += 0.01) { - for (int j = 0; j < NUM_LEDS; j++) { - leds[j] = lerp(oldLeds[j], toColor, i); - } - delay(d); - show(); - } -} + LEDFunction* functions[7]; + functions[0] = new SwirlFunction(random(6) + 1); + functions[1] = new HSVFillFunction(); + functions[2] = new HSVSwirlFunction(random(6) + 1); + functions[3] = new SweepFunction(random(6) + 1); + functions[4] = new RandomFadeFunction(); + functions[5] = new RainFunction(); + functions[6] = new SmoothFadeFunction(); -void startup(CRGB color, int startDelay, int minDelay) { - int d = startDelay; - int dSub = (startDelay - minDelay) / (5 * NUM_STRANDS); - fillAll(CRGB::Black); - for (int j = 0; j < NUM_STRANDS; j++) { - for (int i = 61; i < 83; i++) { - leds[i + j * 144] = color; - } - FastLED.show(); - delay(d); - d -= dSub; - for (int i = 0; i < 12; i++) { - leds[83 + i + j * 144] = color; - leds[62 - i + j * 144] = color; - } - FastLED.show(); - delay(d); - d -= dSub; - for (int i = 0; i < 24; i++) { - leds[96 + i + j * 144] = color; - leds[49 - i + j * 144] = color; - } - FastLED.show(); - delay(d); - d -= dSub; - for (int i = 0; i < 12; i++) { - leds[121 + i + j * 144] = color; - leds[22 - i + j * 144] = color; - } - FastLED.show(); - delay(d); - d -= dSub; - for (int i = 134; i < 155; i++) { - leds[i % 144 + j * 144] = color; - } - FastLED.show(); - delay(d); - d -= dSub; - } + pitLed.runFunction(functions[random(7)], 20); + Serial.print("Free Memory: "); + Serial.println(freeMemory(), DEC); + Serial.println(); } diff --git a/PitLED/RainFunction.cpp b/PitLED/RainFunction.cpp new file mode 100644 index 0000000..95b3fdc --- /dev/null +++ b/PitLED/RainFunction.cpp @@ -0,0 +1,36 @@ +#include "RainFunction.h" +#include "Arduino.h" +#include "PitLED.h" +#include + + + + +RainFunction::RainFunction() { +} +void RainFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { + Serial.println("Running Rain function"); + int center = 72; + for (int dot = 0; dot < numLeds / 2; dot++) + { + leds[(center + dot) % numLeds] = color; // set this pixel to color + if (center - dot < 0) { + leds[(center - dot + numLeds) % numLeds] = color; + } else { + leds[(center - dot) % numLeds] = color; + } + + show(leds, numLeds, numStrands); + FastLED.delay(d); + } + + Serial.println("Rain function completed"); +} +void RainFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { + for (int i = 1; i < numStrands; i++) { + for (int j = 0; j < numLEDsPerStrand; j++) { + leds[numLEDsPerStrand * i + j] = leds[j]; + } + } + FastLED.show(); +} diff --git a/PitLED/RainFunction.h b/PitLED/RainFunction.h new file mode 100644 index 0000000..c17b9ea --- /dev/null +++ b/PitLED/RainFunction.h @@ -0,0 +1,13 @@ +#include +#include "LEDFunction.h" +#ifndef RAIN_H +#define RAIN_H + +class RainFunction: public LEDFunction { + public: + RainFunction(); + void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); + void show(CRGB * leds, int numLeds, int numStrands); +}; + +#endif diff --git a/PitLED/RandomFadeFunction.cpp b/PitLED/RandomFadeFunction.cpp new file mode 100644 index 0000000..8760442 --- /dev/null +++ b/PitLED/RandomFadeFunction.cpp @@ -0,0 +1,40 @@ +#include "RandomFadeFunction.h" +#include "Arduino.h" +#include "PitLED.h" +#include + + + + +RandomFadeFunction::RandomFadeFunction() { +} +void RandomFadeFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { + Serial.println("Running Random Fade function"); + + int numAlready = 0; + for (int i = 0; i < numLeds; i++) { + if (leds[i] == color) { + numAlready++; + } + } + for (int dotRemaining = 0; dotRemaining < (numLeds - numAlready); dotRemaining++) + { + int randomSelection = random(numLeds); + if (leds[randomSelection] == color) { + dotRemaining --; + } else { + leds[randomSelection] = color; // set this pixel to color + show(leds, numLeds, numStrands); + FastLED.delay(d); + } + } + Serial.println("Random Fade function completed"); +} +void RandomFadeFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { + for (int i = 1; i < numStrands; i++) { + for (int j = 0; j < numLEDsPerStrand; j++) { + leds[numLEDsPerStrand * i + j] = leds[j]; + } + } + FastLED.show(); +} diff --git a/PitLED/RandomFadeFunction.h b/PitLED/RandomFadeFunction.h new file mode 100644 index 0000000..bf8d76d --- /dev/null +++ b/PitLED/RandomFadeFunction.h @@ -0,0 +1,13 @@ +#include +#include "LEDFunction.h" +#ifndef RANDOMFADE_H +#define RANDOMFADE_H + +class RandomFadeFunction: public LEDFunction { + public: + RandomFadeFunction(); + void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); + void show(CRGB * leds, int numLeds, int numStrands); +}; + +#endif diff --git a/PitLED/SmoothFadeFunction.cpp b/PitLED/SmoothFadeFunction.cpp new file mode 100644 index 0000000..ff5b8b3 --- /dev/null +++ b/PitLED/SmoothFadeFunction.cpp @@ -0,0 +1,46 @@ +#include "SmoothFadeFunction.h" +#include "Arduino.h" +#include "PitLED.h" +#include + +double lerp(int from, int to, double percent) { + return from + (to - from) * percent; +} + +CRGB lerp(CRGB from, CRGB to, double percent) { + return CRGB( + (int)lerp(from.red, to.red, percent), + (int)lerp(from.green, to.green, percent), + (int)lerp(from.blue, to.blue, percent) + ); +} + + +SmoothFadeFunction::SmoothFadeFunction() { +} +void SmoothFadeFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { + Serial.println("Running Smooth Fade Function"); + + CRGB oldLeds[numLeds]; + for (int i = 0; i +#include "LEDFunction.h" +#ifndef SMOOTH_H +#define SMOOTH_H + +class SmoothFadeFunction: public LEDFunction { + public: + SmoothFadeFunction(); + void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); + void show(CRGB * leds, int numLeds, int numStrands); +}; + +#endif diff --git a/PitLED/StartupFunction.cpp b/PitLED/StartupFunction.cpp new file mode 100644 index 0000000..50d81e6 --- /dev/null +++ b/PitLED/StartupFunction.cpp @@ -0,0 +1,58 @@ +#include "StartupFunction.h" +#include "Arduino.h" +#include "PitLED.h" +#include + +StartupFunction::StartupFunction(int _dMin) { + dMin = _dMin; +} +void StartupFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int startDelay) { + Serial.println("Running Startup function"); + color = CRGB::Red; + int d = startDelay; + int dSub = (startDelay - dMin) / (5 * numStrands); + for (int j = 0; j < numStrands; j++) { + for (int i = 61; i < 83; i++) { + leds[i + j * 144] = color; + } + FastLED.show(); + delay(d); + d -= dSub; + for (int i = 0; i < 12; i++) { + leds[83 + i + j * 144] = color; + leds[62 - i + j * 144] = color; + } + FastLED.show(); + delay(d); + d -= dSub; + for (int i = 0; i < 24; i++) { + leds[96 + i + j * 144] = color; + leds[49 - i + j * 144] = color; + } + FastLED.show(); + delay(d); + d -= dSub; + for (int i = 0; i < 12; i++) { + leds[121 + i + j * 144] = color; + leds[22 - i + j * 144] = color; + } + FastLED.show(); + delay(d); + d -= dSub; + for (int i = 134; i < 155; i++) { + leds[i % 144 + j * 144] = color; + } + FastLED.show(); + delay(d); + d -= dSub; + } + Serial.println("Startup function completed"); +} +void StartupFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { + for (int i = 1; i < numStrands; i++) { + for (int j = 0; j < numLEDsPerStrand; j++) { + leds[numLEDsPerStrand * i + j] = leds[j]; + } + } + FastLED.show(); +} diff --git a/PitLED/StartupFunction.h b/PitLED/StartupFunction.h new file mode 100644 index 0000000..ce0ac0d --- /dev/null +++ b/PitLED/StartupFunction.h @@ -0,0 +1,14 @@ +#include +#include "LEDFunction.h" +#ifndef STARTUP_H +#define STARTUP_H + +class StartupFunction: public LEDFunction { + int dMin; + public: + StartupFunction(int _dMin); + void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); + void show(CRGB * leds, int numLeds, int numStrands); +}; + +#endif diff --git a/PitLED/SweepFunction.cpp b/PitLED/SweepFunction.cpp new file mode 100644 index 0000000..7e26fb9 --- /dev/null +++ b/PitLED/SweepFunction.cpp @@ -0,0 +1,32 @@ +#include "SweepFunction.h" +#include "Arduino.h" +#include "PitLED.h" +#include + + + + +SweepFunction::SweepFunction(int _repeats) { + repeat = _repeats; +} +void SweepFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { + Serial.println("Running Sweep function"); + for (int dot = 0; dot < numLeds / repeat; dot++) + { + for (int i = 0; i < repeat; i++) + { + leds[(dot + i * numLeds / repeat) % numLeds] = color; // set this pixel to color, mod is to be safe + } + show(leds, numLeds, numStrands); + FastLED.delay(d); + } + Serial.println("Sweep function completed"); +} +void SweepFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { + for (int i = 1; i < numStrands; i++) { + for (int j = 0; j < numLEDsPerStrand; j++) { + leds[numLEDsPerStrand * i + j] = leds[j]; + } + } + FastLED.show(); +} diff --git a/PitLED/SweepFunction.h b/PitLED/SweepFunction.h new file mode 100644 index 0000000..432e6fc --- /dev/null +++ b/PitLED/SweepFunction.h @@ -0,0 +1,14 @@ +#include +#include "LEDFunction.h" +#ifndef SWEEP_H +#define SWEEP_H + +class SweepFunction: public LEDFunction { + int repeat; + public: + SweepFunction(int _repeats); + void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); + void show(CRGB * leds, int numLeds, int numStrands); +}; + +#endif diff --git a/PitLED/SwirlFunction.cpp b/PitLED/SwirlFunction.cpp index 620dfaf..b9d1e6e 100644 --- a/PitLED/SwirlFunction.cpp +++ b/PitLED/SwirlFunction.cpp @@ -3,16 +3,63 @@ #include "PitLED.h" #include +const TProgmemPalette16 FirstColors PROGMEM = +{ + CRGB::Red, + CRGB::Blue, + CRGB::White +}; + +const TProgmemPalette16 Seahawks PROGMEM = +{ + CRGB::Blue, + CRGB::Green +}; + +const TProgmemPalette16 Huskies PROGMEM = +{ + CRGB::Yellow, + CRGB::Purple +}; + +const TProgmemPalette16 Robototes PROGMEM = +{ + CRGB::Red, + CRGB::Black, +}; + +const TProgmemPalette16 Sammamish PROGMEM = +{ + CRGB::Red, + CRGB::Black, + CRGB::White +}; +#define TWOCOLORS 3 +CRGBPalette16 twoColors[TWOCOLORS] = { + Robototes, + Huskies, + Seahawks +}; +#define THREECOLORS 2 +CRGBPalette16 threeColors[THREECOLORS] = { + FirstColors, + Sammamish +}; + + + SwirlFunction::SwirlFunction(int _repeats) { repeat = _repeats; } -void SwirlFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB colors[], int d) { +void SwirlFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { Serial.println("Running swirl function"); - int numColors = 3; - Serial.println(sizeof(colors)); - Serial.println(sizeof(colors[0])); - Serial.println(sizeof(colors[1])); - Serial.println(sizeof(colors[2])); + int numColors = (int)random(2) + 2; + CRGBPalette16 palette; + if (numColors == 2) { + palette = twoColors[random(TWOCOLORS)]; + }else if(numColors == 3){ + palette = threeColors[random(THREECOLORS)]; + } int segmentLength = (int) floor(numLeds / (numColors * repeat)); for (int startingLEDIndex = 0; startingLEDIndex < numLeds; startingLEDIndex++) { // The index of the LED where the first color segment starts. int colorIndex = 0; // The index of the current color we're using. @@ -23,7 +70,7 @@ void SwirlFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color colorIndex = (colorIndex + 1) % numColors; lastTransitionLEDIndex = i; } - leds[scaledIndex] = colors[colorIndex]; + leds[scaledIndex] = ColorFromPalette( palette, colorIndex * 16, 255, NOBLEND); } show(leds, numLeds, numStrands); delay(d); @@ -31,7 +78,6 @@ void SwirlFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color Serial.println("Swirl function completed"); } void SwirlFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { - Serial.println(numStrands); for (int i = 1; i < numStrands; i++) { for (int j = 0; j < numLEDsPerStrand; j++) { leds[numLEDsPerStrand * i + j] = leds[j]; diff --git a/PitLED/SwirlFunction.h b/PitLED/SwirlFunction.h index 190563b..e1277bf 100644 --- a/PitLED/SwirlFunction.h +++ b/PitLED/SwirlFunction.h @@ -6,9 +6,9 @@ class SwirlFunction: public LEDFunction { int repeat; public: - SwirlFunction(int repeats); - void execute(CRGB* leds, int numLeds, int numStrands, CRGB colors[], int d); - void show(CRGB* leds, int numLeds, int numStrands); + SwirlFunction(int _repeats); + void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); + void show(CRGB * leds, int numLeds, int numStrands); }; #endif From 09c26bf1bf22e840bb22b177f602ca596adc0c6b Mon Sep 17 00:00:00 2001 From: Robototes programmers Date: Fri, 28 Feb 2020 14:31:10 -0800 Subject: [PATCH 3/8] zoe's preferences --- PitLED/PitLED.cpp | 4 ++-- PitLED/PitLED.ino | 16 ++++++++-------- PitLED/SwirlFunction.cpp | 8 ++++---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/PitLED/PitLED.cpp b/PitLED/PitLED.cpp index 6afbe4e..17822ae 100644 --- a/PitLED/PitLED.cpp +++ b/PitLED/PitLED.cpp @@ -4,9 +4,9 @@ #include "LEDFunction.h" -#define NUMCOLORS 8 +#define NUMCOLORS 4 CRGB CONSTCOLORS[NUMCOLORS] = { - CRGB::Red, CRGB::Green, CRGB::Blue, CRGB::Yellow, CRGB::Orange, CRGB::Cyan, CRGB::Purple, CRGB::White + CRGB::Red, CRGB::Blue, CRGB::White, CRGB::Black }; PitLED::PitLED(int strands, int ledsPerStrand) { diff --git a/PitLED/PitLED.ino b/PitLED/PitLED.ino index 4f6822d..968685e 100644 --- a/PitLED/PitLED.ino +++ b/PitLED/PitLED.ino @@ -32,17 +32,17 @@ void setup() void loop() { - LEDFunction* functions[7]; + LEDFunction* functions[5]; functions[0] = new SwirlFunction(random(6) + 1); - functions[1] = new HSVFillFunction(); - functions[2] = new HSVSwirlFunction(random(6) + 1); - functions[3] = new SweepFunction(random(6) + 1); - functions[4] = new RandomFadeFunction(); - functions[5] = new RainFunction(); - functions[6] = new SmoothFadeFunction(); +// functions[1] = new HSVFillFunction(); +// functions[2] = new HSVSwirlFunction(random(6) + 1); + functions[1] = new SweepFunction(random(6) + 1); + functions[2] = new RandomFadeFunction(); + functions[3] = new RainFunction(); + functions[4] = new SmoothFadeFunction(); - pitLed.runFunction(functions[random(7)], 20); + pitLed.runFunction(functions[random(5)], 20); Serial.print("Free Memory: "); Serial.println(freeMemory(), DEC); Serial.println(); diff --git a/PitLED/SwirlFunction.cpp b/PitLED/SwirlFunction.cpp index b9d1e6e..74572bc 100644 --- a/PitLED/SwirlFunction.cpp +++ b/PitLED/SwirlFunction.cpp @@ -34,11 +34,11 @@ const TProgmemPalette16 Sammamish PROGMEM = CRGB::Black, CRGB::White }; -#define TWOCOLORS 3 +#define TWOCOLORS 1 CRGBPalette16 twoColors[TWOCOLORS] = { - Robototes, - Huskies, - Seahawks + Robototes +// Huskies, +// Seahawks }; #define THREECOLORS 2 CRGBPalette16 threeColors[THREECOLORS] = { From 205995f27c368e6302c40501cb16c1d3788ec93f Mon Sep 17 00:00:00 2001 From: OroArmor Date: Mon, 15 Jun 2020 10:05:16 -0700 Subject: [PATCH 4/8] First upload of rewrite --- PitLED/Functions.h | 206 ++++++++++++++++++++++++++++++++++ PitLED/HSVFillFunction.cpp | 31 ----- PitLED/HSVFillFunction.h | 12 -- PitLED/HSVSwirlFunction.cpp | 34 ------ PitLED/HSVSwirlFunction.h | 14 --- PitLED/LEDFunction.cpp | 21 ---- PitLED/LEDFunction.h | 11 -- PitLED/PitLED.cpp | 11 +- PitLED/PitLED.h | 3 +- PitLED/PitLED.ino | 40 ++----- PitLED/RainFunction.cpp | 36 ------ PitLED/RainFunction.h | 13 --- PitLED/RandomFadeFunction.cpp | 40 ------- PitLED/RandomFadeFunction.h | 13 --- PitLED/RobototesColors.h | 54 +++++++++ PitLED/SmoothFadeFunction.cpp | 46 -------- PitLED/SmoothFadeFunction.h | 13 --- PitLED/StartupFunction.cpp | 58 ---------- PitLED/StartupFunction.h | 14 --- PitLED/SweepFunction.cpp | 32 ------ PitLED/SweepFunction.h | 14 --- PitLED/SwirlFunction.cpp | 87 -------------- PitLED/SwirlFunction.h | 14 --- 23 files changed, 275 insertions(+), 542 deletions(-) create mode 100644 PitLED/Functions.h delete mode 100644 PitLED/HSVFillFunction.cpp delete mode 100644 PitLED/HSVFillFunction.h delete mode 100644 PitLED/HSVSwirlFunction.cpp delete mode 100644 PitLED/HSVSwirlFunction.h delete mode 100644 PitLED/LEDFunction.cpp delete mode 100644 PitLED/LEDFunction.h delete mode 100644 PitLED/RainFunction.cpp delete mode 100644 PitLED/RainFunction.h delete mode 100644 PitLED/RandomFadeFunction.cpp delete mode 100644 PitLED/RandomFadeFunction.h create mode 100644 PitLED/RobototesColors.h delete mode 100644 PitLED/SmoothFadeFunction.cpp delete mode 100644 PitLED/SmoothFadeFunction.h delete mode 100644 PitLED/StartupFunction.cpp delete mode 100644 PitLED/StartupFunction.h delete mode 100644 PitLED/SweepFunction.cpp delete mode 100644 PitLED/SweepFunction.h delete mode 100644 PitLED/SwirlFunction.cpp delete mode 100644 PitLED/SwirlFunction.h diff --git a/PitLED/Functions.h b/PitLED/Functions.h new file mode 100644 index 0000000..c575dff --- /dev/null +++ b/PitLED/Functions.h @@ -0,0 +1,206 @@ +#include +#include "Arduino.h" +#include "PitLED.h" +#include "RobototesColors.h" + +#ifndef FUNCTIONS_H +#define FUNCTIONS_H + +void show(CRGB* leds, int numLEDsPerStrand, int numStrands) { + for (int i = 1; i < numStrands; i++) { + for (int j = 0; j < numLEDsPerStrand; j++) { + leds[numLEDsPerStrand * i + j] = leds[j]; + } + } + FastLED.show(); +} + +void HSVFillFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { + Serial.println("Starting HSVFill function"); + for (int i = 0; i < 255; i++) { + CRGB color; + hsv2rgb_rainbow(CHSV(i, 255, 255), color); + for (int j = 0; j < numLeds; j++) { + leds[j] = color; + } + show(leds, numLeds, numStrands); + FastLED.delay(d); + } + Serial.println("HSVFill function finished"); +} + +void HSVSwirlFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { + Serial.println("Running HSVSwirl function"); + for (int i = 0; i < numLeds; i++) { // one rotation + for (int k = 0 ; k < repeats; k++) { + for (int j = 0; j < numLeds / repeats; j += 1) { //this loop jumps to the start of each new color segment by that length + CRGB temp = CRGB(0, 0, 0); + hsv2rgb_rainbow(CHSV((double)map(j, 0, numLeds / repeats, 0, 255), 255, 255), temp); + leds[(i + j + k * numLeds / repeats) % numLeds] = temp; + } + } + show(leds, numLeds, numStrands); + FastLED.delay(d); + } + Serial.println("HSVSwirl function completed"); +} + +void RainFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { + Serial.println("Running Rain function"); + int center = 72; + for (int dot = 0; dot < numLeds / 2; dot++) + { + leds[(center + dot) % numLeds] = color; // set this pixel to color + if (center - dot < 0) { + leds[(center - dot + numLeds) % numLeds] = color; + } else { + leds[(center - dot) % numLeds] = color; + } + + show(leds, numLeds, numStrands); + FastLED.delay(d); + } + + Serial.println("Rain function completed"); +} + +void RandomFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { + Serial.println("Running Random Fade function"); + + int numAlready = 0; + for (int i = 0; i < numLeds; i++) { + if (leds[i] == color) { + numAlready++; + } + } + for (int dotRemaining = 0; dotRemaining < (numLeds - numAlready); dotRemaining++) + { + int randomSelection = random(numLeds); + if (leds[randomSelection] == color) { + dotRemaining --; + } else { + leds[randomSelection] = color; // set this pixel to color + show(leds, numLeds, numStrands); + FastLED.delay(d); + } + } + Serial.println("Random Fade function completed"); +} + +double lerp(int from, int to, double percent) { + return from + (to - from) * percent; +} + +CRGB lerp(CRGB from, CRGB to, double percent) { + return CRGB( + (int)lerp(from.red, to.red, percent), + (int)lerp(from.green, to.green, percent), + (int)lerp(from.blue, to.blue, percent) + ); +} + +void SmoothFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { + Serial.println("Running Smooth Fade Function"); + + CRGB oldLeds[numLeds]; + for (int i = 0; i - -HSVFillFunction::HSVFillFunction(){ - -} - -void HSVFillFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { - Serial.println("Starting HSVFill function"); - for (int i = 0; i < 255; i++) { - CRGB color; - hsv2rgb_rainbow(CHSV(i, 255, 255), color); - for (int j = 0; j < numLeds; j++) { - leds[j] = color; - } - show(leds, numLeds, numStrands); - FastLED.delay(d); - } - Serial.println("HSVFill function finished"); -} - -void HSVFillFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { - for (int i = 1; i < numStrands; i++) { - for (int j = 0; j < numLEDsPerStrand; j++) { - leds[numLEDsPerStrand * i + j] = leds[j]; - } - } - FastLED.show(); -} diff --git a/PitLED/HSVFillFunction.h b/PitLED/HSVFillFunction.h deleted file mode 100644 index 2cd92d4..0000000 --- a/PitLED/HSVFillFunction.h +++ /dev/null @@ -1,12 +0,0 @@ -#include -#ifndef HSVFILLFUNC_H -#define HSVFILLFUNC_H - -class HSVFillFunction: public LEDFunction { - public: - HSVFillFunction(); - virtual void execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d); - void show(CRGB* leds, int numLeds, int numStrands); -}; - -#endif diff --git a/PitLED/HSVSwirlFunction.cpp b/PitLED/HSVSwirlFunction.cpp deleted file mode 100644 index 99e5ab1..0000000 --- a/PitLED/HSVSwirlFunction.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "HSVSwirlFunction.h" -#include "Arduino.h" -#include "PitLED.h" -#include - - - - -HSVSwirlFunction::HSVSwirlFunction(int _repeats) { - repeat = _repeats; -} -void HSVSwirlFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { - Serial.println("Running HSVSwirl function"); - for (int i = 0; i < numLeds; i++) { // one rotation - for (int k = 0 ; k < repeat; k++) { - for (int j = 0; j < numLeds/ repeat; j += 1) { //this loop jumps to the start of each new color segment by that length - CRGB temp = CRGB(0, 0, 0); - hsv2rgb_rainbow(CHSV((double)map(j, 0, numLeds / repeat, 0, 255), 255, 255), temp); - leds[(i + j + k * numLeds / repeat) % numLeds] = temp; - } - } - show(leds, numLeds, numStrands); - FastLED.delay(d); - } - Serial.println("HSVSwirl function completed"); -} -void HSVSwirlFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { - for (int i = 1; i < numStrands; i++) { - for (int j = 0; j < numLEDsPerStrand; j++) { - leds[numLEDsPerStrand * i + j] = leds[j]; - } - } - FastLED.show(); -} diff --git a/PitLED/HSVSwirlFunction.h b/PitLED/HSVSwirlFunction.h deleted file mode 100644 index d25f681..0000000 --- a/PitLED/HSVSwirlFunction.h +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include "LEDFunction.h" -#ifndef HSVSWIRL_H -#define HSVSWIRL_H - -class HSVSwirlFunction: public LEDFunction { - int repeat; - public: - HSVSwirlFunction(int _repeats); - void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); - void show(CRGB * leds, int numLeds, int numStrands); -}; - -#endif diff --git a/PitLED/LEDFunction.cpp b/PitLED/LEDFunction.cpp deleted file mode 100644 index 81cf234..0000000 --- a/PitLED/LEDFunction.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "Arduino.h" -#include "PitLED.h" -#include - -void LEDFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { - for (int i = 1; i < numStrands; i++) { - for (int j = 0; j < numLEDsPerStrand; j++) { - leds[numLEDsPerStrand * i + j] = leds[j]; - } - } - FastLED.show(); -} - -void LEDFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { - Serial.println("Running base red function"); - for (int i = 0; i < numLeds; i++) { - leds[i] = color; - } - show(leds, numLeds, numStrands); - delay(1000); -} diff --git a/PitLED/LEDFunction.h b/PitLED/LEDFunction.h deleted file mode 100644 index 999beea..0000000 --- a/PitLED/LEDFunction.h +++ /dev/null @@ -1,11 +0,0 @@ -#include -#ifndef LEDFUNC_H -#define LEDFUNC_H - -class LEDFunction { - public: - virtual void execute(CRGB* leds, int numLeds,int numStrands, CRGB color, int d); - void show(CRGB* leds, int numLeds,int numStrands); -}; - -#endif diff --git a/PitLED/PitLED.cpp b/PitLED/PitLED.cpp index 17822ae..823bceb 100644 --- a/PitLED/PitLED.cpp +++ b/PitLED/PitLED.cpp @@ -1,14 +1,9 @@ #include #include "PitLED.h" -#include "LEDFunction.h" +#include "RobototesColors.h" -#define NUMCOLORS 4 -CRGB CONSTCOLORS[NUMCOLORS] = { - CRGB::Red, CRGB::Blue, CRGB::White, CRGB::Black -}; - PitLED::PitLED(int strands, int ledsPerStrand) { numStrands = strands; numLEDsPerStrand = ledsPerStrand; @@ -39,7 +34,7 @@ PitLED::~PitLED() { delete leds; } -void PitLED::runFunction(LEDFunction *function, int d) { +void PitLED::runFunction(void (*func)(CRGB*, int, int, CRGB, int, int), int d) { CRGB color = CONSTCOLORS[random(NUMCOLORS)]; - function->execute(leds, numLEDsPerStrand, numStrands, color, d); + func(leds, numLEDsPerStrand, numStrands, color, d, 1); } diff --git a/PitLED/PitLED.h b/PitLED/PitLED.h index d53bb4e..85da542 100644 --- a/PitLED/PitLED.h +++ b/PitLED/PitLED.h @@ -1,5 +1,4 @@ #include -#include "LEDFunction.h" #ifndef PitLED_H #define PitLED_H @@ -10,7 +9,7 @@ class PitLED { public: PitLED(int strands, int ledsPerStrand); void init(); - void runFunction(LEDFunction *function, int d); + void runFunction(void (*func)(CRGB*, int, int, CRGB, int, int), int d); void show(); ~PitLED(); }; diff --git a/PitLED/PitLED.ino b/PitLED/PitLED.ino index 968685e..c0243c4 100644 --- a/PitLED/PitLED.ino +++ b/PitLED/PitLED.ino @@ -1,49 +1,31 @@ -#include -#include - -//#include - #include #include "PitLED.h" -#include "SwirlFunction.h" -#include "HSVFillFunction.h" -#include "HSVSwirlFunction.h" -#include "SweepFunction.h" -#include "RandomFadeFunction.h" -#include "RainFunction.h" -#include "SmoothFadeFunction.h" -#include "StartupFunction.h" -#include "LEDFunction.h" +#include "Functions.h" + #define NUM_LEDS 144 #define DATA_PIN 5 #define NUM_STRANDS 15 -CRGB leds[NUM_LEDS * NUM_STRANDS]; PitLED pitLed(NUM_STRANDS, NUM_LEDS); void setup() { Serial.begin(9600); Serial.println("Starting program..."); - StartupFunction * startupFunction = new StartupFunction(60); - pitLed.runFunction( startupFunction, 300); + pitLed.runFunction(&StartupFunction, 300); randomSeed(analogRead(0)); } void loop() { + void (* functions [5])(CRGB*, int, int, CRGB, int, int); - LEDFunction* functions[5]; - - functions[0] = new SwirlFunction(random(6) + 1); -// functions[1] = new HSVFillFunction(); -// functions[2] = new HSVSwirlFunction(random(6) + 1); - functions[1] = new SweepFunction(random(6) + 1); - functions[2] = new RandomFadeFunction(); - functions[3] = new RainFunction(); - functions[4] = new SmoothFadeFunction(); + functions[0] = &SwirlFunction; +// functions[1] = &HSVFillFunction; +// functions[2] = &HSVSwirlFunction; + functions[1] = &SweepFunction; + functions[2] = &RandomFadeFunction; + functions[3] = &RainFunction; + functions[4] = &SmoothFadeFunction; pitLed.runFunction(functions[random(5)], 20); - Serial.print("Free Memory: "); - Serial.println(freeMemory(), DEC); - Serial.println(); } diff --git a/PitLED/RainFunction.cpp b/PitLED/RainFunction.cpp deleted file mode 100644 index 95b3fdc..0000000 --- a/PitLED/RainFunction.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "RainFunction.h" -#include "Arduino.h" -#include "PitLED.h" -#include - - - - -RainFunction::RainFunction() { -} -void RainFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { - Serial.println("Running Rain function"); - int center = 72; - for (int dot = 0; dot < numLeds / 2; dot++) - { - leds[(center + dot) % numLeds] = color; // set this pixel to color - if (center - dot < 0) { - leds[(center - dot + numLeds) % numLeds] = color; - } else { - leds[(center - dot) % numLeds] = color; - } - - show(leds, numLeds, numStrands); - FastLED.delay(d); - } - - Serial.println("Rain function completed"); -} -void RainFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { - for (int i = 1; i < numStrands; i++) { - for (int j = 0; j < numLEDsPerStrand; j++) { - leds[numLEDsPerStrand * i + j] = leds[j]; - } - } - FastLED.show(); -} diff --git a/PitLED/RainFunction.h b/PitLED/RainFunction.h deleted file mode 100644 index c17b9ea..0000000 --- a/PitLED/RainFunction.h +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include "LEDFunction.h" -#ifndef RAIN_H -#define RAIN_H - -class RainFunction: public LEDFunction { - public: - RainFunction(); - void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); - void show(CRGB * leds, int numLeds, int numStrands); -}; - -#endif diff --git a/PitLED/RandomFadeFunction.cpp b/PitLED/RandomFadeFunction.cpp deleted file mode 100644 index 8760442..0000000 --- a/PitLED/RandomFadeFunction.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "RandomFadeFunction.h" -#include "Arduino.h" -#include "PitLED.h" -#include - - - - -RandomFadeFunction::RandomFadeFunction() { -} -void RandomFadeFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { - Serial.println("Running Random Fade function"); - - int numAlready = 0; - for (int i = 0; i < numLeds; i++) { - if (leds[i] == color) { - numAlready++; - } - } - for (int dotRemaining = 0; dotRemaining < (numLeds - numAlready); dotRemaining++) - { - int randomSelection = random(numLeds); - if (leds[randomSelection] == color) { - dotRemaining --; - } else { - leds[randomSelection] = color; // set this pixel to color - show(leds, numLeds, numStrands); - FastLED.delay(d); - } - } - Serial.println("Random Fade function completed"); -} -void RandomFadeFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { - for (int i = 1; i < numStrands; i++) { - for (int j = 0; j < numLEDsPerStrand; j++) { - leds[numLEDsPerStrand * i + j] = leds[j]; - } - } - FastLED.show(); -} diff --git a/PitLED/RandomFadeFunction.h b/PitLED/RandomFadeFunction.h deleted file mode 100644 index bf8d76d..0000000 --- a/PitLED/RandomFadeFunction.h +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include "LEDFunction.h" -#ifndef RANDOMFADE_H -#define RANDOMFADE_H - -class RandomFadeFunction: public LEDFunction { - public: - RandomFadeFunction(); - void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); - void show(CRGB * leds, int numLeds, int numStrands); -}; - -#endif diff --git a/PitLED/RobototesColors.h b/PitLED/RobototesColors.h new file mode 100644 index 0000000..c629239 --- /dev/null +++ b/PitLED/RobototesColors.h @@ -0,0 +1,54 @@ +#include + +#ifndef COLORS_H +#define COLORS_H + +#define NUMCOLORS 4 +CRGB CONSTCOLORS[NUMCOLORS] = { + CRGB::Red, CRGB::Blue, CRGB::White, CRGB::Black +}; + +const TProgmemPalette16 FirstColors PROGMEM = +{ + CRGB::Red, + CRGB::Blue, + CRGB::White +}; + +const TProgmemPalette16 Seahawks PROGMEM = +{ + CRGB::Blue, + CRGB::Green +}; + +const TProgmemPalette16 Huskies PROGMEM = +{ + CRGB::Yellow, + CRGB::Purple +}; + +const TProgmemPalette16 Robototes PROGMEM = +{ + CRGB::Red, + CRGB::Black, +}; + +const TProgmemPalette16 Sammamish PROGMEM = +{ + CRGB::Red, + CRGB::Black, + CRGB::White +}; +#define TWOCOLORS 1 +CRGBPalette16 twoColors[TWOCOLORS] = { + Robototes +// Huskies, +// Seahawks +}; +#define THREECOLORS 2 +CRGBPalette16 threeColors[THREECOLORS] = { + FirstColors, + Sammamish +}; + +#endif diff --git a/PitLED/SmoothFadeFunction.cpp b/PitLED/SmoothFadeFunction.cpp deleted file mode 100644 index ff5b8b3..0000000 --- a/PitLED/SmoothFadeFunction.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "SmoothFadeFunction.h" -#include "Arduino.h" -#include "PitLED.h" -#include - -double lerp(int from, int to, double percent) { - return from + (to - from) * percent; -} - -CRGB lerp(CRGB from, CRGB to, double percent) { - return CRGB( - (int)lerp(from.red, to.red, percent), - (int)lerp(from.green, to.green, percent), - (int)lerp(from.blue, to.blue, percent) - ); -} - - -SmoothFadeFunction::SmoothFadeFunction() { -} -void SmoothFadeFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { - Serial.println("Running Smooth Fade Function"); - - CRGB oldLeds[numLeds]; - for (int i = 0; i -#include "LEDFunction.h" -#ifndef SMOOTH_H -#define SMOOTH_H - -class SmoothFadeFunction: public LEDFunction { - public: - SmoothFadeFunction(); - void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); - void show(CRGB * leds, int numLeds, int numStrands); -}; - -#endif diff --git a/PitLED/StartupFunction.cpp b/PitLED/StartupFunction.cpp deleted file mode 100644 index 50d81e6..0000000 --- a/PitLED/StartupFunction.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "StartupFunction.h" -#include "Arduino.h" -#include "PitLED.h" -#include - -StartupFunction::StartupFunction(int _dMin) { - dMin = _dMin; -} -void StartupFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int startDelay) { - Serial.println("Running Startup function"); - color = CRGB::Red; - int d = startDelay; - int dSub = (startDelay - dMin) / (5 * numStrands); - for (int j = 0; j < numStrands; j++) { - for (int i = 61; i < 83; i++) { - leds[i + j * 144] = color; - } - FastLED.show(); - delay(d); - d -= dSub; - for (int i = 0; i < 12; i++) { - leds[83 + i + j * 144] = color; - leds[62 - i + j * 144] = color; - } - FastLED.show(); - delay(d); - d -= dSub; - for (int i = 0; i < 24; i++) { - leds[96 + i + j * 144] = color; - leds[49 - i + j * 144] = color; - } - FastLED.show(); - delay(d); - d -= dSub; - for (int i = 0; i < 12; i++) { - leds[121 + i + j * 144] = color; - leds[22 - i + j * 144] = color; - } - FastLED.show(); - delay(d); - d -= dSub; - for (int i = 134; i < 155; i++) { - leds[i % 144 + j * 144] = color; - } - FastLED.show(); - delay(d); - d -= dSub; - } - Serial.println("Startup function completed"); -} -void StartupFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { - for (int i = 1; i < numStrands; i++) { - for (int j = 0; j < numLEDsPerStrand; j++) { - leds[numLEDsPerStrand * i + j] = leds[j]; - } - } - FastLED.show(); -} diff --git a/PitLED/StartupFunction.h b/PitLED/StartupFunction.h deleted file mode 100644 index ce0ac0d..0000000 --- a/PitLED/StartupFunction.h +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include "LEDFunction.h" -#ifndef STARTUP_H -#define STARTUP_H - -class StartupFunction: public LEDFunction { - int dMin; - public: - StartupFunction(int _dMin); - void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); - void show(CRGB * leds, int numLeds, int numStrands); -}; - -#endif diff --git a/PitLED/SweepFunction.cpp b/PitLED/SweepFunction.cpp deleted file mode 100644 index 7e26fb9..0000000 --- a/PitLED/SweepFunction.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "SweepFunction.h" -#include "Arduino.h" -#include "PitLED.h" -#include - - - - -SweepFunction::SweepFunction(int _repeats) { - repeat = _repeats; -} -void SweepFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { - Serial.println("Running Sweep function"); - for (int dot = 0; dot < numLeds / repeat; dot++) - { - for (int i = 0; i < repeat; i++) - { - leds[(dot + i * numLeds / repeat) % numLeds] = color; // set this pixel to color, mod is to be safe - } - show(leds, numLeds, numStrands); - FastLED.delay(d); - } - Serial.println("Sweep function completed"); -} -void SweepFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { - for (int i = 1; i < numStrands; i++) { - for (int j = 0; j < numLEDsPerStrand; j++) { - leds[numLEDsPerStrand * i + j] = leds[j]; - } - } - FastLED.show(); -} diff --git a/PitLED/SweepFunction.h b/PitLED/SweepFunction.h deleted file mode 100644 index 432e6fc..0000000 --- a/PitLED/SweepFunction.h +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include "LEDFunction.h" -#ifndef SWEEP_H -#define SWEEP_H - -class SweepFunction: public LEDFunction { - int repeat; - public: - SweepFunction(int _repeats); - void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); - void show(CRGB * leds, int numLeds, int numStrands); -}; - -#endif diff --git a/PitLED/SwirlFunction.cpp b/PitLED/SwirlFunction.cpp deleted file mode 100644 index 74572bc..0000000 --- a/PitLED/SwirlFunction.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include "SwirlFunction.h" -#include "Arduino.h" -#include "PitLED.h" -#include - -const TProgmemPalette16 FirstColors PROGMEM = -{ - CRGB::Red, - CRGB::Blue, - CRGB::White -}; - -const TProgmemPalette16 Seahawks PROGMEM = -{ - CRGB::Blue, - CRGB::Green -}; - -const TProgmemPalette16 Huskies PROGMEM = -{ - CRGB::Yellow, - CRGB::Purple -}; - -const TProgmemPalette16 Robototes PROGMEM = -{ - CRGB::Red, - CRGB::Black, -}; - -const TProgmemPalette16 Sammamish PROGMEM = -{ - CRGB::Red, - CRGB::Black, - CRGB::White -}; -#define TWOCOLORS 1 -CRGBPalette16 twoColors[TWOCOLORS] = { - Robototes -// Huskies, -// Seahawks -}; -#define THREECOLORS 2 -CRGBPalette16 threeColors[THREECOLORS] = { - FirstColors, - Sammamish -}; - - - -SwirlFunction::SwirlFunction(int _repeats) { - repeat = _repeats; -} -void SwirlFunction::execute(CRGB* leds, int numLeds, int numStrands, CRGB color, int d) { - Serial.println("Running swirl function"); - int numColors = (int)random(2) + 2; - CRGBPalette16 palette; - if (numColors == 2) { - palette = twoColors[random(TWOCOLORS)]; - }else if(numColors == 3){ - palette = threeColors[random(THREECOLORS)]; - } - int segmentLength = (int) floor(numLeds / (numColors * repeat)); - for (int startingLEDIndex = 0; startingLEDIndex < numLeds; startingLEDIndex++) { // The index of the LED where the first color segment starts. - int colorIndex = 0; // The index of the current color we're using. - int lastTransitionLEDIndex = startingLEDIndex; // The index of the LED in between the segments (where the strip changes color). - for (int i = startingLEDIndex; i < startingLEDIndex + numLeds; i++) { - int scaledIndex = i % numLeds; // Rescale so we loop back around to the beginning if startingLEDIndex is greater than zero. - if (i - lastTransitionLEDIndex == segmentLength) { // New segment - colorIndex = (colorIndex + 1) % numColors; - lastTransitionLEDIndex = i; - } - leds[scaledIndex] = ColorFromPalette( palette, colorIndex * 16, 255, NOBLEND); - } - show(leds, numLeds, numStrands); - delay(d); - } - Serial.println("Swirl function completed"); -} -void SwirlFunction::show(CRGB* leds, int numLEDsPerStrand, int numStrands) { - for (int i = 1; i < numStrands; i++) { - for (int j = 0; j < numLEDsPerStrand; j++) { - leds[numLEDsPerStrand * i + j] = leds[j]; - } - } - FastLED.show(); -} diff --git a/PitLED/SwirlFunction.h b/PitLED/SwirlFunction.h deleted file mode 100644 index e1277bf..0000000 --- a/PitLED/SwirlFunction.h +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include "LEDFunction.h" -#ifndef SWIRL_H -#define SWIRL_H - -class SwirlFunction: public LEDFunction { - int repeat; - public: - SwirlFunction(int _repeats); - void execute(CRGB * leds, int numLeds, int numStrands, CRGB color, int d); - void show(CRGB * leds, int numLeds, int numStrands); -}; - -#endif From cdb671341e12666482fca5071b04398d0536c42f Mon Sep 17 00:00:00 2001 From: OroArmor Date: Mon, 15 Jun 2020 10:36:11 -0700 Subject: [PATCH 5/8] cleanup --- PitLED/Functions.h | 1 - PitLED/PitLED.ino | 6 ++++-- PitLED/RobototesColors.h | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/PitLED/Functions.h b/PitLED/Functions.h index c575dff..bf6a7b5 100644 --- a/PitLED/Functions.h +++ b/PitLED/Functions.h @@ -118,7 +118,6 @@ void SmoothFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, in Serial.println("Smooth Fade Function completed"); } - void StartupFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int startDelay, int repeats) { Serial.println("Running Startup function"); color = CRGB::Red; diff --git a/PitLED/PitLED.ino b/PitLED/PitLED.ino index c0243c4..0672c2f 100644 --- a/PitLED/PitLED.ino +++ b/PitLED/PitLED.ino @@ -1,6 +1,8 @@ #include -#include "PitLED.h" #include "Functions.h" +#include "PitLED.h" + + #define NUM_LEDS 144 #define DATA_PIN 5 @@ -12,7 +14,7 @@ void setup() Serial.begin(9600); Serial.println("Starting program..."); pitLed.runFunction(&StartupFunction, 300); - randomSeed(analogRead(0)); + randomSeed(0); } void loop() diff --git a/PitLED/RobototesColors.h b/PitLED/RobototesColors.h index c629239..25e3dd9 100644 --- a/PitLED/RobototesColors.h +++ b/PitLED/RobototesColors.h @@ -45,6 +45,7 @@ CRGBPalette16 twoColors[TWOCOLORS] = { // Huskies, // Seahawks }; + #define THREECOLORS 2 CRGBPalette16 threeColors[THREECOLORS] = { FirstColors, From d1dd926c69a2b50691d6f636805584514180f8f7 Mon Sep 17 00:00:00 2001 From: OroArmor Date: Mon, 15 Jun 2020 11:52:20 -0700 Subject: [PATCH 6/8] Yay its smarter now --- PitLED/Functions.cpp | 197 +++++++++++++++++++++++++++++++++++++++ PitLED/Functions.h | 196 +++----------------------------------- PitLED/PitLED.cpp | 8 +- PitLED/PitLED.h | 3 +- PitLED/PitLED.ino | 6 +- PitLED/RobototesColors.h | 6 +- 6 files changed, 222 insertions(+), 194 deletions(-) create mode 100644 PitLED/Functions.cpp diff --git a/PitLED/Functions.cpp b/PitLED/Functions.cpp new file mode 100644 index 0000000..6cc81ab --- /dev/null +++ b/PitLED/Functions.cpp @@ -0,0 +1,197 @@ +#include "Functions.h" + +void show(CRGB* leds, int numLEDsPerStrand, int numStrands) { + for (int i = 1; i < numStrands; i++) { + for (int j = 0; j < numLEDsPerStrand; j++) { + leds[numLEDsPerStrand * i + j] = leds[j]; + } + } + FastLED.show(); +} + +void HSVFillFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { + Serial.println("Starting HSVFill function"); + for (int i = 0; i < 255; i++) { + CRGB color; + hsv2rgb_rainbow(CHSV(i, 255, 255), color); + for (int j = 0; j < numLeds; j++) { + leds[j] = color; + } + show(leds, numLeds, numStrands); + FastLED.delay(d); + } + Serial.println("HSVFill function finished"); +} + +void HSVSwirlFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { + Serial.println("Running HSVSwirl function"); + for (int i = 0; i < numLeds; i++) { // one rotation + for (int k = 0 ; k < repeats; k++) { + for (int j = 0; j < numLeds / repeats; j += 1) { //this loop jumps to the start of each new color segment by that length + CRGB temp = CRGB(0, 0, 0); + hsv2rgb_rainbow(CHSV((double)map(j, 0, numLeds / repeats, 0, 255), 255, 255), temp); + leds[(i + j + k * numLeds / repeats) % numLeds] = temp; + } + } + show(leds, numLeds, numStrands); + FastLED.delay(d); + } + Serial.println("HSVSwirl function completed"); +} + +void RainFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { + Serial.println("Running Rain function"); + int center = 72; + for (int dot = 0; dot < numLeds / 2; dot++) + { + leds[(center + dot) % numLeds] = color; // set this pixel to color + if (center - dot < 0) { + leds[(center - dot + numLeds) % numLeds] = color; + } else { + leds[(center - dot) % numLeds] = color; + } + + show(leds, numLeds, numStrands); + FastLED.delay(d); + } + + Serial.println("Rain function completed"); +} + +void RandomFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { + Serial.println("Running Random Fade function"); + + int numAlready = 0; + for (int i = 0; i < numLeds; i++) { + if (leds[i] == color) { + numAlready++; + } + } + for (int dotRemaining = 0; dotRemaining < (numLeds - numAlready); dotRemaining++) + { + int randomSelection = random(numLeds); + if (leds[randomSelection] == color) { + dotRemaining --; + } else { + leds[randomSelection] = color; // set this pixel to color + show(leds, numLeds, numStrands); + FastLED.delay(d); + } + } + Serial.println("Random Fade function completed"); +} + +double lerp(int from, int to, double percent) { + return from + (to - from) * percent; +} + +CRGB lerp(CRGB from, CRGB to, double percent) { + return CRGB( + (int)lerp(from.red, to.red, percent), + (int)lerp(from.green, to.green, percent), + (int)lerp(from.blue, to.blue, percent) + ); +} + +void SmoothFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { + Serial.println("Running Smooth Fade Function"); + + CRGB oldLeds[numLeds]; + for (int i = 0; i Date: Mon, 15 Jun 2020 12:04:37 -0700 Subject: [PATCH 7/8] Maybe it doesnt work.... --- PitLED/Functions.cpp | 8 ++++---- PitLED/Functions.h | 18 +++++++++--------- PitLED/PitLED.ino | 6 +++--- PitLED/RobototesColors.h | 8 ++++---- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/PitLED/Functions.cpp b/PitLED/Functions.cpp index 6cc81ab..bf09265 100644 --- a/PitLED/Functions.cpp +++ b/PitLED/Functions.cpp @@ -95,9 +95,9 @@ CRGB lerp(CRGB from, CRGB to, double percent) { void SmoothFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { Serial.println("Running Smooth Fade Function"); - + CRGB oldLeds[numLeds]; - for (int i = 0; i Date: Mon, 15 Jun 2020 14:35:44 -0700 Subject: [PATCH 8/8] Yay its refactored --- PitLED/Functions.cpp | 27 ++++++++++++++++++++------- PitLED/Functions.h | 15 +++++++-------- PitLED/PitLED.ino | 28 +++++++++++++++------------- PitLED/RobototesColors.h | 12 ++++++------ 4 files changed, 48 insertions(+), 34 deletions(-) diff --git a/PitLED/Functions.cpp b/PitLED/Functions.cpp index bf09265..7f29e0a 100644 --- a/PitLED/Functions.cpp +++ b/PitLED/Functions.cpp @@ -9,6 +9,15 @@ void show(CRGB* leds, int numLEDsPerStrand, int numStrands) { FastLED.show(); } +void printColor(CRGB color) { + Serial.print("r: "); + Serial.print(color.r); + Serial.print(", g: "); + Serial.print(color.g); + Serial.print(", b: "); + Serial.println(color.b); +} + void HSVFillFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { Serial.println("Starting HSVFill function"); for (int i = 0; i < 255; i++) { @@ -18,7 +27,7 @@ void HSVFillFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, leds[j] = color; } show(leds, numLeds, numStrands); - FastLED.delay(d); + delay(d); } Serial.println("HSVFill function finished"); } @@ -34,13 +43,14 @@ void HSVSwirlFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int } } show(leds, numLeds, numStrands); - FastLED.delay(d); + delay(d); } Serial.println("HSVSwirl function completed"); } void RainFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { Serial.println("Running Rain function"); + printColor(color); int center = 72; for (int dot = 0; dot < numLeds / 2; dot++) { @@ -52,7 +62,7 @@ void RainFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, i } show(leds, numLeds, numStrands); - FastLED.delay(d); + delay(d); } Serial.println("Rain function completed"); @@ -60,6 +70,7 @@ void RainFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, i void RandomFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { Serial.println("Running Random Fade function"); + printColor(color); int numAlready = 0; for (int i = 0; i < numLeds; i++) { @@ -75,7 +86,7 @@ void RandomFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, in } else { leds[randomSelection] = color; // set this pixel to color show(leds, numLeds, numStrands); - FastLED.delay(d); + delay(d); } } Serial.println("Random Fade function completed"); @@ -95,7 +106,7 @@ CRGB lerp(CRGB from, CRGB to, double percent) { void SmoothFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { Serial.println("Running Smooth Fade Function"); - + printColor(color); CRGB oldLeds[numLeds]; for (int i = 0; i < numLeds; i++) { oldLeds[i] = leds[i]; @@ -106,7 +117,7 @@ void SmoothFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, in leds[j] = lerp(oldLeds[j], color, i); } show(leds, numLeds, numStrands); - FastLED.delay(d); + delay(d); } Serial.println("Smooth Fade Function completed"); @@ -157,6 +168,7 @@ void StartupFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int s void SweepFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { Serial.println("Running Sweep function"); + printColor(color); for (int dot = 0; dot < numLeds / repeats; dot++) { for (int i = 0; i < repeats; i++) @@ -164,13 +176,14 @@ void SweepFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, leds[(dot + i * numLeds / repeats) % numLeds] = color; // set this pixel to color, mod is to be safe } show(leds, numLeds, numStrands); - FastLED.delay(d); + delay(d); } Serial.println("Sweep function completed"); } void SwirlFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats) { Serial.println("Running swirl function"); + printColor(color); int numColors = (int)color.r % 2 + 2; CRGBPalette16 palette; if (numColors == 2) { diff --git a/PitLED/Functions.h b/PitLED/Functions.h index 40048b8..a66c983 100644 --- a/PitLED/Functions.h +++ b/PitLED/Functions.h @@ -1,6 +1,5 @@ #include #include "Arduino.h" -#include "PitLED.h" #include "RobototesColors.h" #ifndef FUNCTIONS_H @@ -10,19 +9,19 @@ void show(CRGB* leds, int numLEDsPerStrand, int numStrands); void HSVFillFunction (CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); -void HSVSwirlFunction (CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); +void HSVSwirlFunction (CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); -void RainFunction (CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); +void RainFunction (CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); -void RandomFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); +void RandomFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); -void SmoothFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); +void SmoothFadeFunction(CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); -void StartupFunction (CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); +void StartupFunction (CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); -void SweepFunction (CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); +void SweepFunction (CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); -void SwirlFunction (CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); +void SwirlFunction (CRGB* leds, int numLeds, int numStrands, CRGB color, int d, int repeats); double lerp(int from, int to, double percent); diff --git a/PitLED/PitLED.ino b/PitLED/PitLED.ino index 61e916b..183cca0 100644 --- a/PitLED/PitLED.ino +++ b/PitLED/PitLED.ino @@ -1,13 +1,22 @@ +#include #include #include "Functions.h" #include "PitLED.h" - - #define NUM_LEDS 144 #define DATA_PIN 5 #define NUM_STRANDS 6 +void (* functions[])(CRGB*, int, int, CRGB, int, int) = { + &SwirlFunction, + &HSVFillFunction, + &HSVSwirlFunction, + &SweepFunction, + &RandomFadeFunction, + &RainFunction, + &SmoothFadeFunction +}; + PitLED pitLed(NUM_STRANDS, NUM_LEDS); void setup() { @@ -19,15 +28,8 @@ void setup() void loop() { - void (* functions [5])(CRGB*, int, int, CRGB, int, int); - - functions[0] = &SwirlFunction; - // functions[1] = &HSVFillFunction; - // functions[2] = &HSVSwirlFunction; - functions[1] = &SweepFunction; - functions[2] = &RandomFadeFunction; - functions[3] = &RainFunction; - functions[4] = &SmoothFadeFunction; - - pitLed.runFunctionWithRandom(functions[random(5)], 40); + pitLed.runFunctionWithRandom(functions[random(7)], 20); + Serial.print("Free Memory: "); + Serial.println(freeMemory(), DEC); + Serial.println(); } diff --git a/PitLED/RobototesColors.h b/PitLED/RobototesColors.h index 1faf178..4e83cd4 100644 --- a/PitLED/RobototesColors.h +++ b/PitLED/RobototesColors.h @@ -3,9 +3,9 @@ #ifndef COLORS_H #define COLORS_H -#define NUMCOLORS 4 +#define NUMCOLORS 10 const CRGB CONSTCOLORS[NUMCOLORS] = { - CRGB::Red, CRGB::Blue, CRGB::White, CRGB::Black + CRGB::Red, CRGB::Blue, CRGB::White, CRGB::Black, CRGB::Green, CRGB::Orange, CRGB::Purple, CRGB::Cyan, CRGB::Yellow, CRGB::Gray }; const TProgmemPalette16 FirstColors PROGMEM = @@ -39,11 +39,11 @@ const TProgmemPalette16 Sammamish PROGMEM = CRGB::Black, CRGB::White }; -#define TWOCOLORS 1 +#define TWOCOLORS 3 const CRGBPalette16 twoColors[TWOCOLORS] = { - Robototes - // Huskies, - // Seahawks + Robototes, + Huskies, + Seahawks }; #define THREECOLORS 2