Array variable name changes from readings1 to readings2
for (int j = 1; j &lt; sizeof(readings1)-1; j++) {



sizeof(readings1) is not the number of elements in the array, it is the size of the array in bytes (in your case, 40). You are exceeding the bounds of the array. You know the number of items (numberOfReadings) - why not use that?