Skip to main content

Animations

What this example shows​

How to animate the whole grid by redrawing it frame by frame. The sketch runs two animations in a loop: a row sweep that lights one row at a time from top to bottom, and a rainbow that cycles every pixel through a colour wheel, each pixel a little further along than its neighbour.

Wire VCC to 5 V, GND to GND and DIN to pin 6 of your controller, or change PIN to the pin you used.

The example code​

This is Animations.ino from the pinned library, exactly as shipped.

Select Board
Animations.ino
/**
**************************************************
*
* @file Animations.ino
* @brief Demonstrates row sweep and colour-cycle animations on the
* Soldered 8x8 WS2812B LED grid.
*
* Connect the grid data line to PIN below.
*
* @authors Josip Šimun Kuči @ soldered.com
***************************************************/

#include "WS2812Grid-SOLDERED.h"

#define PIN 6

WS2812Grid grid(PIN);

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

// Simple HSV-to-RGB wheel: hue 0–255
static uint32_t colorWheel(uint8_t pos)
{
pos = 255 - pos;
if (pos < 85)
return WS2812Grid::Color(255 - pos * 3, 0, pos * 3);
if (pos < 170)
{
pos -= 85;
return WS2812Grid::Color(0, pos * 3, 255 - pos * 3);
}
pos -= 170;
return WS2812Grid::Color(pos * 3, 255 - pos * 3, 0);
}

// ---------------------------------------------------------------------------
// Animations
// ---------------------------------------------------------------------------

void rowSweep()
{
for (uint8_t y = 0; y < 8; y++)
{
grid.clear();
for (uint8_t x = 0; x < 8; x++)
{
grid.setPixel(x, y, 0, 180, 255);
}
grid.show();
delay(80);
}
}

void rainbowGrid(uint8_t cycles)
{
for (uint8_t c = 0; c < cycles; c++)
{
for (uint16_t hue = 0; hue < 256; hue++)
{
for (uint8_t y = 0; y < 8; y++)
{
for (uint8_t x = 0; x < 8; x++)
{
uint8_t offset = (x + y * 2) & 0xFF;
grid.setPixel(x, y, colorWheel((hue + offset) & 0xFF));
}
}
grid.show();
delay(10);
}
}
}

// ---------------------------------------------------------------------------
// Setup / loop
// ---------------------------------------------------------------------------

void setup()
{
grid.begin();
grid.setBrightness(40);
}

void loop()
{
rowSweep();
delay(200);
rainbowGrid(2);
delay(200);
}

Expected result: the sketch prints nothing to the Serial Monitor. On the grid, at brightness 40 out of 255, each row in turn lights in the colour (0, 180, 255) for 80 ms, from the top row to the bottom one. After a 200 ms pause, the rainbow steps through the 256 colours of the wheel twice, 10 ms per step, with each pixel offset along the wheel by its column plus twice its row. Another 200 ms pause, and the loop starts again.

Functions used​

WS2812Grid(uint8_t pin, uint8_t width = 8, uint8_t height = 8)returns WS2812Grid

Creates the grid driver for width x height LEDs on the pin wired to DIN. One grid is 8 x 8, the default. For several chained grids, pass the panel's full width and height.

ReturnsThe grid object.

Parameters

TypeNameDescription
uint8_tpinArduino pin connected to the grid's DIN.
uint8_twidthNumber of columns, a multiple of 8. Default 8.
uint8_theightNumber of rows, a multiple of 8. Default 8.
begin()returns void

Initialises the LED driver, setting the data pin as an output, then clears every pixel and calls show(), so the grid starts dark. Call it once in setup().

ReturnsNothing.

setBrightness(uint8_t b)returns void

Sets the output brightness for all LEDs. The change shows at the next show(). It is meant to be set once in setup(): the library scales the stored colours, so changing it again and again loses colour precision.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_tbBrightness, 0 (off) to 255 (brightest).
clear()returns void

Sets every pixel to off in memory. The LEDs go dark at the next show(). The row sweep uses it to wipe the previous row before drawing the next.

ReturnsNothing.

setPixel(uint8_t x, uint8_t y, uint8_t r, uint8_t g, uint8_t b)returns void

Sets one LED by its grid position to separate red, green and blue values. A position outside the grid is ignored. Call show() afterwards to send the change to the LEDs.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_txColumn, 0 = left.
uint8_tyRow, 0 = top.
uint8_trRed, 0 to 255.
uint8_tgGreen, 0 to 255.
uint8_tbBlue, 0 to 255.
setPixel(uint8_t x, uint8_t y, uint32_t color)returns void

Sets one LED by its grid position to a packed colour. The rainbow passes the value colorWheel() builds with WS2812Grid::Color(). A position outside the grid is ignored. Call show() afterwards to send the change to the LEDs.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_txColumn, 0 = left.
uint8_tyRow, 0 = top.
uint32_tcolorPacked colour, 0x00RRGGBB.
WS2812Grid::Color(uint8_t r, uint8_t g, uint8_t b)returns uint32_t

A static helper that packs red, green and blue into one 32-bit colour value. The sketch's colorWheel() helper uses it to turn a position on the wheel into a colour.

ReturnsThe packed colour, (r << 16) | (g << 8) | b.

Parameters

TypeNameDescription
uint8_trRed, 0 to 255.
uint8_tgGreen, 0 to 255.
uint8_tbBlue, 0 to 255.
show()returns void

Sends the colours held in memory to the LEDs. On most boards it briefly turns interrupts off to keep the signal timing, so millis() and micros() lose about 30 µs per LED each time it runs.

ReturnsNothing.

Putting it together​

Each frame is drawn the same way: change the pixels in memory with clear() and setPixel(), then call show() once and wait before the next frame. The delay() after each show() sets the speed, so change the 80 and 10 to make the row sweep or the rainbow faster or slower. colorWheel() and the two animation functions are part of the sketch, not the library.