-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor tweaks to the lib, and enhancements to custom effects and examp…
…le sketches (#283) * Minor tweaks to the lib, and enhancments to custom effects and example sketches. * Added audio reactive example sketch * fixed a typo * Updated documentation
- Loading branch information
1 parent
4774866
commit 6f5783d
Showing
22 changed files
with
4,653 additions
and
927 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
examples/ws2812fx_audio_reactive/ws2812fx_audio_reactive.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
WS2812FX example sketch to demo sound reactivity | ||
Keith Lord - 2021 | ||
FEATURES | ||
* use an audio sensor (like those commonly made with an LM393 chip) to add | ||
sound reactivity to your LED lights. Connect the audio output of the sound | ||
detector (AO) to your microprocessor's analog input. | ||
* https://electropeak.com/learn/interfacing-ky-037-sound-sensor-with-arduino/ | ||
LICENSE | ||
The MIT License (MIT) | ||
Copyright (c) 2021 Keith Lord | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
CHANGELOG | ||
2021-04-02 initial version | ||
*/ | ||
|
||
#include <WS2812FX.h> | ||
|
||
#define LED_PIN 4 // digital pin used to drive the LED strip | ||
#define LED_COUNT 144 // number of LEDs on the strip | ||
|
||
#define MIN_BRIGHTNESS 1 | ||
#define MAX_BRIGHTNESS 255 | ||
#define THRESHOLD 5 | ||
|
||
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); | ||
|
||
uint16_t quietLevel = 0; | ||
uint16_t maxSample = 0; | ||
uint16_t maxSampleEver = 0; | ||
unsigned long timer = 0; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
|
||
// take some initial audio measurements to establish the "quiet" level | ||
for(int i=0; i<20; i++) { | ||
quietLevel += analogRead(A0); // 0-1023 | ||
delay(25); | ||
} | ||
quietLevel /= 20; | ||
Serial.print("\nquietLevel is "); Serial.println(quietLevel); | ||
|
||
ws2812fx.init(); | ||
ws2812fx.setBrightness(64); | ||
|
||
// parameters: index, start, stop, mode, color, speed, reverse | ||
ws2812fx.setSegment(0, 0, LED_COUNT-1, FX_MODE_STATIC, RED, 8000, NO_OPTIONS); | ||
|
||
ws2812fx.start(); | ||
} | ||
|
||
void loop() { | ||
// take an audio sample | ||
uint16_t audioSample = abs(analogRead(A0) - quietLevel); | ||
if(audioSample > maxSample) maxSample = audioSample; | ||
|
||
// if the timer has expired, use the sampled audio to recalculate the LED brightness | ||
if(millis() > timer) { | ||
if(maxSample > THRESHOLD) { // ensure the audio is above the threshold to reduce LED flickering | ||
if(maxSample > maxSampleEver) maxSampleEver = maxSample; | ||
|
||
// calculate a new brightness, properly scaled to the sampled audio | ||
uint8_t newBrightness = map(maxSample, THRESHOLD, maxSampleEver, MIN_BRIGHTNESS, MAX_BRIGHTNESS); | ||
// Serial.print("maxSample:");Serial.print(maxSample); // debug | ||
// Serial.print(", maxSampleEver:");Serial.print(maxSampleEver); | ||
// Serial.print(", newBrightness:");Serial.println(newBrightness); | ||
ws2812fx.setBrightness(newBrightness); | ||
} else { | ||
ws2812fx.setBrightness(MIN_BRIGHTNESS); | ||
} | ||
|
||
maxSample = 0; | ||
timer = millis() + 100; // recalc brightness every 100ms | ||
} | ||
|
||
ws2812fx.service(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Adafruit_NeoMatrix example for single NeoPixel Shield. | ||
// Scrolls 'Howdy' across the matrix in a portrait (vertical) orientation. | ||
|
||
#include <Adafruit_GFX.h> | ||
#include <Adafruit_NeoMatrix.h> | ||
#include <Adafruit_NeoPixel.h> | ||
#ifndef PSTR | ||
#define PSTR // Make Arduino Due happy | ||
#endif | ||
|
||
#define PIN D5 | ||
|
||
// MATRIX DECLARATION: | ||
// Parameter 1 = width of NeoPixel matrix | ||
// Parameter 2 = height of matrix | ||
// Parameter 3 = pin number (most are valid) | ||
// Parameter 4 = matrix layout flags, add together as needed: | ||
// NEO_MATRIX_TOP, NEO_MATRIX_BOTTOM, NEO_MATRIX_LEFT, NEO_MATRIX_RIGHT: | ||
// Position of the FIRST LED in the matrix; pick two, e.g. | ||
// NEO_MATRIX_TOP + NEO_MATRIX_LEFT for the top-left corner. | ||
// NEO_MATRIX_ROWS, NEO_MATRIX_COLUMNS: LEDs are arranged in horizontal | ||
// rows or in vertical columns, respectively; pick one or the other. | ||
// NEO_MATRIX_PROGRESSIVE, NEO_MATRIX_ZIGZAG: all rows/columns proceed | ||
// in the same order, or alternate lines reverse direction; pick one. | ||
// See example below for these values in action. | ||
// Parameter 5 = pixel type flags, add together as needed: | ||
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) | ||
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) | ||
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) | ||
// NEO_GRBW Pixels are wired for GRBW bitstream (RGB+W NeoPixel products) | ||
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) | ||
|
||
|
||
// Example for NeoPixel Shield. In this application we'd like to use it | ||
// as a 5x8 tall matrix, with the USB port positioned at the top of the | ||
// Arduino. When held that way, the first pixel is at the top right, and | ||
// lines are arranged in columns, progressive order. The shield uses | ||
// 800 KHz (v2) pixels that expect GRB color data. | ||
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(5, 8, PIN, | ||
NEO_MATRIX_TOP + NEO_MATRIX_RIGHT + | ||
NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE, | ||
NEO_GRB + NEO_KHZ800); | ||
|
||
|
||
int16_t x = matrix.width(); | ||
uint8_t color_index = 0; | ||
char text[] = "Howdy"; | ||
int scroll_limit = sizeof(text) * 6; // each text character is 6 pixels wide | ||
|
||
void setup() { | ||
matrix.begin(); | ||
matrix.setTextWrap(false); | ||
matrix.setBrightness(40); | ||
matrix.setTextColor(matrix.Color(255, 0, 0)); // default color is red | ||
} | ||
|
||
void loop() { | ||
matrix.fillScreen(0); | ||
matrix.setCursor(x, 0); | ||
matrix.print(text); | ||
|
||
if(--x < -scroll_limit) { | ||
x = matrix.width(); | ||
color_index = 0; // reset the rainbow color index | ||
} | ||
matrix.setTextColor(color_wheel(color_index)); | ||
color_index += max(1, 256 / scroll_limit); | ||
matrix.show(); | ||
delay(100); | ||
} | ||
|
||
uint16_t color_wheel(uint8_t pos) { | ||
pos = 255 - pos; | ||
if(pos < 85) { | ||
// return ((uint32_t)(255 - pos * 3) << 16) | ((uint32_t)(0) << 8) | (pos * 3); | ||
return matrix.Color((uint16_t)(255 - pos * 3), 0, (pos * 3)); | ||
} else if(pos < 170) { | ||
pos -= 85; | ||
// return ((uint32_t)(0) << 16) | ((uint32_t)(pos * 3) << 8) | (255 - pos * 3); | ||
return matrix.Color(0, (uint32_t)(pos * 3), (255 - pos * 3)); | ||
} else { | ||
pos -= 170; | ||
// return ((uint32_t)(pos * 3) << 16) | ((uint32_t)(255 - pos * 3) << 8) | (0); | ||
return matrix.Color((uint16_t)(pos * 3), (uint32_t)(255 - pos * 3), 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <pgmspace.h> | ||
|
||
#ifndef _APP_CSS_ | ||
#define _APP_CSS_ | ||
|
||
const char app_css[] PROGMEM = R"=====( | ||
/* tweaks for Material Design Components */ | ||
body { | ||
margin: 0; /* remove the default 8px body margin to fix MDC top-app-bar placement */ | ||
/* --mdc-theme-primary: #3fb8af; */ /* change MDC color theme to match noUiSlide's theme */ | ||
} | ||
.mdc-top-app-bar { /* force the top bar text color to white */ | ||
color: #ffffff; | ||
} | ||
main, .mdc-switch { | ||
margin: 0px 8px; /* add a little margin the MDC switches */ | ||
} | ||
h3 { | ||
margin: 4px 0px; /* add a little margin to the widget labels */ | ||
} | ||
/* MDC select and text-field labels are hardcoded to purple??? reset to primary color */ | ||
/* .mdc-select:not(.mdc-select--disabled).mdc-select--focused .mdc-floating-label { | ||
color: var(--mdc-theme-primary, #3fb8af) | ||
} | ||
.mdc-text-field--focused:not(.mdc-text-field--disabled) .mdc-floating-label { | ||
color: var(--mdc-theme-primary, #3fb8af); | ||
} */ | ||
.mdc-list, div.border { /* add border around MDC lists */ | ||
border: 1px solid rgba(0, 0, 0, 0.1); | ||
padding: 0px; | ||
} | ||
.mdc-list-item { /* make list items a little shorter (default is 48px) */ | ||
height: 32px; | ||
} | ||
.mdc-button { /* add margin-bottom to buttons so they wrap properly on small screens */ | ||
margin-bottom: 8px; | ||
} | ||
.noUi-tooltip { /* only show slider tooltips when the slider is being moved */ | ||
display: none; | ||
} | ||
.noUi-active .noUi-tooltip { | ||
display: block; | ||
} | ||
footer { | ||
margin: 4px 16px; /* add a little margin to the footer content */ | ||
} | ||
|
||
div.fine-border { /* add a border */ | ||
border: 1px solid rgba(0, 0, 0, 0.1); | ||
padding: 4px 16px; | ||
} | ||
)====="; | ||
#endif |
Oops, something went wrong.
6f5783d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool update