Hi Guys, I'm working with a Maduino LoRa Radio I know you like people to post all the code but, mine is split between three files to keep it manageable. The code is getting long. Is this the right forum to post a lot of code? Should I post them all separately? I've tried isolating the problem but, when I do things work fine. Maybe I'm running out of memory or something?
Here is the file that triggers the freeze:
#include "pedestalInfo.h"
#include <EEPROM.h> // include EEPROM library
#include <stdlib.h> // might be needed for itoa()
String pedestalName;
unsigned int pedNameLenActual;
unsigned int pedNameLenMax = 9;
const int pedNameEepromAddr = 0;
int pedestalNameSize;
void writeToEeprom();
void pedestalInfo();
void pedestalInfo() {
// EEPROM CHECK
if (EEPROM.read(0) == 0) {
Serial.println("");
Serial.println("Enter a pedestal label up to 9 characters");
Serial.println("or hit enter to auto generate: ");
////////////////////////
//// FIRST WHILE / WHLE WHICH WORKS FINE
////////////////////////
while (Serial.available() == 0) { // wait for at least a carriage return
}
while (Serial.available() > 0) {
pedestalName = Serial.readString(); // if string entered, else should trigger
if (pedestalName == "\r\n") { // user simply hits enter on keyboard
char randomName[10];
itoa(millis(), randomName, 16); // https://fresh2refresh.com/c-programming/c-type-casting/c-itoa-function/
pedestalName = String(randomName);
}
else {
if (pedestalName.length() > pedNameLenMax) {
pedestalName.remove(pedNameLenMax, 100);
}
}
writeToEeprom();
}
}
else {
Serial.print("Current Pedestal Name is: ");
int newStrLen = EEPROM.read(pedNameEepromAddr);
char data[newStrLen + 1];
for (int i = 0; i < newStrLen; i++) {
data[i] = EEPROM.read(pedNameEepromAddr + 1 + i);
Serial.write(data[i]);
}
data[newStrLen] = '\0';
Serial.println(" ");
Serial.println("Type in a new name or just hit enter to keep the current:");
while (Serial.available() == 0) { // wait for at least a carriage return
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////// FREEZE BUG STARTS HERE: 2nd WHILE / WHILE, IT IS COMMENTED OUT
///////////////////////////////////////////////////////////////////////////////////////////////////
/*
while (Serial.available() > 0) {
pedestalName = Serial.readString(); // if string entered, else should trigger
if (pedestalName == "\r\n") {
Serial.println("Pedestal name staying the same");
}
else {
if (pedestalName.length() > pedNameLenMax) {
pedestalName.remove(pedNameLenMax, 100);
}
writeToEeprom(); // i'm commented this out, NOT THE PROBLEM
}
}
*/
////////////////////////////////////
///////// FREEZE BUG ENDS HERE
////////////////////////////////////
}
}
void writeToEeprom() { // doesn't appear to be the bug, I've tried commenting it out.
Serial.println("Remember: Pedestal Name Limited to 9 characters.");
Serial.print("The pedestal name is: ");
Serial.println(pedestalName);
Serial.print("The length of the pedestal name is = "); // this is debug
pedNameLenActual = pedestalName.length();
Serial.println(pedNameLenActual);
// prepare for EEPROM: convert pedestalName to char https://arduinogetstarted.com/reference/arduino-string-tochararray
byte pedestalNameAsChar[pedestalName.length() + 1];
pedestalName.toCharArray(pedestalNameAsChar, pedestalName.length() + 1);
for (int i = 0; i < pedestalName.length() + 1; i++) {
Serial.write(pedestalNameAsChar[i]); // debug: can't use Serial.print for ascii
}
Serial.println(" ");
pedestalNameSize = sizeof(pedestalNameAsChar); //get the size of the char array
Serial.print("sizeof in bytes = "); // debug
Serial.println(pedestalNameSize);
Serial.println("Lets get this new name written to the EEPROM");
Serial.println(" ");
// EEPROM : Writing pedestalNameAsChar, length first.
// https://roboticsbackend.com/arduino-write-string-in-eeprom/
// https://docs.arduino.cc/learn/built-in-libraries/eeprom
EEPROM.write(pedNameEepromAddr, pedestalNameSize);
for (int j = 0; j < pedestalNameSize + 1; j++) { // 1st byte contains the length
EEPROM.write(pedNameEepromAddr + 1 + j, pedestalNameAsChar[j]); // now write the name
}
}
Does anyone need to see the other files?
Thank you.

