I was trying to clean up some of my code last night for emonbase (effectively NanodeRF_Power_RTCrelay_GLCDtemp) and removed all the serial output with the use of a conditional compilation flag. However, removing the serial output lines made the code very unstable, and it would restart every minute or so. I had expected the opposite. Putting the serial back in and it returned to its previous (semi-)stable state.
Can anyone explain why? The only suggestion I could think of was that there was some critical timing going on?
Thanks
Lloyd
Re: Code stability
I too have some very strange goings on. Simply adding one line of serial output changes the stability of the code.
Going back to the downloaded code and wrapping the serial prints in dhcp_dns with #ifdef DEBUG and #endif resulted in the node continually restarting. Reversing that change restored stability.
Commenting out the same serial prints also resulted in stable code.
Any guidance much appreciated.
Geoff
Re: Code stability
You can try this to check your free RAM:
//**********************************************************************************************************************
// SETUP_memory
//**********************************************************************************************************************
void setup_memory() {
int result = memoryTest();
Serial.print("Memory:");
Serial.println(result,DEC);
}
// this function will return the number of bytes currently free in RAM
int memoryTest() {
int byteCounter = 0; // initialize a counter
byte *byteArray; // create a pointer to a byte array
// More on pointers here: http://en.wikipedia.org/wiki/Pointer#C_pointers
// use the malloc function to repeatedly attempt allocating a certain number of bytes to memory
// More on malloc here: http://en.wikipedia.org/wiki/Malloc
while ( (byteArray = (byte*) malloc (byteCounter * sizeof(byte))) != NULL ) {
byteCounter++; // if allocation was successful, then up the count for the next try
free(byteArray); // free memory after allocating it
}
free(byteArray); // also free memory after the function finishes
return byteCounter; // send back the highest number of bytes successfully allocated
}