Hi, I am new to Arduino programming and am having a problem using circular buffers, I clearly have a mistake in my code somewhere.
I wish to add some debugging to a sketch and want to use a function logit() to insert a record into a circular buffer in the form:-
dd hh:mm
If I call struct.push in the loop() function, the correct record gets inserted into the circular buffer. If I call the logit() function and do the struct.push there, the date/time are correct but not the debug text.
Code:-
#include <CircularBuffer.h>
namespace data {
typedef struct {
unsigned int day;
unsigned int hour;
unsigned int minute;
char text[20];
} record;
void print(record r) {
Serial.print(r.day);
Serial.print(" ");
Serial.print(r.hour);
Serial.print(":");
Serial.print(r.minute);
Serial.print(" ");
Serial.println(r.text);
}
}
CircularBuffer<data::record, 10> structs;
int days=12;
int hours=15;
int minutes=35;
char text[20];
void setup() {
Serial.begin(9600);
Serial.println("STARTING UP");
}
void loop() {
structs.push(data::record{days,hours,minutes,"abcdefg"});
minutes++;
structs.push(data::record{days,hours,minutes,"hijk"});
minutes++;
logit(days, hours, minutes, "another test");
strcpy(text,"morecopiedtext");
minutes++;
logit(days, hours, minutes, text);
Serial.println("Print Stack");
Serial.print(F("structs size:"));
Serial.println(structs.size());
while (!structs.isEmpty()) {
data::print(structs.shift());
Serial.println();
}
Serial.println("START AGAIN");
delay(100000);
} // end main loop
void logit(int d, int h, int m, char * instr){
Serial.print("logit = days, hours, minutes, text =");
Serial.print(d);
Serial.print(h);
Serial.print(m);
Serial.println(instr);
structs.push(data::record{d,h,m,instr});
}
Outout:-
19:57:57.300 ->STARTING UP
19:57:57.300 -> logit = days, hours, minutes, text =121537another test
19:57:57.347 -> logit = days, hours, minutes, text =121538morecopiedtext
19:57:57.394 -> Print Stack
19:57:57.441 -> structs size:4
19:57:57.441 -> 12 15:35 abcdefg
19:57:57.441 ->
19:57:57.488 -> 12 15:36 hijk
19:57:57.488 ->
19:57:57.488 -> 12 15:37 p
19:57:57.488 ->
19:57:57.488 -> 12 15:38 ⸮
19:57:57.488 ->
19:57:57.488 -> START AGAIN
Thanks for any assistance!