Skip to main content

Arduino - Displaying text (example)

This page contains a simple example with function documantation on how to display simple text with scrolling 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>

// We always wait a bit between updates of the display
#define DELAYTIME 300 // in milliseconds

// Define the number of devices we have in the chain and the hardware interface
#define HARDWARE_TYPE Led_Matrix::PAROLA_HW
#define MAX_DEVICES 1
#define CLK_PIN 18 // or SCK
#define DATA_PIN 23 // or MOSI
#define CS_PIN 4 // or LOAD

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

#define CHAR_SPACING 1 // Pixels between characters

// Global message buffers shared by Serial and Scrolling functions
#define BUF_SIZE 75
char message[BUF_SIZE] = "Hello!";
bool newMessageAvailable = true;

// Read a message from serial
void readSerial(void)
{
static uint8_t putIndex = 0;

while (Serial.available())
{
message[putIndex] = (char)Serial.read();
if ((message[putIndex] == '\n') || (putIndex >= BUF_SIZE - 3)) // End of message character or full buffer
{
// Put in a message separator and end the string
message[putIndex] = '\0';
// Restart the index for next filling spree and flag we have a message waiting
putIndex = 0;
newMessageAvailable = true;
}
else
// Just save the next char in next location
message[putIndex++];
}
}

void scrollText(const char *p)
{
uint8_t charWidth;
uint8_t cBuf[8]; // This should be ok for all built-in fonts

mx.clear();

// Go through the string until the end
while (*p != '\0')
{
charWidth = mx.getChar(*p++, sizeof(cBuf) / sizeof(cBuf[0]), cBuf);

for (uint8_t i = 0; i <= charWidth; i++) // Allow space between characters
{
mx.transform(Led_Matrix::TSL);
if (i < charWidth)
mx.setColumn(0, cBuf[i]);
delay(DELAYTIME);
}
}
}


void setup()
{
// Init matrix
mx.begin();

// Init serial communication
Serial.begin(115200);
}

void loop()
{
// Read a message from serial
readSerial();

// If there is a new message
if (newMessageAvailable)
{
// Print it on the matrices and Serial Monitor
scrollText(message);
newMessageAvailable = false;
}
}

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.clear()

Clears the buffer and all dislay data on the devices

Returns value: none

mx.begin()

Initialize the object.

Returns value: none

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_valuevalueEach bit set to 1 will light up the corresponding LED.

Full example

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

Led_Matrix_Test.ino

Example file for using most of the functions in the library.