int array not working

Hello,

I’m making a simple proyect in wich I need to measure two voltages. I’m using analog pins 0 and 1 on an Arduino Uno board for this purpose. To increase the reliability of this measurments, I take a number of samples and store them into arrays for then computing the mean value and the variance.
I’m having two problems:

  1. I don’t know why but the last sample of each of the arrays is always wrong. Independently of the lenghts of these arrays and the velocity at which the samples are taken, the values that are stored are like this: 500 501 500 504 495 3080. All the N-1 values are ok but the last one. The way I “solved” this was not using the last value to compute the mean and variance.
  2. If the line that says “// Al descomentar esta linea se pudre todo…” is comented, the samples of the array “mediciones_VTEMP” are ok (except for the last value). When this line is uncommented, then the samples taken in “mediciones_VTEMP” increase significanty their disperssion. For example:
    mediciones_VTEMP = 100 104 99 102 98 399 → this is with the line commented
    mediciones_VTEMP = 100 140 82 137 78 590 → this is with the line uncommented

Ideas?

Code:

#define PIN_TERMOMETRO A0
#define PIN_VREF A1
#define LENGTH 10
#define PARLANTITO 8
#define PENDIENTE 1.87
#define ORDENADA  481
#define VAR_ALARM 1

  unsigned int mediciones_VTEMP[LENGTH-1], mediciones_VREF[LENGTH-1];
  float var_VTEMP, mean_VTEMP, temp, var_VREF, mean_VREF;
  int i;
  char serial_temp[4]; // Aca almacenamos la temperatura enviada por el puerto serie

void setup ()
{
  pinMode(PIN_TERMOMETRO, INPUT);
  pinMode(PIN_VREF, INPUT);
  Serial.begin(9600);
}

void loop ()
{

  tone (PARLANTITO, 440, 100);
  while (Serial.available() == 0) {;}
  leerCadena('\n', serial_temp, 3);

  for (i=0; i<LENGTH; i++) {
    mediciones_VTEMP[i] = analogRead(PIN_TERMOMETRO);
    delay(1);
  }

  for (i=0; i<LENGTH; i++) {
//    mediciones_VREF[i] = 0; // Al descomentar esta linea se pudre todo...
    delay(1);
  }
  
  mean_VTEMP = 0;
  mean_VREF = 0;
  for (i=0; i<LENGTH-1; i++) { // Valla Dios a saber por que pero la ultima posicion esta corrupta asi que no se puede usar...
    mean_VTEMP += mediciones_VTEMP[i];
    mean_VREF += mediciones_VREF[i];
  }
  mean_VTEMP /= LENGTH-1;
  mean_VREF /= LENGTH-1;
  
  var_VTEMP = 0;
  var_VREF = 0;
  for (i=0; i<LENGTH-1; i++) { // Valla Dios a saber por que pero la ultima posicion esta corrupta asi que no se puede usar...
    var_VTEMP += ((double)mediciones_VTEMP[i]-mean_VTEMP)*((double)mediciones_VTEMP[i]-mean_VTEMP);
    var_VREF += ((double)mediciones_VREF[i]-mean_VREF)*((double)mediciones_VREF[i]-mean_VREF);
  }
  var_VTEMP /= LENGTH;
  var_VREF /= LENGTH;
  
  temp = (mean_VTEMP - ORDENADA)/PENDIENTE;
  
  Serial.print(serial_temp);
  Serial.print('\t');
  Serial.print(mean_VTEMP);
  Serial.print('\t'); 
  Serial.print(var_VTEMP);
  Serial.print('\t');
  Serial.print(mean_VTEMP*0.0048876);
  Serial.print('\t');
  Serial.print(mean_VREF);
  Serial.print('\t');
  Serial.print(var_VREF);
  Serial.print('\t');
  Serial.print(mean_VREF*0.0048876);
//  Serial.print('\t');
//  Serial.print(temp);
  Serial.print('\n');
  
  for (i=0; i<LENGTH; i++) {
    Serial.print(mediciones_VTEMP[i]);
    Serial.print('|');
  }
  Serial.print('\n');
  
  if (var_VTEMP > VAR_ALARM || var_VREF > VAR_ALARM) {
    varAlarm();
  }
  
}

void leerCadena (char corte, char* cadena, byte n_bytes) {
  byte n_leidos;
  n_leidos = Serial.readBytesUntil(corte, cadena, n_bytes);
  cadena[n_leidos] = '\0';
  delay(100);
  while (Serial.read() >= 0) {;}
  
}

void varAlarm (void) {
  byte i;
  for (i=0; i<10; i++) {
    tone(PARLANTITO, 1000);
    delay(20);
    noTone(PARLANTITO);
    delay(20);
  }
}

One of the problems is here:

  unsigned int mediciones_VTEMP[LENGTH-1], mediciones_VREF[LENGTH-1];

the length of your arrays is not enough to hold all the samples. Change this to:

  unsigned int mediciones_VTEMP[LENGTH], mediciones_VREF[LENGTH];

and you’ll be fine.

Why you want to clean the array?

  for (i=0; i<LENGTH; i++) {
    mediciones_VREF[i] = 0; // Al descomentar esta linea se pudre todo...
    delay(1);
  }