Hi Programmers,
Here I have a working sample sketch. I have a few arrays in PROGMEM, and I have an array in RAM that I want to compare against each of the PROGMEM arrays and see which one matches.
Can you help me understand is it possible to pass the name of a PROGMEM array to my compare function areSame, so I can move my while loop into the areSame function? I want to move the while loop that brings the PROGMEM array into the tempArray, so I am not repeating that code over and over for each compare. If I have a way to pass which PROGMEM array I want to compare to inside the function, I could even eliminate using the tempArray.
/*
Compare arrays
https://forum.arduino.cc/t/solved-comparing-whole-arrays/247786/17
Added PROGMEM storage of arrays
*/
const PROGMEM uint16_t pat11[] = {1, 2, 2, 25, 3, 1, 4, 14, 1, 4, 2, 20, 3, 2, 4, 14, 1, 4, 2, 20, 3, 2, 4, 100, 13, 0, 0, 0, 0, 0};
const PROGMEM uint16_t pat12[] = {1, 2, 2, 85, 3, 2, 4, 15, 1, 3, 2, 17, 3, 1, 4, 100, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
const PROGMEM uint16_t pat13[] = {1, 3, 2, 69, 3, 1, 4, 23, 1, 1, 2, 24, 3, 1, 4, 16, 1, 1, 2, 28, 3, 1, 4, 100, 13, 0, 0, 0, 0, 0};
const PROGMEM uint16_t pat14[] = {1, 2, 2, 23, 3, 1, 4, 20, 1, 1, 2, 72, 3, 1, 4, 20, 1, 1, 2, 20, 3, 1, 4, 100, 13, 0, 0, 0, 0, 0};
const PROGMEM uint16_t pat15[] = {1, 29, 2, 100, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
const PROGMEM uint16_t pat16[] = {1, 1, 2, 100, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
const PROGMEM uint16_t pat17[] = {1, 4, 2, 15, 3, 2, 4, 21, 1, 1, 2, 100, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
const PROGMEM uint16_t pat18[] = {1, 3, 2, 20, 3, 1, 4, 17, 1, 1, 2, 17, 3, 1, 4, 26, 1, 2, 2, 100, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0};
const PROGMEM uint16_t pat19[] = {1, 2, 2, 25, 3, 1, 4, 100, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned int mycode[30] = {1, 29, 2, 100, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned int tempArray[30];
void setup() {
Serial.begin(115200);
}
boolean areSame(int arrayA[], int arrayB[]) {
// compare arrays
boolean same = true;
int i = 0;
while (i < 30 && same && ! (arrayA[i] == 0 && arrayB[i] == 0)) {
same = arrayA[i] == arrayB[i];
i++;
}
return same;
}
void printFormattedArray(unsigned int arrayA[]) {
// print array elements up to the end or the first 0
Serial.print("{");
int i = 0;
while (i < 30) {
Serial.print(arrayA[i]);
i++;
if (i == 30)
Serial.println("}");
else
Serial.print(",");
}
}
void loop() {
Serial.println("begin");
Serial.println("model array");
printFormattedArray(mycode);
Serial.println("");
int i = 0;
while (i < 30) {
tempArray[i] = pgm_read_word_near(pat11 + i);
i++;
}
printFormattedArray(tempArray);
if (areSame(tempArray,mycode)) {
Serial.println("match=yes");
}
else {
Serial.println("match=no");
}
i = 0;
while (i < 30) {
tempArray[i] = pgm_read_word_near(pat12 + i);
i++;
}
printFormattedArray(tempArray);
if (areSame(tempArray,mycode)) {
Serial.println("match=yes");
}
else {
Serial.println("match=no");
}
i = 0;
while (i < 30) {
tempArray[i] = pgm_read_word_near(pat13 + i);
i++;
}
printFormattedArray(tempArray);
if (areSame(tempArray,mycode)) {
Serial.println("match=yes");
}
else {
Serial.println("match=no");
}
i = 0;
while (i < 30) {
tempArray[i] = pgm_read_word_near(pat14 + i);
i++;
}
printFormattedArray(tempArray);
if (areSame(tempArray,mycode)) {
Serial.println("match=yes");
}
else {
Serial.println("match=no");
}
i = 0;
while (i < 30) {
tempArray[i] = pgm_read_word_near(pat15 + i);
i++;
}
printFormattedArray(tempArray);
if (areSame(tempArray,mycode)) {
Serial.println("match=yes");
}
else {
Serial.println("match=no");
}
i = 0;
while (i < 30) {
tempArray[i] = pgm_read_word_near(pat16 + i);
i++;
}
printFormattedArray(tempArray);
if (areSame(tempArray,mycode)) {
Serial.println("match=yes");
}
else {
Serial.println("match=no");
}
i = 0;
while (i < 30) {
tempArray[i] = pgm_read_word_near(pat17 + i);
i++;
}
printFormattedArray(tempArray);
if (areSame(tempArray,mycode)) {
Serial.println("match=yes");
}
else {
Serial.println("match=no");
}
i = 0;
while (i < 30) {
tempArray[i] = pgm_read_word_near(pat18 + i);
i++;
}
printFormattedArray(tempArray);
if (areSame(tempArray,mycode)) {
Serial.println("match=yes");
}
else {
Serial.println("match=no");
}
i = 0;
while (i < 30) {
tempArray[i] = pgm_read_word_near(pat19 + i);
i++;
}
printFormattedArray(tempArray);
if (areSame(tempArray,mycode)) {
Serial.println("match=yes");
}
else {
Serial.println("match=no");
}
while (true) {}
}