Hey guys,
I have a question. I want to clean up my files and sort the code into different files. So i have my code in the main sketch.ino and my string will go into "strings.h". I am also doing this for functions.
The problem i have is with strings. For some error messages i have multiple strings, but instead if making a new function for each string that returns, I am putting them in a array. Then i simply call the function and put a number in it to call what string i want to have.
The problem that i have is when i call multiple strings right after each other, the monitor does not show all characters. Sometimes it leaves characters from the beginning, the middle or the end of the sentence. Sometimes the console start showing little squares as it does not recognize the characters.
But if i only as for the 1 string then it is always correct.
Can anyone explain me what is wrong with the code ?
Please note i am still working the script.
I am talking about the "Monitor()" function.
Many thanks for your time and help
Main sketch
#include "DHT.h"
#include "monitor.h"
#define sensor 2
#define type DHT11
DHT dht(sensor, type);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Monitor(true, error_sensor(0));
delay(1000);
Monitor(true, error_sensor(1));
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}
monitor.h
String Monitor(int switcher, String text) {
if (switcher == true) Serial.println(text);
if (switcher == false) Serial.print(text);
}
String error_sensor (int nr) {
String v[] = {
"SYSTEM ERROR: Unable to read sensor!",
"SYSTEM EXIT: Restart the program to try again."
};
return v[nr];
}