PitLedRingStrip - Utilizzo delle stringhe led Neopixel
L'utilizzo delle stringhe led NeoPixel con le librerie Neopixel di Adafruit e FastLed.
Tra un po' arriva il Natale, quest'anno l'albero potrebbe essere addobbato con alcuni anelli di striscie led. Per fare questo è importante incominciare a farci la mano.
Nel seguito alcuni esempi da provare con le due principali librerie che indirizzano i led ring strip. Gli esempi sono modifiche degli esempi contenuti nelle librerie originarie prendendo spunti da alcuni articoli trovati sul WEB e citati all'interno del codice.
Vediamo se per Natale si riuscirà a costruire un nuovo tipo di albero di Natale.
Esempio con libreria NeoPixel:
Esempio con libreria FastLed - 2 pin:
Cosa serve:
- Alcune LedRingStrip
- cavetti
- libreria FastLed
- libreria NeoPixel
Codice sorgente libreria NeoPixel:
// modifiche degli esempi delle librerie neopixel
// by Zappoco
// www.zappoco.altervista.org
//
// spunti da
// https://www.9minuti.it/led-rgb-arduino-libreria-neopixel/
// Simple NeoPixel test. Lights just a few pixels at a time so a
// 1m strip can safely be powered from Arduino 5V pin. Arduino
// may nonetheless hiccup when LEDs are first connected and not
// accept code. So upload code first, unplug USB, connect pixels
// to GND FIRST, then +5V and digital pin 6, then re-plug USB.
// A working strip will show a few pixels moving down the line,
// cycling between red, green and blue. If you get no response,
// might be connected to wrong end of strip (the end wires, if
// any, are no indicatiPOn -- look instead for the data directiPOn
// arrows printed on the strip).
// spunti e da/*
http://www.sciamannalucio.it/arduino-come-usare-led-rgb-neopixel/
Programma per utilizzare un anello di 24 led RGB NeoPixel
Puoi utilizzare un quasiasi pin di Arduino per la trasmissiPOne dei dati
di programmaziPOne. E' consigliato interporre tra il pin di Arduino ed il
"Data Input" del led una resistenza da 470 Ohm.
*/
#include "Adafruit_NeoPixel.h"
#define PIN 6
#define N_LEDS 28
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800);
uint32_t red = strip.Color(255, 0, 0); // Red
uint32_t green = strip.Color(0, 255, 0); // Green
uint32_t blue = strip.Color(0, 0, 255); // Blue
/*
uint32_t purple = strip.Color(255, 0, 255); // Purple
uint32_t lightBlue = strip.Color(0, 255, 255); // Light blue
uint32_t yellow = strip.Color(255, 255, 0); // Yellow
uint32_t white = strip.Color(255, 255, 255); // White
uint32_t lightBrightGreen = strip.Color( 0, 63, 0); //1/4 bright green
uint32_t brightCyan = strip.Color( 0, 255, 255); //full-bright cyan
uint32_t brightYellow = strip.Color(127, 127, 0); //half-bright yellow
uint32_t orange = strip.Color(255, 192, 255); //orange
uint32_t lightBrightWhite = strip.Color( 63, 63, 63); //1/4-bright white
*/
void setup() {
Serial.begin(9600);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
strip.setBrightness(255); //30-255
}
void loop() {
//
// Esempi libreria NeoPixel con modifiche a StranTest e 9minuti by Zappoco
//
/*
theaterChaseZap(strip.Color(127, 0 , 0), 50, 3, 1, 6); // Red
theaterChaseZap(strip.Color( 0, 127 , 0), 50, 3, 1, 6); // Green
theaterChaseZap(strip.Color( 0, 0 , 127), 50, 3, 1, 6); // blue
theaterChaseZap(strip.Color(127, 0 , 0), 50, 3, -1, 6); // Red
theaterChaseZap(strip.Color( 0, 127 , 0), 50, 3, -1, 6); // Green
theaterChaseZap(strip.Color( 0, 0 , 127), 50, 3, -1, 6); // blue
chaseZap(strip.Color(127, 0, 0), 4, 1, 50, 0, 3);
chaseZap(strip.Color( 0, 127, 0), 4, 1, 50, 0, 3);
chaseZap(strip.Color( 0, 0, 127), 4, 1, 50, 0, 3);
chaseZap(strip.Color(127, 0, 0), 4, -1, 50, 0, 3);
chaseZap(strip.Color( 0, 127, 0), 4, -1, 50, 0, 3);
chaseZap(strip.Color( 0, 0, 127), 4, -1, 50, 0, 3);
simpleZap(strip.Color(255, 0, 0), 50);
simpleZap(strip.Color( 0,255, 0), 50);
simpleZap(strip.Color( 0, 0,255), 50);
*/
//chaseZapMir(red , 4, 1, 1, 150, 0, 3);
//delay(3000);
// /*
simpleZapMir(red ,red , 50 , 1, 1);
simpleZapMir(green,green, 50 , 1, 1);
simpleZapMir(blue ,blue , 50 , 1, 1);
simpleZapMir(red ,red , 50 , -1, -1);
simpleZapMir(green,green, 50 , -1, -1);
simpleZapMir(blue ,blue , 50 , -1, -1);
simpleZapMir(red ,red , 50 , 1, -1);
simpleZapMir(green,green, 50 , 1, -1);
simpleZapMir(blue ,blue , 50 , 1, -1);
simpleZapMir(red ,red , 50 , -1, 1);
simpleZapMir(green,green, 50 , -1, 1);
simpleZapMir(blue ,blue , 50 , -1, 1);
// */
//
// Esempi della libreria NeoPixel StranTest
//
/*
//
// Some example procedures showing how to display to the pixels:
//
colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color(0, 255, 0), 50); // Green
colorWipe(strip.Color(0, 0, 255), 50); // Blue
colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW
// Send a theater pixel chase in...
theaterChase(strip.Color(127, 127, 127), 250); // White
theaterChase(strip.Color(127, 0 , 0 ), 50); // Red
theaterChase(strip.Color(0 , 127, 0 ), 50); // White
theaterChase(strip.Color(0 , 0 , 127), 50); // Blue
*/
/*
rainbow(20);
rainbowCycle(20);
theaterChaseRainbow(20);
*/
}
static void simpleZap(uint32_t c, int wait) {
for(int i=0;i numero di pixel accesi
*
* dir --> maggiore di zero senso orario
* minore di zero senso antiorario
* wait --> tempo di delay tra un pixel e l'altro
*
* startP --> pixel di partenza
*
* numGiri --> numero di giri
*/
int iPOn, iPOff;
for (int j = 0; j < numGiri; j++){
for (int i = 0; i < strip.numPixels(); i++) {
//Serial.print ("i = ");Serial.print (i);
if (dir >= 0) {
iPOn = (i + startP) % strip.numPixels() ;
if (iPOn - numOn >= 0){
iPOff = iPOn - numOn;
Serial.print (" >>>> ");
}
else {
iPOff = strip.numPixels() + iPOn - numOn;
Serial.print (" <<<< ");
}
} else {
iPOn = strip.numPixels() - i -1 -startP;
if (iPOn < 0){
iPOn = strip.numPixels() + iPOn;
}
//if (i - numOn >= 0){
if (iPOn + numOn < strip.numPixels()){
iPOff = iPOn + numOn;
Serial.print (" >>>> ");
}
else {
iPOff = -strip.numPixels() + iPOn + numOn;
Serial.print (" <<<< ");
}
}
Serial.print (" i = ");Serial.print (i);
Serial.print (" iPOn = ");Serial.print (iPOn);
Serial.print (" iPOff = ");Serial.println (iPOff);
strip.setPixelColor(iPOn , c); // Draw new pixel
strip.setPixelColor(iPOff , 0); // Erase pixel a few steps back
/*
if (i - numOn >= 0){
//Serial.print (" i-4 = ");Serial.println (i-4);
strip.setPixelColor(i - numOn, 0); // Erase pixel a few steps back
}
else{
//Serial.print (" n+i-4 = ");Serial.println (strip.numPixels() + i - 4);
strip.setPixelColor(strip.numPixels()+ i - numOn, 0); // Erase pixel a few steps back
}
*/
strip.show();
delay(wait);
}
}
}
static void chaseZapMir(uint32_t c, int numOn, int dir1, int dir2, int wait, int startP, int numGiri) {
/*
* numOn --> numero di pixel accesi
*
* dir --> maggiore di zero senso orario
* minore di zero senso antiorario
* wait --> tempo di delay tra un pixel e l'altro
*
* startP --> pixel di partenza
*
* numGiri --> numero di giri
*/
int iPOn1, iPOff1, iPOn2, iPOff2;;
for (int j = 0; j < numGiri; j++){
for (int i = 0; i < strip.numPixels()/2; i++) {
Serial.print ("i = ");Serial.print (i);
if (dir1 >= 0) {
iPOn1 = (i + startP) % strip.numPixels()/2 ;
if (iPOn1 - numOn >= 0){
iPOff1 = iPOn1 - numOn;
Serial.print (" >>>> ");
}
else {
iPOff1 = strip.numPixels()/2 + iPOn1 - numOn;
Serial.print (" <<<< ");
}
} else {
iPOn1 = strip.numPixels()/2 - i -1 -startP;
if (iPOn1 < 0){
iPOn1 = strip.numPixels()/2 + iPOn1;
}
//if (i - numOn >= 0){
if (iPOn1 + numOn < strip.numPixels()/2){
iPOff1 = iPOn1 + numOn;
Serial.print (" >>>> ");
}
else {
iPOff1 = -strip.numPixels()/2 + iPOn1 + numOn;
Serial.print (" <<<< ");
}
}
Serial.print (" i = ");Serial.print (i);
Serial.print (" iPOn = ");Serial.print (iPOn1);
Serial.print (" iPOff = ");Serial.println (iPOff1);
strip.setPixelColor(iPOn1 , c); // Draw new pixel
strip.setPixelColor(iPOff1 , 0); // Erase pixel a few steps back
strip.show();
delay(wait);
}
}
}
//Theatre-style crawling lights.
void theaterChaseZap (uint32_t c, int wait, int numOn, int dir, int numGiri) {
/*
* numOn --> numero di pixel accesi
*
* dir --> maggiore di zero senso orario
* minore di zero senso antiorario
* wait --> tempo di delay tra un pixel e l'altro
*
* startP --> pixel di partenza
*
* numGiri --> numero di giri
*/
int iPOn, iPOff;
for (int j=0; j= 0) {
for (int i=0; i < strip.numPixels(); i=i+numOn) {
iPOn = (i + q) % strip.numPixels() ;
strip.setPixelColor(iPOn, c); //turn every third pixel on
//Serial.print (" i = ");Serial.print (i);
//Serial.print (" iPOn = ");Serial.println (iPOn);
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+numOn) {
iPOff = (i + q) % strip.numPixels() ;
strip.setPixelColor(iPOff, 0); //turn every third pixel off
//Serial.print (" i = ");Serial.print (i);
//Serial.print (" iPOff = ");Serial.println (iPOff);
}
} else {
for (int i=0; i < strip.numPixels(); i=i+numOn) {
iPOn = strip.numPixels() - ((i + q) % strip.numPixels())-1 ;
strip.setPixelColor(iPOn, c); //turn every third pixel on
//Serial.print (" i = ");Serial.print (i);
//Serial.print (" iPOn = ");Serial.println (iPOn);
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+numOn) {
iPOff = strip.numPixels() - ((i + q) % strip.numPixels())-1 ;
strip.setPixelColor(iPOff, 0); //turn every third pixel off
//Serial.print (" i = ");Serial.print (i);
//Serial.print (" iPOff = ");Serial.println (iPOff);
}
}
}
}
}
//
// Esempio della libreria NeoPixel da 9minuti02
//
static void chase(uint32_t c) {
for (uint16_t i = 0; i < strip.numPixels() + 4; i++) {
strip.setPixelColor(i , c); // Draw new pixel
strip.setPixelColor(i - 4, 0); // Erase pixel a few steps back
strip.show();
delay(150);
}
}
//
// Esempio della libreria NeoPixel StranTest
//
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i
Codice sorgente libreria FastLed:
#include "FastLED.h"
#define LED_PIN1 6
#define LED_PIN2 7
#define NUM_LEDS 28
#define BRIGHTNESS 64
//#define LED_TYPE WS2811
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
// This example shows several ways to set up and use 'palettes' of colors
// with FastLED.
//
// These compact palettes provide an easy way to re-colorize your
// animation on the fly, quickly, easily, and with low overhead.
//
// USING palettes is MUCH simpler in practice than in theory, so first just
// run this sketch, and watch the pretty lights as you then read through
// the code. Although this sketch has eight (or more) different color schemes,
// the entire sketch compiles down to about 6.5K on AVR.
//
// FastLED provides a few pre-configured color palettes, and makes it
// extremely easy to make up your own color schemes with palettes.
//
// Some notes on the more abstract 'theory and practice' of
// FastLED compact palettes are at the bottom of this file.
CRGBPalette16 currentPalette;
TBlendType currentBlending;
extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
void setup() {
delay( 3000 ); // power-up safety delay
FastLED.addLeds(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
}
void loop()
{
ChangePalettePeriodically();
static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
FillLEDsFromPaletteColors( startIndex);
FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);
}
void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
uint8_t brightness = 255;
for ( int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
colorIndex += 3;
}
}
// There are several different palettes of colors demonstrated here.
//
// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
//
// Additionally, you can manually define your own color palettes, or you can write
// code that creates color palettes on the fly. All are shown here.
void ChangePalettePeriodically()
{
uint8_t secondHand = (millis() / 1000) % 60;
static uint8_t lastSecond = 99;
if ( lastSecond != secondHand) {
lastSecond = secondHand;
if ( secondHand == 0) {
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
}
if ( secondHand == 10) {
currentPalette = RainbowStripeColors_p;
currentBlending = NOBLEND;
}
if ( secondHand == 15) {
currentPalette = RainbowStripeColors_p;
currentBlending = LINEARBLEND;
}
if ( secondHand == 20) {
SetupPurpleAndGreenPalette();
currentBlending = LINEARBLEND;
}
if ( secondHand == 25) {
SetupTotallyRandomPalette();
currentBlending = LINEARBLEND;
}
if ( secondHand == 30) {
SetupBlackAndWhiteStripedPalette();
currentBlending = NOBLEND;
}
if ( secondHand == 35) {
SetupBlackAndWhiteStripedPalette();
currentBlending = LINEARBLEND;
}
if ( secondHand == 40) {
currentPalette = CloudColors_p;
currentBlending = LINEARBLEND;
}
if ( secondHand == 45) {
currentPalette = PartyColors_p;
currentBlending = LINEARBLEND;
}
if ( secondHand == 50) {
currentPalette = myRedWhiteBluePalette_p;
currentBlending = NOBLEND;
}
if ( secondHand == 55) {
currentPalette = myRedWhiteBluePalette_p;
currentBlending = LINEARBLEND;
}
}
}
// This function fills the palette with totally random colors.
void SetupTotallyRandomPalette()
{
for ( int i = 0; i < 16; i++) {
currentPalette[i] = CHSV( random8(), 255, random8());
}
}
// This function sets up a palette of black and white stripes,
// using code. Since the palette is effectively an array of
// sixteen CRGB colors, the various fill_* functions can be used
// to set them up.
void SetupBlackAndWhiteStripedPalette()
{
// 'black out' all 16 palette entries...
fill_solid( currentPalette, 16, CRGB::Black);
// and set every fourth one to white.
currentPalette[0] = CRGB::White;
currentPalette[4] = CRGB::White;
currentPalette[8] = CRGB::White;
currentPalette[12] = CRGB::White;
}
// This function sets up a palette of purple and green stripes.
void SetupPurpleAndGreenPalette()
{
CRGB purple = CHSV( HUE_PURPLE, 255, 255);
CRGB green = CHSV( HUE_GREEN, 255, 255);
CRGB black = CRGB::Black;
currentPalette = CRGBPalette16(
green, green, black, black,
purple, purple, black, black,
green, green, black, black,
purple, purple, black, black );
}
// This example shows how to set up a static color palette
// which is stored in PROGMEM (flash), which is almost always more
// plentiful than RAM. A static PROGMEM palette like this
// takes up 64 bytes of flash.
const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
{
CRGB::Red,
CRGB::Gray, // 'white' is too bright compared to red and blue
CRGB::Blue,
CRGB::Black,
CRGB::Red,
CRGB::Gray,
CRGB::Blue,
CRGB::Black,
CRGB::Red,
CRGB::Red,
CRGB::Gray,
CRGB::Gray,
CRGB::Blue,
CRGB::Blue,
CRGB::Black,
CRGB::Black
};
// Additional notes on FastLED compact palettes:
//
// Normally, in computer graphics, the palette (or "color lookup table")
// has 256 entries, each containing a specific 24-bit RGB color. You can then
// index into the color palette using a simple 8-bit (one byte) value.
// A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
// is quite possibly "too many" bytes.
//
// FastLED does offer traditional 256-element palettes, for setups that
// can afford the 768-byte cost in RAM.
//
// However, FastLED also offers a compact alternative. FastLED offers
// palettes that store 16 distinct entries, but can be accessed AS IF
// they actually have 256 entries; this is accomplished by interpolating
// between the 16 explicit entries to create fifteen intermediate palette
// entries between each pair.
//
// So for example, if you set the first two explicit entries of a compact
// palette to Green (0,255,0) and Blue (0,0,255), and then retrieved
// the first sixteen entries from the virtual palette (of 256), you'd get
// Green, followed by a smooth gradient from green-to-blue, and then Blue.