Hello all,
I am having another issue where the Arduino locks up after executing a piece of code. The locking up occurs right after the section posted here on a Serial.println function call. I am not sure what could be causing this issue and have been running around in circles for the last few hours here. Could someone take a look at the function that I have posted here and see if there is anything glaring obvious that is an issue? Please let me know. Best
Note: I know that I should not use String but there were a few instances where I needed something easier to manipulate then a c string. I have been actively trying to remove String from my source. Also I am using std::vector because I found in some applications that it is much easier to utilize a dynamic array. IN terms of performance, I am only using the code if necessary
LEDStrip StripArray[11];
std::vector<RoomObject> RoomArray;
bool firstRun = true;
File testFile;
String Temp = "Room 1 Strip 1 LED 1-143 Group 1 ; Room 2 Strip 1,2 LED 144,1 Group 2 ; Room 3 Strip 2 LED 2-144 Group 3";
//String Temp = "Room:1;Strip:1;LED:1-143 Room:2;Strip:1,2;LED:144,1 Room:3;Strip:2;LED:2-144";
// Global variables for the Ethernet portion
byte mac[] = {0x00, 0xBA, 0xDA, 0xDF, 0xAC, 0x02};
unsigned int localPort = 8888;
IPAddress ip(192, 168, 1, 178);
EthernetUDP udp;
void ParseSDCardFile(String fileString);
void setup() {
String fileString = "";
Serial.begin(9600);
Serial.println("-------- New Program Running --------");
Ethernet.init(Ethernet_CS_PIN);
Ethernet.begin(mac, ip);
if(Ethernet.hardwareStatus() == EthernetNoHardware)
Serial.println("Ethernet Hardware not connected");
else
Serial.println("Ethernet connected");
if(Ethernet.linkStatus() == LinkOFF)
Serial.println("No cable connected");
else
Serial.println("Link status up and communicating");
// Open file of SD Card file logic here and read into a string
if(!SD.begin(CS_PIN))
{
Serial.println("SD port failed to init");
}
else
Serial.println("initilization complete");
testFile = SD.open("config.ini");
if(testFile)
{
while(testFile.available())
{
char readValue = testFile.read();
fileString.concat(readValue);// Future update. Could rewrite this section where it reads one line at a time and process the infomation, then reads a second and repeats instead of reading the entire thing first
}
testFile.close();
}
else
{
fileString = Temp;
Serial.println("Error reading file");
}
ParseSDCardFile(fileString);
//ParseSDCardFileOld(fileString);
Serial.println("Parsed SD CARD file"); // freeze up occurs here
}
void ParseSDCardFile(String fileString)
{
Serial.println("Parsing file");
char *outerPtr = nullptr;
char *inner1ptr = nullptr;
char *inner2ptr = nullptr;
char *RoomToken = strtok_r(fileString.c_str(), " ", &outerPtr);
uint16_t RoomCount = 1;
bool ParsingComplete = false;
RoomObject *NewRoom = new RoomObject;
while(RoomToken != NULL)
{
if(strcmp(RoomToken, "Room"))
{
RoomToken = strtok_r(NULL, " ", &outerPtr);
long tempValue(std::strtol(RoomToken, nullptr, 10));
uint8_t finalValue(0);
finalValue = static_cast<uint8_t>(tempValue);
NewRoom->SetRoomID(finalValue);
RoomCount++;
}
else if(strcmp(RoomToken, "Strip"))
{
RoomToken = strtok_r(NULL, " ", &outerPtr);
std::vector<uint8_t> LEDStripNumber;
if(strchr(RoomToken, ','))
{
// There are 2 strips here
char *secondLevelToken = strtok_r(RoomToken, ",", &inner1ptr);
while(secondLevelToken != NULL)
{
long tempValue(std::strtol(secondLevelToken, nullptr, 10));
uint8_t finalValue(0);
finalValue = static_cast<uint8_t>(tempValue);
LEDStripNumber.push_back(finalValue);
secondLevelToken = strtok_r(NULL, ",", &inner1ptr);
}
}
else
{
long tempValue(std::strtol(RoomToken, nullptr, 10));
uint8_t finalValue(0);
finalValue = static_cast<uint8_t>(tempValue);
LEDStripNumber.push_back(finalValue);
}
inner1ptr = nullptr;
NewRoom->setLEDStripNumberReference(LEDStripNumber);
}
else if(strcmp(RoomToken, "LED"))
{
RoomToken = strtok_r(NULL, " ", &outerPtr);
std::vector<uint8_t> LEDnumberCount;
if(strchr(RoomToken, ','))
{
char *secondLevelToken = strtok_r(RoomToken, ",", &inner1ptr);
while(secondLevelToken != NULL)
{
long tempValue(std::strtol(secondLevelToken, nullptr, 10));
uint8_t finalValue(0);
finalValue = static_cast<uint8_t>(tempValue);
LEDnumberCount.push_back(finalValue);
secondLevelToken = strtok_r(NULL, ",", &inner1ptr);
}
}
else if(strchr(RoomToken, '-'))
{
uint8_t lowerValue = 0;
uint8_t higherValue = 0;
char *secondLevelToken = strtok_r(RoomToken, "-", &inner1ptr);
while(secondLevelToken != NULL)
{
long tempValue(std::strtol(secondLevelToken, nullptr, 10));
uint8_t finalValue(0);
finalValue = static_cast<uint8_t>(tempValue);
if(lowerValue == 0)
lowerValue = finalValue;
else if(higherValue == 0)
higherValue = finalValue;
secondLevelToken = strtok_r(NULL, ",", &inner1ptr);
}
for(int i = lowerValue; i < higherValue + 1; i++)
{
LEDnumberCount.push_back(i);
}
}
NewRoom->setLEDNumberArray(LEDnumberCount);
inner1ptr = nullptr;
}
else if(strcmp(RoomToken, "Group"))
{
RoomToken = strtok_r(NULL, " ", &outerPtr);
long tempValue(std::strtol(RoomToken, nullptr, 10));
uint8_t finalValue(0);
finalValue = static_cast<uint8_t>(tempValue);
NewRoom->setGroupID(finalValue);
}
else if(strcmp(RoomToken, ";"))
{
RoomArray.push_back(*NewRoom);
NewRoom = new RoomObject;
inner1ptr = nullptr;
inner2ptr = nullptr;
}
RoomToken = strtok_r(NULL, " ", &outerPtr);
}
}