Skip to main content

Arduino - Displaying animation (Pacman example)

This page contains a simple example with function documentation on how to display simple animation.


Initialization

To use the matrix, first, include the required library, create the sensor object and initialize the sensor in the setup() function. You can use the return of begin() to check if everything is connected correctly:


#include "Led-Matrix-SOLDERED.h"
#include <SPI.h>


#define HARDWARE_TYPE Led_Matrix::PAROLA_HW
#define MAX_DEVICES 3
#define CLK_PIN 18 // or SCK
#define DATA_PIN 23 // or MOSI
#define CS_PIN 4 // or LOAD

Led_Matrix mx = Led_Matrix(HARDWARE_TYPE, CS_PIN, MAX_DEVICES); // SPI hardware interface

// --------------------
// Constant parameters
//
#define ANIMATION_DELAY 75 // Milliseconds
#define MAX_FRAMES 4 // Number of animation frames

// ========== General Variables ===========
//
const uint8_t pacman[MAX_FRAMES][18] = // Pacman pursued by a ghost
{
{0xfe, 0x73, 0xfb, 0x7f, 0xf3, 0x7b, 0xfe, 0x00, 0x00, 0x00, 0x3c, 0x7e, 0x7e, 0xff, 0xe7, 0xc3, 0x81, 0x00},
{0xfe, 0x7b, 0xf3, 0x7f, 0xfb, 0x73, 0xfe, 0x00, 0x00, 0x00, 0x3c, 0x7e, 0xff, 0xff, 0xe7, 0xe7, 0x42, 0x00},
{0xfe, 0x73, 0xfb, 0x7f, 0xf3, 0x7b, 0xfe, 0x00, 0x00, 0x00, 0x3c, 0x7e, 0xff, 0xff, 0xff, 0xe7, 0x66, 0x24},
{0xfe, 0x7b, 0xf3, 0x7f, 0xf3, 0x7b, 0xfe, 0x00, 0x00, 0x00, 0x3c, 0x7e, 0xff, 0xff, 0xff, 0xff, 0x7e, 0x3c},
};
const uint8_t DATA_WIDTH = (sizeof(pacman[0]) / sizeof(pacman[0][0]));

uint32_t prevTimeAnim = 0; // Remember the millis() value in animations
int16_t idx; // Display index (column)
uint8_t frame; // Current animation frame
uint8_t deltaFrame; // The animation frame offset for the next frame

// ========== Control routines ===========
//
void resetMatrix(void)
{
mx.control(Led_Matrix::INTENSITY, MAX_INTENSITY / 2);
mx.control(Led_Matrix::UPDATE, Led_Matrix::ON);
mx.clear();
}

void setup()
{
mx.begin(); // Init matrix
resetMatrix();
prevTimeAnim = millis(); // Remember the last animation time

// Init serial communication if it's needed

}

void loop(void)
{
static boolean bInit = true; // Initialise the animation

// Is it time to animate?
if (millis() - prevTimeAnim < ANIMATION_DELAY)
return;
prevTimeAnim = millis(); // Starting point for next time

mx.control(Led_Matrix::UPDATE, Led_Matrix::OFF);

// Initialize
if (bInit)
{
mx.clear();
idx = -DATA_WIDTH;
frame = 0;
deltaFrame = 1;
bInit = false;

// Lay out the dots
for (uint8_t i = 0; i < MAX_DEVICES; i++)
{
mx.setPoint(3, (i * COL_SIZE) + 3, true);
mx.setPoint(4, (i * COL_SIZE) + 3, true);
mx.setPoint(3, (i * COL_SIZE) + 4, true);
mx.setPoint(4, (i * COL_SIZE) + 4, true);
}
}

// Clear old graphic
for (uint8_t i = 0; i < DATA_WIDTH; i++)
mx.setColumn(idx - DATA_WIDTH + i, 0);
// Move reference column and draw new graphic
idx++;
for (uint8_t i = 0; i < DATA_WIDTH; i++)
mx.setColumn(idx - DATA_WIDTH + i, pacman[frame][i]);

// Advance the animation frame
frame += deltaFrame;
if (frame == 0 || frame == MAX_FRAMES - 1)
deltaFrame = -deltaFrame;

// Check if we are completed and set initialise for next time around
bInit = (idx == mx.getColumnCount() + DATA_WIDTH);

mx.control(Led_Matrix::UPDATE, Led_Matrix::ON);

return;
}

Led_Matrix mx = Led_Matrix()

Creates matrix object

Returns value: none

Function parameters:

TypeNameDescription
moduleType_tmodSets the mode in which module will be working
uint8_tcsPinDigital pin number for data communication
uint8_tnumDevicesSets the number of daisy chained modules

mx.control()

Set the control status of the specified parameter for all devices.

Returns value: none

Function parameters:

TypeNameDescription
ControlRequest_tmodeOne of the defined control requests.
uint8_tcsPinDigital pin number for data communication
intvaluesParameter value or one of the control status defined.

mx.clear()

Clears the buffer and all dislay data on the devices

Returns value: none

mx.begin()

Initialize the object.

Returns value: none

mx.setPoint()

Set the status of a single LED, addressed as a pixel.

Returns value: Returns bool value, false if parameter errors, true otherwise.

Function parameters:

TypeNameDescription
uint16_trRow coordinate for the point [0..ROW_SIZE-1].
uint16_tcColumn coordinate for the point [0..getColumnCount()-1]
boolstateSets the state of pixel.

mx.getChar()

Load a character from the font data into a user buffer.

Returns value: Width (in columns) of the character, 0 if parameter error

Function parameters:

TypeNameDescription
uint16_tcThe character to retrieve.
uint8_tsizeThe size of the user buffer in unit8_t units
uint8_t*buf*Address of the user buffer supplied

mx.transform()

Apply a transformation to the data in contiguous subset of devices.

Returns value: Returns bool value, fales if parameter errors, true otherwise

Function parameters:

TypeNameDescription
transformType_tttypeThe character to retrieve.

mx.setColumn()

Set all LEDs in a specific column to a new state.

Returns value: Returns bool value, fales if parameter errors, true otherwise

Function parameters:

TypeNameDescription
uint8_tcColumn which is to be set [0..getColumnCount()-1].
uint8_valuevaleEach bit set to 1 will light up the corresponding LED.

mx.getColumnCount()

Gets the maximum number of columns for devices attached to this class instance.

Returns value: Returns uint16_t representing the number of columns.


Full example

Try all of the above mentioned functions and more in this full example which detects presence of magnetic object.

Led_Matrix_Pacman.ino

Example file for using the library to display a Pacman animation.