I am trying to create a few methods which allow my program to detect whether it has received a "Transaction" multiples times, and if so avoid processing it again.
I have tried using both char pointers and String arrays and I still havent been able to get this to compile and run correctly
Any help is very much appreciated
//Transaction history/memory setup
const int tranHistoryLength = 10;
String tranHistory[tranHistoryLength] = {};
int tranHistIndex = 0;
void initTranHistory(){
for (int i = 0 ; i < tranHistoryLength; i++){
tranHistory[i] = String();
}
}
void addToTranHistory(String TID){
tranHistory[tranHistIndex++] = TID;
tranHistIndex %= tranHistoryLength;
}
boolean tranHistoryContains(String TID){
for (int i = 0 ; i < tranHistoryLength; i++){
if (TID == tranHistory[i])
return true;
}
return false;
}
void setup(){
initTranHistory();
Serial.println("adding to hist");
delay(100);
addToTranHistory("HI");
addToTranHistory("HO");
delay(100);
Serial.println("HO in history: " + String(tranHistoryContains("HO")));
delay(100);
Serial.println("done");
}
void loop(){}
using a global you dont need to use pointers within the sketch as a global variable is accessible to all functions within the sketch.
i dont think thats a valid string constructor your using in the init function, string constructors
and the casting of the return from tranHistoryContains is redundant, try spliting the output up using a few Serial.print statements ending with a Serial.println to new line it
Using Strings (capital S) in an Arduino can cause memory problems because it has so little SRAM. Much better to use strings (small s) which are null terminated arrays of char.
pYro_65:
If you are referring to * *tranHistory[i] = String();* *
, its using the copy constructor to clear the string.
That was my query yes, my limited reading led me to believe the String() constructor required an argument,
I qualified my doubt with the 'i think' But thanks for picking up on it and correcting.. seems like if you want to initialise its a good idea to do when declaring, but its a class not a variable.. getting there....
@PaulS, yours isn't any more 'proper' really, the String class has multiple overloads of the assignment operator, one accepts a String object and one accepts a char*.
I was tired and mentioned copy constructor, however both yours and the original example use the assignment operator. And both RHS values are effectively empty strings.