For some reason the my array (codeList) is cleared after searching in the main loop
int codeList[] = {10,15,20,25,30};
int item = 10;
void setup() {
Serial.begin(9600);
for (int i; i < (sizeof(codeList)) / sizeof(int); i++){Serial.println(codeList[i]);}
}
void loop() {
// show all items in array
for (int i; i < (sizeof(codeList)) / sizeof(int); i++){Serial.println(codeList[i]);}
// search item in array
bool succes = false;
for (int i; i < (sizeof(codeList)) / sizeof(int); i++){
// if item is in list
if (item == codeList[i]){
succes = true;
}
}
//print succes ore not
Serial.print("succes: ");
if (succes){Serial.println("True");}
else{Serial.println("False");}
// delay to make promt readble
delay(2000);
}
as you will see in the image, in the setup and the first loop it works. but after that he just forgot the array, even if i use const int codeList.
No, the array is ok. The search is failing because of the error that @gcjr found. The loop variable i will not automatically default to zero, except by luck. It will begin at some unknown value, which will probably be greater than the size of the array, so the for-loop will not run even once. I worked the first time because of luck, perhaps some previously unused memory was used which contained zero.
Set your compiler warnings to "All" in preferences. you would have gotten these handy warnings:
sketch_jul27a.ino:8:12: warning: 'i' is used uninitialized in this function [-Wuninitialized]
for (int i; i < (sizeof(codeList)) / sizeof(int); i++)
^
sketch_jul27a.ino:17:12: warning: 'i' is used uninitialized in this function [-Wuninitialized]
for (int i; i < (sizeof(codeList)) / sizeof(int); i++)
^
sketch_jul27a.ino:24:12: warning: 'i' is used uninitialized in this function [-Wuninitialized]
for (int i; i < (sizeof(codeList)) / sizeof(int); i++)
^
You would have also gotten some warnings about comparing a signed value (int i) with an unsigned value (sizeof). Those are not a problem in your case but to get rid of the warnings, change "int i" to "size_t i". The type 'size_t' is the type that the 'sizeof' operator returns. Probably 'unsigned int'.