I am having some trouble with extracting data from an array of codes, adding it to a string variable and then making a comparison to the correct code.
I define Check String and then after that loop through all the possible tags, make the result UPPERCASE and then print the result that I will try to make a comparison with later. Here is an excerpt, without the loop.
When I print CheckString, all I get is "TAG=3000". I do not understand why it is not adding the actual Tag value when it loops through the array. When I print a few of the array items manually Tag[0], Tag[1], Tag*, etc. It appears the array and loop are working just fine as I get the expected values when I test. * I am lost why I cannot add these to my CheckString. My syntax must be off or the UpperCase is causing me an issue. Any suggestions to try? THANKS!
Please read the sticky "How to use this forum - please read", specifically point 7 about posting code.
Note that it's not advisable to use String (capital S) if you have limited memory resources; it can result in unexpected behaviour. Use.nul-terminated character arrays instead.
If the tags are already in a char array, why do you want to convert to String (which is never a smart move)? Change to char CheckString[n] = "TAG=3000"; where 'n' is a value large enough to hold the initialized value plus whatever you may want to add later. You need 8+1 (null terminator) for "TAG=3000" so make it 15 or 20. Later, add the tag with strcat(CheckString, tag);
Thank you all for your time and responses. I had thought I made a simple error somewhere, but it sounds like it is more complex than that and has to do with my choice of array. Here is the draft of the sketch that I am working with as I understand this more has to do with the array/loop.
I am working with 24 HEX characters and then was adding the "TAG=3000" prefix that my inputString came with. I will read up on the difference between string and nul-terminated character arrays.
If you have any more suggestions, I appreciate it. I will read up on types of arrays more in the meantime. Thank you again.
// GLOBAL DECLARATIONS
String inputString = ""; // Holds incoming data
void setup()
{
// Setup Pins
Serial.begin(9600);
inputString.reserve(1000);
void validateTag()
{
String CheckString = "TAG=3000";
int i = 1; // Array Counter
// Parse Input
inputString.trim();
inputString = inputString.substring(inputString.length() - 32, inputString.length()); // Parse last 32 CHARS as inputString
if (inputString.startsWith("TAG=3000"))
{
// Build Tag ID Array
int ArraySize = 7;
String Tag[] = {
"9ecfa8e99409f099db5ddc03",
"0c1b69c80dfd36584ccf7b75",
"40dff033d211d2fe25f76d27",
"a0bb8ac8b8fa31136652c9d5",
"f4ccad4d3a73e7314aa96874",
"54eb069621328ea51958f7c0",
"5c832e37199511cb3841db43"
};
for (i = 0; i < ArraySize; i++){
CheckString += Tag[i];
CheckString.toUpperCase();
if (inputString == CheckString)
{
Serial.println("ARDUINO: YES, MATCH");
Serial.println(inputString);
}
else
{
// Invalid tag, access denied, do nothing
Serial.println("ARDUINO: NO MATCH FOR:");
Serial.println(inputString);
}
CheckString = "TAG=3000"; //Reset to start
}
inputString = ""; // Clear the inputSting
}
}
}