I am passing some strings and some sensor data to a function.
The strings are of two types:
- remarks
- description of sensors
The remarks should be printed with Serial.println and the descriptions with Serial.print followed by the sensor data printed with Serial.println.
I have tried (String).substring() and (String).indexOf(). The character to be searched would be ':' as this would be the character that definitely differs in the two types of strings. IndexOf() seemed to give kind of a correct result in the sense that it would print the string and sensor in one line. But it would also do the same for the string without ':'.
I used at first this code:
if ( debugName.indexOf(':') )
Serial.print(debugName);
else
Serial.println(debugName);
After the result was wrong I thought that may be I would need to work with a flag which didnt improve the situation:
if ( debugName.indexOf(':') )
description = true;
if ( description )
Serial.print(debugName);
else
Serial.println(debugName);
Here is the whole code:
#define DEBUG_ERROR true
//#define DEBUG_ERROR_SERIAL if(DEBUG_ERROR)Serial
#define DEBUG_WARNING true
//#define DEBUG_WARNING_SERIAL if(DEBUG_WARNING)Serial
#define DEBUG_INFORMATION true
#define DEBUG_INFORMATION_SERIAL if(DEBUG_INFORMATION)Serial
const int sensor1 = 5;
const float sensor2 = 10.6;
byte previousId;
void debugTitle(const byte &id)
{
const char idTitle [] [12] = {"Time", "Lamp", "Temperature", "Humidity", "Waterlevel"};
Serial.print(F("--- "));
Serial.print(idTitle[id]);
Serial.println(F(" ---"));
}
void debugMsg(const String &debugName, const byte &id)
//template<typename T> void debug(const T &debugName, const byte &id)
{
const unsigned int debugOutputInterval = 1000;
static unsigned long debugOutputTimestamp;
unsigned long currentMillis = millis();
static bool description = false;
if (currentMillis - debugOutputTimestamp >= debugOutputInterval)
{
debugOutputTimestamp = currentMillis;
//static byte previousId;
if ( id != previousId )
{
previousId = id;
debugTitle(id);
}
if ( debugName.indexOf(':') )
description = true;
if ( description )
Serial.print(debugName);
else
Serial.println(debugName);
}
}
template<typename T> void debugSensor(const T &debugName, const byte &id)
{
const unsigned int debugOutputInterval = 1000;
static unsigned long debugOutputTimestamp;
unsigned long currentMillis = millis();
if (currentMillis - debugOutputTimestamp >= debugOutputInterval)
{
debugOutputTimestamp = currentMillis;
//static byte previousId;
if ( id != previousId )
{
previousId = id;
debugTitle(id);
}
Serial.println(debugName);
}
}
void setup()
{
Serial.begin(115200);
while (!Serial);
}
void loop()
{
if ( DEBUG_ERROR )
{
debugMsg("Hello Error: ", 1);
debugSensor(sensor1, 1);
}
if ( DEBUG_WARNING )
{
debugMsg("Hello Warning", 2);
debugSensor(sensor2, 2);
}
}
Is there also a way to do the same with this function taking both strings and sensor data?
template<typename T> void debug(const T &debugName, const byte &id)
Thank you