I am porting working code from a UNO R4 WIFI to a nano and seem have a very odd problem. The value of the last ( and sometimes first) value of the array gets corrupted. That is, the second time I access the array the values are changed from the value originally set. In the code below I've stripped out almost everything to illustrate the problem but will reiterate: this code works perfectly on the UNO R4.
I thought the first nano was defective, tried another - same result. This is the first time I've worked with a nano so there maybe some idiosyncrasy I'm not expecting.
Any help would be appreciated.
int maxPow;
int pow2;
int minPin;
int maxPin = 8;
int currentVal;
int maxVal = 15;
int maxIndex = 3;
void setup() {
Serial.begin(9600);
maxPow = log(maxVal)/log(2);
minPin = maxPin - maxPow;
Serial.println("maxPow = " + String(maxPow, DEC) + " minPin = " + String(minPin, DEC));
delay(longPulse);
int i;
int j;
int k;
int pins[maxIndex];
//Create array and print it out immediately
for(j=0;j<=maxIndex;++j){
pins[j] = maxPin - j;
Serial.println("first time: pins[" + String(j, DEC) + "]: " + String(pins[j], DEC));
}
// Print it out again
for(k=0;k<=maxIndex;++k){
Serial.println("second time: pins[" + String(k, DEC) + "]: " + String(pins[k], DEC));
}
}
void loop() {
}
Should create a 4 element array with the numbers 8-5 descending, then print it out immediately as created and then in a second for loop.
Should be super simple.
This is the result from the nano:
first time: pins[0]: 8
first time: pins[1]: 7
first time: pins[2]: 6
first time: pins[3]: 5
second time: pins[0]: 8
second time: pins[1]: 7
second time: pins[2]: 6
second time: pins[3]: 520
Result from the UNO:
first time: pins[0]: 8
first time: pins[1]: 7
first time: pins[2]: 6
first time: pins[3]: 5
second time: pins[0]: 8
second time: pins[1]: 7
second time: pins[2]: 6
second time: pins[3]: 5
One more thing - when I change the creation of the array to a single line and then print it out the first for loop also fails:
int pins[maxIndex] = {8,7,6,3};
for(j=0;j<=maxIndex;++j){
//pins[j] = maxPin - j;
Serial.println("first time: pins[" + String(j, DEC) + "]: " + String(pins[j], DEC));
}
for(k=0;k<=maxIndex;++k){
Serial.println("second time: pins[" + String(k, DEC) + "]: " + String(pins[k], DEC));
}
produces
first time: pins[0]: 8
first time: pins[1]: 7
first time: pins[2]: 6
first time: pins[3]: 520
second time: pins[0]: 8
second time: pins[1]: 7
second time: pins[2]: 6
second time: pins[3]: 520
