Do not want serial data concatenating while sending to 8x8 led matrix

My code works and I know where the “problem” lies. So I send characters using the serial monitor and they display in the matrix. It keeps looping the characters, which is what I want. But when I send new characters with the monitor it just keeps the old characters and adds the new data to the end of the variable. I can’t seem to figure out a way to clear the variable “content” but clear it only before new data is received. The while is where things happen. I was trying to think of a way to use a “needsUpdate” boolean and use it to trigger an event to clear “content” .

Here is what I have.

/*
 ****************************************
 * Send serial data and display in matrix.
 * Update matrix when new serial data.
 ****************************************
 */

#include <Wire.h>

#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();

int x, srlLen, srtLenNum;
String content = "";   
char characters; 
boolean needsUpdate = false;


void setup() {
  matrix.setRotation(1);
  matrix.begin(0x70);  // Pass in the address
  Serial.begin(9600);  // Initialize serial port
}

void loop() {
  matrix.setBrightness(1);  // 1 = dimmest, 15 = brightest

  while(Serial.available()) {  
    characters = Serial.read();       
    content.concat(characters);
    delay (10); 
    needsUpdate = true; 
  }   

  if (content != "") {
    content.trim(); // trim what was sent
    matrix.clear();
    matrix.setTextWrap(false);  
    matrix.setTextColor(LED_ON);

    srlLen = content.length();
    srtLenNum = srlLen*8*-1; // Allow the matrix to display the full lenght of content
    
    // Scroll caracters on matrix
    for (int8_t x=8; x>= srtLenNum; x--) {
      matrix.clear();
      matrix.setCursor(x,0);
      matrix.print(content);
      matrix.writeDisplay();
      delay(77);
    }  
  }
}

Maybe add a content check between these 2 lines: characters = Serial.read(); content.concat(characters); delay (10);

If "clear" character is sent, then do your other action. What's the delay for?

Since you're using the String class, I think you need to clear it out when you're done with it:

void loop()
{
// your code...
    for (int8_t x=8; x>= srtLenNum; x--) {
      matrix.clear();
      matrix.setCursor(x,0);
      matrix.print(content);
      matrix.writeDisplay();
      delay(77);
    }  
    content = "";      // Clear out the old data
  }
}

Personally, I would rewrite this without using the String class and just use a char array instead of content. I also agree with CrossRoads...why the delay() and why is it 77. That magic number just begs for a comment.

But when I send new characters with the monitor it just keeps the old characters ...

Here is the answer to your problem. You know the difference between new characters and old characters but your program does not. Use a line terminator (carriage return or line feed) to let your program know that the input is complete and ready to be displayed. When you see a line terminator, display the contents of your input buffer and then clear the buffer in preparation for the next input.

econjack:
Personally, I would rewrite this without using the String class and just use a char array instead of content. I also agree with CrossRoads…why the delay() and why is it 77. That magic number just begs for a comment.

This is the speed the characters scroll on the matrix. I was testing speeds for readability i.e. 22,77,99,111. 33ms was way too fast and 111ms was too slow. Mystery solved. :stuck_out_tongue: