Arduino R4 Wifi LED Binary display

So, I noticed there wasn't a "built-in" function for displaying text on the R4 wifi. I decided to make my own sketch that could reliably display up to twelve characters in binary form via the 8x12 LED matrix.

In this example sketch you can send up to 128 characters through the serial monitor and it will display them via scrolling.

Fair warning - this isn't the most optimal way of doing things and there is a very real risk of fragmenting the heap.

#include "Arduino_LED_Matrix.h"
ArduinoLEDMatrix matrix;
const int bufferSize = 128;

byte frame[8][12] = {
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};


//Takes a char array, slices the char at postion zero and shifts
//the entire array to the right so that position one becomes zero
void removeFirstChar(char* charArray) {
    int length = strlen(charArray);
    
    if (length > 0) {
        for (int i = 0; i < length; i++) {
            charArray[i] = charArray[i + 1];
        }
        charArray[length - 1] = '\0'; // Null-terminate the string
    }
}
//==================================================================

//This is a char array that is used for display purposes.
char str[bufferSize];


//Takes the str array and converts the individual chars into binary
//If the array is 12 or less it will display everything in a single frame
//If it is longer than 12, it will display the first 12 chars of array and
//call the removeFirstChar() function
void translate(char snip[]){
if (strlen(snip) <= 12){
    for (int i=0;i<strlen(snip);i++)  // loop through all chars in string
      {
        for (int j=7;j>=0;j--) // loop from bit-7 down to bit-0 (high-bit to low-bit)
          //from str[i] read the bit-j and print it to matrix
          frame[j][i] = bitRead(snip[i],j);
      }
    }
else{
    for (int i=0;i<12;i++)  // loop through all chars in string
    {
      for (int j=7;j>=0;j--) // loop from bit-7 down to bit-0 (high-bit to low-bit)
        //from str[i] read the bit-j and print it to matrix
        frame[j][i] = bitRead(snip[i],j);
    }
    removeFirstChar(str);
  }
}
//=====================================================================================

void clearFrame() {
  for (int row = 0; row < 8; row++) {
    for (int col = 0; col < 12; col++) {
      frame[row][col] = 0;  // Set each element to zero
    }
  }
  matrix.renderBitmap(frame, 8, 12);
}

void cleanCharArray() {
  for (int i = 0; i < bufferSize; i++) {
    str[i] = '\0';  // Overwrite with null characters
  }
}


String serialString;

void setup() {
  // Starts the matrix and displays an empty matrix
  matrix.begin();
  matrix.renderBitmap(frame, 8, 12);
  delay(1000);
  Serial.begin(9600);
}

void loop() {



if (Serial.available()){
  cleanCharArray();
  clearFrame();
  delay(100);
  serialString = Serial.readString();
  serialString.remove(serialString.length()-1); // Needed to remove excess character
  serialString.toCharArray(str, bufferSize);
}

  
 



  //Serial outputs str array and length
  Serial.println(str);
  Serial.println(strlen(str));
  //translate() is used to display the binary portion
  translate(str);
  //Serial outputs str array and length after translate()
  Serial.println(str);
  Serial.println(strlen(str));
  //Displays binary on led matrix
  matrix.renderBitmap(frame, 8, 12);
  delay(1000);
}

Why the need to remove the "excess" character ?
Doing so means that if a single character is entered then nothing is displayed and if more than one character is entered the first one is removed

I'm so glad you asked!

There are actually two such cases where an "excess" character is removed for different purposes.

The first char is removed from a string entered via the serial console. In my personal experience, if the char was not removed at the end of the string, the matrix would display an EOT character with the code 00000100.

The second instance is with characters above 12. Let's say the string is 13 characters. One character is removed at the start and the char array is shifted to the right. This only happens after the characters are displayed initially, so that you don't lose out on that first character.

Try entering a single character. Is its binary value displayed on the matrix ?

Try entering two characters. How many binary values are displayed on the matrix ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.