Embedded Loop and Array Issues

We are building this code to take temperature measurements in a for loop and input them to an array. The goal is to average the array values. The following is a flowchart with our thought process.

This is the code we have developed. We are unsure of where the array needs to be created, how to create it in this context, and how to average the values.

#include <SPI.h>
#include "Adafruit_MAX31855.h"

// Shared Pins for Thermocouples
#define MAXDO   2
#define MAXCLK  3

// digital IO pins for Thermocouple1
#define MAXCS1   4
Adafruit_MAX31855 thermocouple1(MAXCLK, MAXCS1, MAXDO);

// digital IO pins for Thermocouple2
#define MAXCS2   5
Adafruit_MAX31855 thermocouple2(MAXCLK, MAXCS2, MAXDO);

void setup() {
Serial.begin(9600);                                   //communicates and sets data rate in bits per second
while (!Serial) delay(1);                             //wait for Serial on Leonardo/Zero, etc

//Thermocouple Setup
Serial.println("MAX31855 test");
// wait for MAX chip to stabilize
delay(500);
Serial.print("Initializing sensor...");
if (!thermocouple1.begin()) {
Serial.println("ERROR.");
while (1) delay(10);
}
Serial.println("DONE.");

Serial.println("MAX31855 test");
// wait for MAX chip to stabilize
delay(500);
Serial.print("Initializing sensor...");
if (!thermocouple2.begin()) {
Serial.println("ERROR.");
while (1) delay(10);
}
Serial.println("DONE.");

//Relay Setup
int relay1 = 8;                                       //sets relay1 variable as value of 11
pinMode(relay1, OUTPUT);                              //sets digital pin 11 as an output         

//LED Setup
int led1 = 9;
pinMode(led1, OUTPUT);

}

void loop() {

// basic readout test, just print the current temp
Serial.print("Internal Temp = ");
Serial.println(thermocouple1.readInternal());

float c1 = thermocouple1.readCelsius();        

//If-Else Statement providing Error Code
if (isnan(c1)) {
    Serial.println("Something wrong with Thermocouple 1.");
} 
else {
  Serial.print("C1 = ");
  Serial.println(c1);
}

// basic readout test, just print the current temp
Serial.print("Internal Temp = ");
Serial.println(thermocouple2.readInternal());

float c2 = thermocouple2.readCelsius();           

//If-Else Statement providing Error Code
  if (isnan(c2)) {
    Serial.println("Something wrong with Thermocouple 2.");
  } 
  else {
    Serial.print("C2 = ");
    Serial.println(c2);
}

float C_diff = c2 - c1;
if (C_diff >= 100){
  for (int i = 0; i < 120; i++){
  
    float c1 = thermocouple1.readCelsius();
    float c2 = thermocouple2.readCelsius();
    
    C_diff = c2 - c1;
    float arrayC_diff[i] = C_diff;
    
    delay(500);
}
}
else{
  digitalWrite(relay1, LOW);
  Serial.println("Relay Pin Low");
}


float arrayC_diff[y];
avgarrayC_diff = ???;
if(avgarrayC_diff>125){
  digitalWrite(led1, HIGH);
  Serial.println("Overheating Condition");
  digitalWrite(relay1, HIGH);
  Serial.println("Relay Pin High"); 
}

delay(60000);
}

Any help would be greatly appreciated. Thanks!

You don’t need an array with all the values If you only want the average
Calculate the sum of all the readings and at the end divide the sum by the number of samples

It looks like your flowchart is:

void loop()
{
  do 
  {
    int t1 = GetAmbientTemperature();
    int t2 = GetDirectTemperature();
  } while (t2-t1 <= 100);

  long total = 0;
  for (int i=0; i<120; i++)
  {
    int t1 = GetAmbientTemperature();
    int t2 = GetDirectTemperature();
    total += (t2-t1);
  }
  int average = total / 120;
  if (average > 125)
  {
    digitalWrite(LEDSignalPin, HIGH);
    digitalWrite(RelayPin, LOW); // Active Low
  }
}

Do you really never want the LED or Relay to turn off? That's what your flow chart is saying.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.