First code

So I’m trying to add an average to smooth things out and to practice coding.

Could you correct the code? i’m sure it’s wrong, but I want to know why.
Thank you

const byte tempPin[] = {A1, A2, A3, A4};
const byte relayPin[] = {6, 7, 8, 9};

// hysteresis = upperLimit - lowerLimit
const byte lowerLimit = 24;
const byte upperLimit = 31;

float tempC[4];
word reading[4];

const int numReadings = 50; //added
int readings[numReadings];                  //added
int readIndex = 0;                              //added
int total = 0;                                     //added
int average[4] = 0;[/color]                  //addded


word printInterval = 1000; // 1 second
unsigned long printCheck = 0, lastPrintTime = 0;

void setup()
{
  analogReference(INTERNAL);
  Serial.begin(115200);
  for (int i = 0; i < 4; i++) {
    pinMode(relayPin[i], INPUT_PULLUP);
    pinMode(relayPin[i], OUTPUT); // defaults HIGH, relay OFF
  }
 for (int thisReading = 0; thisReading < numReadings; thisReading++){ //added
    readings[thisReading] = 0;                                                        //added
  }
}

void loop()
{
  // readings and control
  for (int i = 0; i < 4; i++) {
    reading[i] = analogRead(tempPin[i]);
    tempC[i] = reading[i] / 9.31;
    total[i] = total[i] - readings[readIndex];  //added
    readings[readIndex] = tempC[i]                            //added
    total[i] = total[i] + readings[readIndex]                 //added
    readIndex[i] = readIndex[i] + 1 ;                          //added
    if (readIntex[i] >= numReadings) {                        //added
      readIndex = 0;                                                //added  
    }
    average[i] = total[i] / numReadings;[/color]           //added
    
    if (average[i]< lowerLimit) {
      digitalWrite(relayPin[i], LOW);   //relay OFF
    }
    else if (average[i] > upperLimit) {
      digitalWrite(relayPin[i], HIGH);  // relay ON
    }
  }
  printCheck = millis() - lastPrintTime;
  if (printCheck >= printInterval) {
    for (int i = 0; i < 4; i++) {
      Serial.print("TempC");
      Serial.print(i + 1);
      Serial.print(" ");
      Serial.println(average[i]);
    }
    Serial.println();
    lastPrintTime = millis(); // reset print timer
  }
}

Does your code really have [ color=red ] crap in it?

PaulS:
Does your code really have [ color=red ] crap in it?

Nah, I tried to highmark the changes and forgot to delete the bb code afterwards. Sorry

Ok, a guy made it A LOT SIMPLER using Exponential smoothing:

const byte tempPin[] = {A1, A2, A3, A4};
const byte relayPin[] = {6, 7, 8, 9};

// hysteresis = upperLimit - lowerLimit
const byte lowerLimit = 24;
const byte upperLimit = 31;

const int numReadings = 64;
const word printInterval = 1000; // 1 second

float tempC[4];
unsigned long lastPrintTime = 0;

void setup()
{
  analogReference(INTERNAL);
  Serial.begin(115200);
  for (int i = 0; i < 4; i++) {
    digitalWrite(relayPin[i], HIGH);
    pinMode(relayPin[i], OUTPUT);
  }
}

void loop()
{
  // readings and control
  for (int i = 0; i < 4; i++) {
    float raw_temp = analogRead(tempPin[i]) / 9.31;
    tempC[i] += (raw_temp - tempC[i]) / numReadings;  // filter
    if (tempC[i] < lowerLimit) {
      digitalWrite(relayPin[i], LOW);   //relay OFF
    }
    else if (tempC[i] > upperLimit) {
      digitalWrite(relayPin[i], HIGH);  // relay ON
    }
  }
  if (millis() - lastPrintTime >= printInterval) {
    for (int i = 0; i < 4; i++) {
      Serial.print("tempC");
      Serial.print(i + 1);
      Serial.print(" ");
      Serial.println(tempC[i]);
    }
    Serial.println();
    lastPrintTime = millis(); // reset print timer
  }
}