when i open the serial monitor the value of a variable that is altered every 800ms gets an offset.
The code is for my fish tank. Currently i have added a temperature and a tds sensor and i output it on a TFT display.
Now: if the serial monitor is closed the variable tdsvalue value is printed with like “715 ppm” on the screen. If i open the serial monitor the value changes to like “515 ppm”. Is there a reason why the serial monitor could alter the value?
The whole code looks like this:
#include <Arduino.h>
#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//##########################################################
// TDS Sensor
//##########################################################
#define TdsSensorPin 4
#define VREF 3.3
#define SCOUNT 30
int analogBuffer[SCOUNT];
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0,copyIndex = 0;
float averageVoltage = 0,tdsValue = 0;
int getMedianNum(int bArray[], int iFilterLen);
unsigned long TDSTimepoint = millis();
unsigned long printTDSTimepoint = millis();
//##########################################################
// Temperatur Sensor
//##########################################################
const int oneWireBus = 18;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
float temperature;
unsigned long TempTimepoint = millis();
//##########################################################
// TFT Display
//##########################################################
TFT_eSPI tft = TFT_eSPI();
unsigned long currentMillis, seconds, minutes, hours, days;
void printTFT(float &temp);
//##########################################################
// Main Code
//##########################################################
bool print_out = false;
void setup(void) {
Serial.begin(9600);
//########################################################
// TDS Sensor Setup
//########################################################
pinMode(TdsSensorPin,INPUT);
//########################################################
// TFT Display Setup
//########################################################
tft.init();
tft.setRotation(3);
}
void loop() {
//########################################################
// Temperatur Sensor loop
//########################################################
if(millis()-TempTimepoint > 500U) //every 40 milliseconds,read the analog value from the ADC
{
TempTimepoint = millis();
sensors.requestTemperatures();
temperature = sensors.getTempCByIndex(0);
print_out=true;
}
//########################################################
// TDS Sensor loop
//########################################################
if(millis()-TDSTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
{
TDSTimepoint = millis();
analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
analogBufferIndex++;
if(analogBufferIndex == SCOUNT)
{
analogBufferIndex = 0;
}
}
if(millis()-printTDSTimepoint > 800U)
{
printTDSTimepoint = millis();
for(copyIndex=0;copyIndex<SCOUNT;copyIndex++)
analogBufferTemp[copyIndex]= analogBuffer[copyIndex];
averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
float compensationCoefficient=1.0+0.02*(temperature-25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
float compensationVolatge=averageVoltage/compensationCoefficient; //temperature compensation
tdsValue=(133.42*compensationVolatge*compensationVolatge*compensationVolatge - 255.86*compensationVolatge*compensationVolatge + 857.39*compensationVolatge)*0.5; //convert voltage value to tds value
Serial.print("TDS Value:");
Serial.print(tdsValue,0);
Serial.println("ppm");
print_out=true;
}
//########################################################
// TFT Display loop
//########################################################
if(print_out == true)
{
printTFT(temperature);
print_out = false;
}
}
int getMedianNum(int bArray[], int iFilterLen)
{
int bTab[iFilterLen];
for (byte i = 0; i<iFilterLen; i++)
bTab[i] = bArray[i];
int i, j, bTemp;
for (j = 0; j < iFilterLen - 1; j++)
{
for (i = 0; i < iFilterLen - j - 1; i++)
{
if (bTab[i] > bTab[i + 1])
{
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0)
bTemp = bTab[(iFilterLen - 1) / 2];
else
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
return bTemp;
}
void printTFT(float &temp){
currentMillis = millis();
seconds = currentMillis / 1000;
minutes = seconds / 60;
hours = minutes / 60;
days = hours / 24;
currentMillis %= 1000;
seconds %= 60;
minutes %= 60;
hours %= 24;
tft.fillScreen(TFT_BLACK);
tft.setCursor(0, 0, 2);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.setTextSize(1);
tft.print(temperature);
tft.println(" `C");
tft.print(tdsValue);
tft.println(" ppm");
tft.print(days);
tft.print(' ');
if (days < 10)
tft.print('0');
tft.print(hours);
tft.print(':');
if (minutes < 10)
tft.print('0');
tft.print(minutes);
tft.print(':');
if (seconds < 10)
tft.print('0');
tft.print(seconds);
}