Hi guys, i'm dumbfounded here as the code is not doing what I expected....so basically I have an array of struct that holds a list of corresponding numbers related to Strings. Like shown below, this is declared before the Setup():
#define maxMenuItems 44 //total number of entries in array
typedef struct
{
unsigned int code;
String text;
} menu_type;
menu_type menu[maxMenuItems];
after the above array is declared, it is populated somewhere during Setup(), I then used a method to look for a number from the array that is the closest number smaller or equal to the one specified, the code is such:
unsigned int menuVerifyPos(unsigned int menuCode) {
unsigned int closestCode;
for (unsigned int k = 0; k < (maxMenuItems - 1); k++) {
if (menuCode == menu[k].code) { //found exact code, returns it
closestCode = menu[k].code;
} else if (menu[k].code < menuCode) { //if the code being checked is bigger than in the menu, potentially the closest
if (closestCode < menu[k].code) { //if the code being checked is smaller than the code in the menu
closestCode = menu[k].code; //this code from the menu is so far the next closest and smaller than the code being checked
}
}
}
Serial.print(menuCode);
Serial.print(" -> ");
Serial.println(closestCode);
return closestCode;
}
However, this method is not returning at all the numbers I expect, two issues I'm noticing:
(1) With the same parameter passed to the method, each time it would return me a different number. Seems entirely random.
(2) In the for loop that loops through the array, if I use sizesof(menu) as the loop-stop-condition, it would go on much longer and gave me results beyond what the variable maxMenuItems had specified as the size of the array. (the first 44 entries are what I had populated it with, but it would show results after that with random numbers).
I must be missing something entirely, what is wrong with my code??
Thanks in advance for looking at this.
Oscar
sizeof (menu) gives you the number of bytes for the number of elements you want:
(sizeof(menu)/sizeof(menu[0])
Since this is calculated at compiletime, there is no performance penalty for using a division.
AWOL:
It's incomplete
There you go:
#define maxMenuItems 44 //total number of entries in array
unsigned int menuPos = 0; //current menu position
typedef struct
{
unsigned int code;
String text;
} menu_type;
menu_type menu[maxMenuItems];
void setup() {
menu[0].code = 0;
menu[0].text = "EXIT MENU";
menu[1].code = 1;
menu[1].text = "MENU 1";
menu[2].code = 2;
menu[2].text = "MENU 2";
menuVerifyPos (4);
}
void loop() {
}
unsigned int menuVerifyPos(unsigned int menuCode) {
//accepts a code that represents position in the menu
//checks against the menu, verify it exist, and returns it
//if the menu code given does not exist,
//returns the closest code smaller than the one given
unsigned int closestCode;
for (unsigned int k = 0; k < (maxMenuItems - 1); k++) {
if (menuCode == menu[k].code) { //found exact code, returns it
closestCode = menu[k].code;
} else if (menu[k].code < menuCode) { //if the code being checked is bigger than in the menu, potentially the closest
if (closestCode < menu[k].code) { //if the code being checked is smaller than the code in the menu
closestCode = menu[k].code; //this code from the menu is so far the next closest and smaller than the code being checked
}
}
}
Serial.print(menuCode);
Serial.print(" -> ");
Serial.println(closestCode);
return closestCode;
}
In the simplified example above, I loaded the menu array with 3 sets of entries. and then called the method menuVerifyPos with parameter of 4.
the menuVerifyPos(4) is supposed to return 2 because that is the closest number equal to or smaller than 4 thats held in menu[k].code But that is not what its happening.
The method seems to be return arbitrary numbers instead somehow.
For example i would get in serial monitor something like: 4 -> 8
and if i run it again i would get yet a different number...
and I thought the code would return 4 -> 2
You logic is wrong. If I type in 4, your first if blocks checks to see if it equals 4. If not, the next if block asks if it is less than 4. Since it is 1, the test is true, but gives the wrong result.
econjack:
You logic is wrong. If I type in 4, your first if blocks checks to see if it equals 4. If not, the next if block asks if it is less than 4. Since it is 1, the test is true, but gives the wrong result.
the logic test is wrapped in a loop, it should go through the array and always record a larger number that's less than 4?
Where do you initialise closestCode to a known starting value?
arduarn:
Where do you initialise closestCode to a known starting value?
right at beginning of the method
unsigned int closestCode;
airoscar:
right at beginning of the method
unsigned int closestCode;
To a known starting value?
arduarn:
To a known starting value?
Good catch man, yep that was the bug thats causing all sort of weird behaviour from the array. Thanks again!