I'm trying to reset the board based upon command send from the serial monitor. For example if I type reset in the monitor, then the board should reset. But I'm not able to do so.
This use of variable-type String is inside loop (which is never left like more sub-ordered functions.
This includes the danger of filling up all RAM with repeated assigning values to the String-type variable.
You should not use variable-type String in that way. Of course an ESP32 has more RAM than an Arduino-Uno. But it is just a matter time until the RAM-overwrite caused through repeated assigning new content to the string-variable happends.
You should use a library that is based on c_strings (which are arrays of char) like the SafeString-library. The SafeString-library offers (most of the comfort if variable-type String but is safe to use.
@the purists: If you want newcomers to use c_string directly without an additional library provide an easy to understand tutorial with direct coded functions that give all the comfort of SafeString.
Too lazy to provide this tutorial? Well I guess you have to accept the shift towards SafeString
#include <SafeString.h>
createSafeString(myDemo_SS, 32);
createSafeString(mySecondDemo_SS, 32);
unsigned long myCounter;
// if program starts printout the source-code filename etc. to the serial monitor
void PrintFileNameDateTime() {
Serial.println( F("Code running comes from file ") );
Serial.println(__FILE__);
Serial.print( F(" compiled ") );
Serial.print(__DATE__);
Serial.print( F(" ") );
Serial.println(__TIME__);
}
//useful function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - periodStartTime >= TimePeriod )
{
periodStartTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
unsigned long MyTestTimer = 0; // variables MUST be of type unsigned long
const byte OnBoard_LED = 13; // Arduino-Uno Onboard-LED is IO-pin 13
// make onboard-LED blink to show: "program is running"
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
digitalWrite(IO_Pin,!digitalRead(IO_Pin) );
}
}
void setup() {
Serial.begin(115200);
Serial.println( F("Setup-Start") );
PrintFileNameDateTime();
myCounter = 0;
myDemo_SS = "Hello world!";
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED,500);
myCounter++;
// loop is running very fast counting up very fast
// but only once every 1234 milliseconds print
if ( TimePeriodIsOver(MyTestTimer,1234) ) {
mySecondDemo_SS = myDemo_SS; // assigning a SafeString to another SafeString
mySecondDemo_SS += " "; // append a SPACE
mySecondDemo_SS += myCounter; // append integer-number
Serial.println(mySecondDemo_SS);
}
}
Most of the posts in the Arduino Forum are related to problems due to the abuse of delay(), another big chunk about of "strange things" done in an attempt not to use String...
I'm not a fan of String, but I'm quite sure this sketch will never have problems, especially considering that the variable is declared local as it should be and considering the 520 KB of SRAM of ESP32.
The necessary memory space will be allocated in the heap contiguously because there are no other statements that can cause fragmentation in between and, when there are no more characters in the serial buffer, temp is destroyed and memory released.
The declaring of the String is inside "void loop" which is never left
Does this mean that a variable that is declared inside a function
In this case it is function "loop"
that even if this function is never left the variable gets destroyed and re-allocated new with each iteration of loop?
void loop()
{
if (Serial.available()) {
String temp = Serial.readString(); //<<== the declaration does destroy **and** allocate new?
Serial.println(temp);