Howdy!
I've been working on a data collecting Arduino code. Its purpose is to take 2 thermocouples, 1 load cell, and 1 pressure sensor, format the data for PLX DAQ, add time/relevant data, then print it to the serial monitor.
However, I've run into a problem where the data being written to the serial monitor is extremely laggy. Though I have the delay set to run a loop about 5.7 times per second, the most it can do is 3 or 4 times per second. When adding or subtracting weight from the load cell, it can take up to 4 seconds to register the weight going from 0g to 131g, a far cry from the mere 3/4 second it takes in the example sketch. The application this is being used for requires near-instantaneous readings.
When I try to reduce the delay at the end of my code, the thermocouples stop collecting data altogether. The lowest delay usable without breaking them is 175.
I've tried adding and subtracting from delay, changing the baud rate, and optimizing how data is converted. Nothing seems to help.
Using an Arduino Uno (original), my thermocouples are running on MAX6675 modules, the load cell is running off an HX711 module. Do keep in mind I opened my first Arduino tutorial at 3pm, it's current 2am. For organization sakes, if I used any sort of variable, it's declared above setup or given a value before it's assigned an operational value. This code is guaranteed to look like a toddler with Parkinson's slept on a keyboard, so go easy on me.
TLDR: Input data takes an unreasonable amount of time to update in the serial monitor and the thermocouples outright break when the delay is lowered below 175.
Cheers!
//Load Cell Global Variables
const int HX711_dout = 8;
const int HX711_sck = 9;
const int calVal_eepromAdress = 0;
unsigned long t = 0;
int thrust = 0;
float i = 0;
//Clock Global Variables
int loopCount = 0;
int activated = activated;
unsigned long timer;
unsigned long millisUnmod;
unsigned long math;
unsigned long timerMS;
//Pressure Sensor Global Variables
const int pressureInput = A1;
const int pressureZero = 100;
const int pressureMax = 900;
const int pressuretransducermaxPSI = 300;
float pressureValue = 0;
#include <Thermocouple.h>
#include <MAX6675_Thermocouple.h>
#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif
HX711_ADC LoadCell(HX711_dout, HX711_sck);
//Exhaust Thermocouple
#define SCK_PIN 2
#define CS_PIN 3
#define SO_PIN 4
Thermocouple* thermocoupleExhaust;
//Internal Thermocouple
#define SCK_PIN2 5
#define CS_PIN2 6
#define SO_PIN2 7
Thermocouple* thermocoupleInternal;
//---------------------------------------
void setup() {
Serial.begin(9600);
Serial.println("CLEARSHEET");
Serial.println("RESETTIMER");
Serial.println("LABEL,Sample No., Timer MS, Internal Celsius, Internal Fahrenheit, Exhaust Celsius, Exhaust Fahrenheit, Chamber Pressure PSI, Thrust Grams, ");
thermocoupleExhaust = new MAX6675_Thermocouple(SCK_PIN, CS_PIN, SO_PIN);
thermocoupleInternal = new MAX6675_Thermocouple(SCK_PIN, CS_PIN, SO_PIN);
//Load Cell Setup
LoadCell.begin();
float calibrationValue;
calibrationValue = 213.65;
#if defined(ESP8266)|| defined(ESP32)
#endif
unsigned long stabilizingtime = 2000;
boolean _tare = true;
LoadCell.start(stabilizingtime, _tare);
LoadCell.setCalFactor(calibrationValue);
}
//--------------------------------------
void loop() {
timer = millis();
millisUnmod = millis();
loopCount = loopCount + 1;
const double celsius = thermocoupleInternal->readCelsius();
const double fahrenheit = thermocoupleInternal->readFahrenheit();
const double celsius2 = thermocoupleExhaust->readCelsius();
const double fahrenheit2 = thermocoupleExhaust->readFahrenheit();
pressureValue = analogRead(pressureInput);
pressureValue = ((pressureValue - pressureZero) * pressuretransducermaxPSI) / (pressureMax - pressureZero);
Serial.print("DATA, ");
Serial.print( (String)(loopCount) + (", "));
if (timer >= 1000) {
timer = timer / 1000;
math = timer * 1000;
timerMS = millisUnmod - math;
Serial.print( (String)(timer) + ("s ") + (timerMS) + ("ms, "));
}
else {
Serial.print( (String)(timer) + ("ms, "));
}
Serial.print( (String)(celsius) + (", "));
Serial.print( (String)(fahrenheit) + (", "));
Serial.print( (String)(celsius2) + (", "));
Serial.print( (String)(fahrenheit2) + (", "));
Serial.print( (String)(pressureValue) + (", "));
float i = LoadCell.getData();
Serial.println(i);
delay(175);
}