This code example uses the FastLED library, and is modified from a sketch called DesignerPalettes by Mark Kriegsman.
It's a great idea to get your software all set up and loaded onto your board right away, to make testing your connections easier later on.
To get the code running you'll need:
- Arduino IDE (1.8 or newer)
- Adafruit Board support for QT Py
- Arduino libraries: FastLED, Adafruit_NeoPixel
1. Arduino IDE
If you’re not using a recent version of the Arduino IDE, this would be a good time to upgrade. If this is your first time using Arduino, head over to this guide to get it installed. It's free and fairly simple to get set up.
2. Board Support
You'll need to tell the Arduino IDE which board you're using. This takes just a few minutes of setup, and you'll only need to do it once.
3. Libraries
Both libraries can be installed using the Arduino Library Manager — use Sketch → Include Library → Manage Libraries… and search for all or part of the library’s name, then click “Install.”
Look for:
FastLEDAdafruit_NeoPixel
Adafruit_NeoPixel isn’t absolutely required for this project, but it’s handy to have installed in case you have problems with FastLED. Troubleshooting the basics is a little easier with Adafruit_NeoPixel.
Upload the Code
Get the code by clicking "Download Project Zip" in the code listing below. Unzip the code into your Arduino project folder.
Plug your microcontroller into your computer with a USB cable. In the Arduino IDE, go to File -> Open to open the code file. Go to Tools -> Boards and select the name of the board: Adafruit QT PY (SAMD21). Then go to Tools -> Port and select the board there too. (If it's not showing up there, be sure your microcontroller is plugged into your computer via USB).
// SPDX-FileCopyrightText: 2024 Erin St Blaine for Adafruit Industries
//
// SPDX-License-Identifier: MIT
//
// Based on ColorWavesWithPalettes
// Animated shifting color waves, with several cross-fading color palettes.
// by Mark Kriegsman, August 2015
//
//
// Color palettes courtesy of cpt-city and its contributors:
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/
//
// Color palettes converted for FastLED using "PaletteKnife" v1:
// http://fastled.io/tools/paletteknife/
//
#include "FastLED.h"
#if defined(FASTLED_VERSION) && FASTLED_VERSION > 3010001
#error "FastLED 3.10.2 has known compile issues with SAMD boards. Please downgrade to FastLED 3.10.1"
#endif
#define DATA_PIN SCL
#define LED_TYPE WS2812
#define COLOR_ORDER RBG // If colors are coming out wrong, re-order (RGB, BRG, etc)
#define NUM_LEDS 150 // Change this to reflect the number of LEDs you have
#define BRIGHTNESS 255 // Set Brightness here
CRGB leds[NUM_LEDS];
// ten seconds per color palette makes a good demo
// 20-120 is better for deployment
#define SECONDS_PER_PALETTE 30
void setup() {
delay(1000); // 3 second delay for recovery
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE,DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS)
.setCorrection(TypicalLEDStrip) // cpt-city palettes have different color balance
.setDither(BRIGHTNESS < 255);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
}
// Forward declarations of an array of cpt-city gradient palettes, and
// a count of how many there are. The actual color palette definitions
// are at the bottom of this file.
extern const TProgmemRGBGradientPaletteRef gGradientPalettes[];
extern const uint8_t gGradientPaletteCount;
// Current palette number from the 'playlist' of color palettes
uint8_t gCurrentPaletteNumber = 0;
CRGBPalette16 gCurrentPalette( CRGB::Black);
CRGBPalette16 gTargetPalette( gGradientPalettes[0] );
// This function draws color waves with an ever-changing,
// widely-varying set of parameters, using a color palette.
void colorwaves( CRGB* ledarray, uint16_t numleds, CRGBPalette16& palette)
{
static uint16_t sPseudotime = 0;
static uint16_t sLastMillis = 0;
static uint16_t sHue16 = 0;
uint8_t sat8 = beatsin88( 87, 220, 250);
uint8_t brightdepth = beatsin88( 341, 225, 255);
uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
uint8_t msmultiplier = beatsin88(147, 23, 60);
uint16_t hue16 = sHue16;//gHue * 256;
uint16_t hueinc16 = beatsin88(113, 300, 1500);
uint16_t ms = millis();
uint16_t deltams = ms - sLastMillis ;
sLastMillis = ms;
sPseudotime += deltams * msmultiplier;
sHue16 += deltams * beatsin88( 400, 5,9);
uint16_t brightnesstheta16 = sPseudotime;
for( uint16_t i = 0 ; i < numleds; i++) {
hue16 += hueinc16;
uint8_t hue8 = hue16 / 256;
uint16_t h16_128 = hue16 >> 7;
if( h16_128 & 0x100) {
hue8 = 255 - (h16_128 >> 1);
} else {
hue8 = h16_128 >> 1;
}
brightnesstheta16 += brightnessthetainc16;
uint16_t b16 = sin16( brightnesstheta16 ) + 32768;
uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
bri8 += (255 - brightdepth);
uint8_t index = hue8;
//index = triwave8( index);
index = scale8( index, 240);
CRGB newcolor = ColorFromPalette( palette, index, bri8);
uint16_t pixelnumber = i;
pixelnumber = (numleds-1) - pixelnumber;
nblend( ledarray[pixelnumber], newcolor, 128);
}
}
// Alternate rendering function just scrolls the current palette
// across the defined LED strip.
void palettetest( CRGB* ledarray, uint16_t numleds, const CRGBPalette16& gCurrentPalette)
{
static uint8_t startindex = 0;
startindex--;
fill_palette( ledarray, numleds, startindex, (256 / NUM_LEDS) + 1, gCurrentPalette, 255, LINEARBLEND);
}
// Gradient palette "bhw1_01_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw1/tn/bhw1_01.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 12 bytes of program space.
DEFINE_GRADIENT_PALETTE( bhw1_01_gp ) {
0, 227,101, 3,
117, 194, 18, 19,
255, 92, 8,192};
// Gradient palette "bhw1_07_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw1/tn/bhw1_07.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 8 bytes of program space.
DEFINE_GRADIENT_PALETTE( bhw1_07_gp ) {
0, 232, 65, 1,
255, 229,227, 1};
// Gradient palette "bhw1_sunset3_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw1/tn/bhw1_sunset3.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 28 bytes of program space.
DEFINE_GRADIENT_PALETTE( bhw1_sunset3_gp ) {
0, 227,237, 56,
33, 186, 67, 1,
71, 163, 21, 1,
81, 157, 13, 1,
188, 39, 21, 18,
234, 12, 7, 4,
255, 12, 7, 4};
// Gradient palette "bhw1_sunset2_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw1/tn/bhw1_sunset2.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 20 bytes of program space.
DEFINE_GRADIENT_PALETTE( bhw1_sunset2_gp ) {
0, 255,175, 8,
81, 237, 29, 10,
137, 148, 57, 42,
165, 68, 54, 54,
255, 18, 23, 29};
// Gradient palette "bhw2_sherbet2_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw2/tn/bhw2_sherbet2.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 32 bytes of program space.
DEFINE_GRADIENT_PALETTE( bhw2_sherbet2_gp ) {
0, 217, 1, 1,
35, 249, 43, 19,
71, 247,125,172,
109, 206, 2, 32,
163, 210, 23, 9,
211, 255,255,255,
232, 252,199, 88,
255, 206,115, 52};
// Gradient palette "bhw2_39_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw2/tn/bhw2_39.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 28 bytes of program space.
DEFINE_GRADIENT_PALETTE( bhw2_39_gp ) {
0, 2,184,188,
33, 56, 27,162,
66, 56, 27,162,
122, 255,255, 45,
150, 227, 65, 6,
201, 67, 13, 27,
255, 16, 1, 53};
// Gradient palette "bhw2_sunsetx_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw2/tn/bhw2_sunsetx.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 36 bytes of program space.
DEFINE_GRADIENT_PALETTE( bhw2_sunsetx_gp ) {
0, 42, 55,255,
25, 73,101,242,
89, 115,162,228,
107, 115,162,228,
114, 100, 77,201,
127, 86, 23,174,
142, 190, 32, 24,
171, 210,107, 42,
255, 232,229, 67};
// Gradient palette "bhw2_xc_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw2/tn/bhw2_xc.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 28 bytes of program space.
DEFINE_GRADIENT_PALETTE( bhw2_xc_gp ) {
0, 4, 2, 9,
58, 16, 0, 47,
122, 24, 0, 16,
158, 144, 9, 1,
183, 179, 45, 1,
219, 220,114, 2,
255, 234,237, 1};
// Gradient palette "bhw2_07_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw2/tn/bhw2_07.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 24 bytes of program space.
DEFINE_GRADIENT_PALETTE( bhw2_07_gp ) {
0, 92, 1, 1,
26, 153, 20, 5,
79, 232, 72, 12,
127, 220,231, 89,
173, 232, 72, 12,
255, 92, 1, 1};
// Gradient palette "bhw3_32_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/bhw/bhw3/tn/bhw3_32.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 52 bytes of program space.
DEFINE_GRADIENT_PALETTE( bhw3_32_gp ) {
0, 234,231, 1,
15, 171, 43, 6,
40, 121, 0, 0,
53, 95, 1, 29,
71, 73, 1,168,
94, 38, 63,221,
109, 115, 51,221,
127, 38, 63,221,
147, 73, 1,168,
181, 203, 28, 1,
193, 155, 16, 11,
216, 73, 1,168,
255, 1, 4, 29};
// Single array of defined cpt-city color palettes.
// This will let us programmatically choose one based on
// a number, rather than having to activate each explicitly
// by name every time.
// Since it is const, this array could also be moved
// into PROGMEM to save SRAM, but for simplicity of illustration
// we'll keep it in a regular SRAM array.
//
// This list of color palettes acts as a "playlist"; you can
// add or delete, or re-arrange as you wish.
const TProgmemRGBGradientPaletteRef gGradientPalettes[] = {
bhw3_32_gp,
bhw1_01_gp,
bhw1_07_gp,
bhw1_sunset3_gp,
bhw1_sunset2_gp,
bhw2_sherbet2_gp,
bhw2_39_gp,
bhw2_sunsetx_gp,
bhw2_xc_gp,
bhw2_07_gp,
};
// Count of how many cpt-city gradients are defined:
const uint8_t gGradientPaletteCount =
sizeof( gGradientPalettes) / sizeof( TProgmemRGBGradientPaletteRef );
void loop()
{
EVERY_N_SECONDS( SECONDS_PER_PALETTE ) {
gCurrentPaletteNumber = addmod8( gCurrentPaletteNumber, 1, gGradientPaletteCount);
gTargetPalette = gGradientPalettes[ gCurrentPaletteNumber ];
}
EVERY_N_MILLISECONDS(1500) {
nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 16);
}
colorwaves( leds, NUM_LEDS, gCurrentPalette);
FastLED.show();
//FastLED.delay(50);
}
Page last edited January 22, 2025
Text editor powered by tinymce.