Hi, I am using an arduino R4 wifi in order to display on the onboard led matrix messages that are sent via the iot messenger widget to a MatrixWrite() command.
The messages are Strings.
The sketch runs roughly smoothly but crashed from time to time, and I finally realized that the sketch crashes each time that I sent 3 point in a row (…). I have absolutely no clue why it is the case.
I know that using Strings is suboptimal for memory management, but I have no choice as this is the standard output of the messenger widget. I use the reserve() command to try to keep the memory as clean as possible.
Note that for every message received, an other string is created to include spaces before and after the message for proper display. I think this is the source of the issue, but I am still wondering why:
XXXMsgXXX=" ";
XXXMsgXXX+=MSG;
XXXMsgXXX+=" ";
Any idea on the source of these crashes?
See below the overall sketch:
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled 4"
https://create.arduino.cc/cloud/things/33b5abdd-5b4b-40e8-b57c-d5e911e30de9
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
String MSG;
int compteur;
bool syncLed;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"
#include "WiFiS3.h"
#define DEBUG 0 // set 1 for debugging; mremoves debuggibg code before compiling if not required
#if DEBUG ==1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)
#endif
unsigned long previousMillis = 0;
const unsigned long TextInterval = 2500; //definit le temps entre 2 messages (en millisecondes)
String XXXMsgXXX;
ArduinoLEDMatrix matrix;
const char BootText[] = " Booting... ";
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
MSG.reserve(91);
XXXMsgXXX.reserve(100);
matrix.begin();
initProperties(); // Defined in thingProperties.h
ArduinoCloud.begin(ArduinoIoTPreferredConnection); // Connect to Arduino IoT Cloud
XXXMsgXXX = String(BootText);
MatrixWrite(XXXMsgXXX);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
compteur = 0;
}
void loop() {
ArduinoCloud.update();
PrintText();
}
void PrintText() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > TextInterval)
{
MatrixWrite(XXXMsgXXX);
compteur++;
debug(F("compteur:"));
debugln(compteur);
previousMillis = millis();
digitalWrite(LED_BUILTIN, syncLed);
syncLed = 1 - syncLed;
}
}
void MatrixWrite(String &i)
{
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textScrollSpeed(25);
matrix.textFont(Font_5x7);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(i);
matrix.endText(SCROLL_LEFT);
matrix.endDraw();
}
void onMSGChange() {
debugln(MSG);
XXXMsgXXX=" ";
XXXMsgXXX+=MSG;
XXXMsgXXX+=" ";
debugln(XXXMsgXXX);
}
void onCompteurChange() {
{
}