When I attempt to increment an index that will be later used in an array, the index is reset. Below are some simple examples:
int i = 0;
int vector[] = {0, 0, 0};
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(i);
vector[i] = 0;
i++;
delay(500);
}
Output in Serial Monitor:
01234012340123401234012340123401234.......
If the array is not accessed by commenting out that line, the index counts as expected
int i = 0;
int vector[] = {0, 0, 0};
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(i);
// vector[i] = 0;
i++;
delay(500);
}
Output in Serial Monitor:
01234567891011121314151617181920212223242526272829303132333435......
Increasing the size of the declared array increases the value i will count to:
int i = 0;
int vector[] = {0, 0, 0, 0};
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(i);
vector[i] = 0;
i++;
delay(500);
}
Output in Serial Monitor:
012345012345012345012345012345012345012345012345012345.....
Is the compiler somehow trying to prevent writing to unallocated memory space?