[SOLVED] Possible problem with Strings, Serial.print(), or commas

Apologies for the weird subject line, but I’m having trouble narrowing down the exact problem.

I have a program running on an Arduino Nano to control an electric pump. It writes to a DAC (I2C) and an OLED (serial) as well as reading from a rotary encoder and tachometer signal (attached to an interrupt on D2).

The Arduino updates the OLED with input from the rotary encoder while occasionally printing to the serial monitor for troubleshooting.

On making certain edits and uploading the new code, the Arduino will “freeze”:
-The OLED no longer updates with input from the rotary encoder
-Nothing appears on the serial monitor

The below snippet of code outputs the expected values to the serial monitor (with everything else working normally as well)

for(int i = 1; i <= 3; i++){
    Serial.print(*(routines[2] + (i * 2 ) - 1));

Whereas replacing Serial.print() with Serial.println caused the “freezing” issue as previously described:

for(int i = 1; i <= 3; i++){
    Serial.print(*(routines[2] + (i * 2 ) - 1));

I changed no part of the program other than this line. Uploading with the first snippet in void setup() resulted in normal behavior; uploading with the second snippet caused the “freezing” behavior. I went back and forth between the two versions with consistent results.

I tried cutting out different sections of code without being able to reproduce the issue, so I unplugged and went home. On restarting Arduino IDE, the problem went away; uploading either version of the program resulted in “normal behavior”.

After making a few more edits the Arduino started freezing again. This time:

Code snippet that allows for normal behavior:

display.println("JZ Version 0.0"); display.println("Poly6 Technologies.");

Replacement snippet that causes freezing behavior:

display.println("JZ, Version 0.0"); display.println("Poly6 Technologies.");

The only difference is a comma in the string.
I’ve never come across an issue like this before. I tried looking up issues with Serial.print() but this seems too specific; the only lead I have is that no other edits have caused freezing behavior other than changes involving Serial.println() or display.println(). Has anyone had a similar issue or know where to begin troubleshooting?

I’ve uploaded my program in its entirety since I haven’t been able to reliably replicate the issue other than in the previously mentioned situations.

menuWithRotary-sundayNightCodingSpree.ino (9.4 KB)

Please post ALL the code, using code tags. Snippets are useless. Especially when two identical snippets are posted, and discussed as if they are different.

See the "How to use this forum" post for additional hints.

I had already attached the entirety of the code to the original post since it's lengthy, would it be better if I embed all of it in the post?
I apologize if I've posted identical code snippets, but I believe the ones I posted are different. Could you point out the snippets you're referring to?

Actually, I just noticed the identical snippets. Sorry!

The first identical snippet was correct, reposted below:

for(int i = 1; i <= 3; i++){
    Serial.print(*(routines[2] + (i * 2 ) - 1));

The second should read:

for(int i = 1; i <= 3; i++){
    Serial.println(*(routines[2] + (i * 2 ) - 1));
  1. Yes. However, it is always better to post the minimum code that reproduces the problem.

  2. The first two snippets are identical.

Freezing is often caused by exceeding available RAM memory, array bound violations, or on the standard Arduino, memory problems caused by String object manipulation. Avoid use of Strings.

Thanks for the tips. I looked up freezing from overusing RAM and it matches my problems pretty closely. I found that using F() around string literals helped a lot.

When you say avoid use of Strings, do you mean use char arrays instead? I saw that string manipulation abuses memory, so just to be clear, does using character arrays avoid that issue?

does using character arrays avoid that issue?

Yes, because the array sizes are fixed.