### Boolean to define prefix

Hi

I have a number of string variables

String Day1_Log = "Logs for day 1: "
String Day2_Log = "Logs for day 2: "
String Day3_Log = "Logs for day 3: "

And a value that accumulates over time the variable is called,

int Power_Value = 1

that power value will increase gradually over time and there are other values like it, one is a max value, another is an on and off.

Given that there are 86400000 in a day I need to append the values upon trigger boolean to the correct day logs.

For example if millis is 8578990 it is day 1 so the value Power_Value = 6 if it was 6 would be appended to Day1_Log = "Logs for day 1: " making it Day1_Log = "Logs for day 1: 8578990, 6, "

If the next time the power value boolean is satisfied as True at 86200000 but this time the Power_Value was 10, the same would happen and it would be "Logs for day 1: 8578990, 6, " changing to "Logs for day 1: 8578990, 6, 86200000, 10, "

In the above case there would have been two values added to the Day1 log string.

As with the examples before if the next time it happens the millis value is 89400000 it would be Day2 so the output would move on from day1 to append to day 2, if this time the Power_Value was still 10 that would be fine and the appended variable would be Day2_Log = "Logs for day 2: 89400000, 10, "

Sorry for the long winded explanation, I understand the boolean for basic if, then but not how to use an antecedent IF that leads to a condition about which variable to append something to.

Thanks for your help!
Last edited on
this is a little hard to follow, but, are you saying
that you need to deal with days from milliseconds?
then..
int days = millis / 8640...000
if(days == 1)
append to log 1
else if (days == 2)
append to log 2
//add 1 if day zero isn't a thing for you..!

which is done by
filename += std::to_string(days);
where filename is a string of what log file to open, eg 1,2,3,...10000,...

and similar logic, eg if you want to keep writing to an open file.. compare newfilename and currentfilename if it is different, close old and open new, if not, keep writing...
Last edited on
If you have C++20 there is no reason to have a separate string for the base of every day's log file.
 ``12345678910111213`` ``````#include #include #include // C++20 using namespace std; int main() { int day_num = 1; string filename; filename = format("Logs for day {}: ", day_num); cout << "Logname name is: " << filename << endl; }``````

If you don't have C++20, you can accomplish the same thing with a C string and sprintf.
Hi, thanks for your feedback.
Nice..

I don't have any volatile memory, am tring to run TinyML, BTLE comms, and find a way to appending to logs but only by adjusting variables. Not usual protocol but it's a small microcontroller and doesn't even have a real time function.

Backup battery and multiple chances available! Lol

 ``12345678910111213141516171819202122232425`` ``````char rx_byte = 0; int TEST_VALUE = 887; void loop() { if (Serial.available() > 0) { // is a character available? rx_byte = Serial.read(); // get the character if ((rx_byte == '1')) { Serial.println("Number 1 received"); } else if ((rx_byte == '2')) { Serial.println("Number 2 received"); Serial.println(millis()); Str_ON_Log.concat(millis()); Str_ON_Log.concat(StrSpacer + TEST_VALUE + StrSpaceCommaSpace); Serial.println(Str_ON_Log); TEST_VALUE = 999; } else { Serial.println("Not logged."); } } // end: if (Serial.available() > 0) } */``````
Last edited on
The latest code is working and it looks for a state change
First it was on serial buttons then it evolved into something fed by the variable.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` `````` int R1_Pin = A1; int R1_ADC; String POWER_STATE; String last_POWER_STATE; String Str_CommaSpace = ", "; String Str_POWER_ON_Log = "Switched on at: "; // When CT is reading add a timestamp String Str_POWER_OFF_Log = "Switched off at: "; // When CT is not reading add a timestamp void loop() { R1_ADC = analogRead(R1_Pin); String str1_R1_ADC = "R1_ADC: "; Serial.println(str1_R1_ADC + R1_ADC); delay(1000); if (R1_ADC > 1) { POWER_STATE = "ON"; } else { POWER_STATE = "OFF"; } if (POWER_STATE != last_POWER_STATE) { if (R1_ADC > 1) { String Str1_POWER_STATE = "Power State Changed to.... "; Serial.println(Str1_POWER_STATE + POWER_STATE); Str_POWER_ON_Log.concat(millis()); Str_POWER_ON_Log.concat(Str_CommaSpace); Serial.println(Str_POWER_ON_Log); Serial.println(Str_POWER_OFF_Log); last_POWER_STATE = "ON"; } else { String Str1_POWER_STATE = "Power State Changed to...."; Serial.println(Str1_POWER_STATE + POWER_STATE); Str_POWER_OFF_Log.concat(millis()); Str_POWER_OFF_Log.concat(Str_CommaSpace); Serial.println(Str_POWER_ON_Log); Serial.println(Str_POWER_OFF_Log); last_POWER_STATE = "OFF"; } } // Print Current Status = ON / OFF String Str2_POWER_STATE = "Currently Switched: "; Serial.println(Str2_POWER_STATE + POWER_STATE); delay(10000); }``````

I just need to change the format of the millis output later and leave it as they are for now.

Topic closed :-)
Last edited on
Topic archived. No new replies allowed.