Inactivating Serial.begin leads to restart

Hi all,

in the attached dummy sketch I ran into a strange problem.

For debugging I initiated Serial.begin in the setup loop. When everything worked as expected I commented the Serial.begin and some other related lines out. Reloading and starting the sketch lead to the repeatable problem, that turning the rotary encoder knob immediately was causing a restart.

First I thought of a memory overflow caused by having inserted/created custom characters (which was the original intention to learn a bit about that). But eventually I found out that the meanwhile inactive Serial.begin line was causing this behaviour. Leaving Serial.begin active resulted in a working sketch.

Could somebody update me with a reasonable explanation for that? I think I encountered that issue already in another project and remarked in the documentation that this “Serial.begin”-line should never be removed or commented out. Strange enough, isn’t it?

You can repeat that problem by toggling the Serial.begin line being active/inactive.

For your convenience I will also attach Ben Buxton’s fantastic rotary library (incl. support of button) being used in that sketch. (9.31 KB)

Cocktail_Test3.ino (7.47 KB)

What do these numbers mean

LiquidCrystal_I2C  lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

if it means that the LCD is connected, inter alia, to pins 0 and 1 that is probably the cause of the problem. Pins 0 and 1 on an Uno or Mega are used by Serial. You have not said what Arduino you are using.

I think the issue that @econjack linked to is only relevant if you are opening a serial connection on a PC connected to the Arduino.


Following up on Robin’s comment, it’s usually a good idea to leave pins 0 and 1 unchanged because they are used for serial communications. Also, pins 2 and 3 are external interrupt pins, so I try to leave those free when I can, just in case I want to add an interrupt later on.

Thank you both, @econjack and @robin2, for your thoughts.
The pins of the lcd initialization are the pins of the LCD as I am using an I2C operated lcd.
The wiring follows exactly the proposal on the LCD_I2C_Wiki.

So this has nothing to do with the TX/RX pins of the Arduino (nano in my case)
As commented in the sketch, I used pins 2 and 3 just “by chance”, because they were wired that way from another test build. They are not declared/used as interrupt pins in this test sketch. For the rotary decoder you can use any other digital pin.

To prove that I changed the wiring now from (3, 4, 2) to (4, 5, 6) in line #35 and repeated the test.
Same result: with activated Serial.begin everything works as expected, with inactive Serial.begin → Auto-Reset.

So my question is still valid.

rpt007: Strange enough, isn't it?

No, not strange at all.

Your sketch is creating buffer overflows in char arrays, and you never can tell if a buffer overflow will crash your program instantly, possibly with a reset, or if it will lead to undetected mistakes.

This is wrong code and creates buffer overflow in char arrray lcdline1 (you write after the end of the arrray when using 'sprintf() function call:

 char lcdline1[13];
    sprintf (lcdline1, "Cocktail #:  %02i", x + 1);

For the reason of avoiding buffer overflow with sprintf there is a'safe' version of that function named (snprintf(). to snprintf() you can/must provide the size of the buffer as a second parameter and then snprintf() will not wite after the end of the buffer size into the buffer, avoiding buffer overflow).


 char lcdline1[13];
    snprintf (lcdline1, sizeof(lcdline1, "Cocktail #:  %02i", x + 1);


I changed all code lines with your corrected version of the "sprintf" command.

snprintf (lcdline1, sizeof(lcdline1, "Cocktail #:  %02i", x + 1));


IDE compiler (1.6.9) fails: exit status 1 " too few arguments to function 'int snprintf(char*, size_t, const char*, ...)' " ??

snprintf/ shows the syntax for the function.

rpt007: @jurs:

I changed all code lines with your corrected version of the "sprintf" command.

snprintf (lcdline1, sizeof(lcdline1, "Cocktail #:  %02i", x + 1));


IDE compiler (1.6.9) fails: exit status 1 " too few arguments to function 'int snprintf(char*, size_t, const char*, ...)' " ??

The example I posted in reply #5 is missing a closing bracket with the sizeof() funcion:

fix it!

sizeof() needs one parameter in round brackets.

char lcdline1[13];
    snprintf (lcdline1, sizeof(lcdline1), "Cocktail #:  %02i", x + 1);

Thanks @jurs and @groundfungus,

meanwhile I found the syntax error myself in another thread while you were posting the corrections. I de-activated the Serial.begin() line and the previous re-start of the sketch is history.

The only thing which is left: I have to look carefully at the syntax for snprintf as the first line of my lcd is now showing not the complete text (the variable is not shown).

I guess that is just an issue with using the right synatx I am not yet familar with. But - I am happy that I learnt what was causing the "strange behaviour".

Will get back to this thread as soon as I have completely solved the remaining display issue and will re-post the corrected code then. Mightbe useful for people who are looking for a pretty simple solution for a flat (=1 layer deep) menu structure with using cheap rotary encoders without any further hardware needed.

Hi all,

thanks for your support.
Pls find attached the corrected code which is working without problems now thanks to the snprintf-function, thus eliminating the need for the somehow “error covering Serial.print function” of my previous sketch.

As this is just a test sketch - the real sketch has to deal without delays as you can see that the execution of the “case”-functions will be done only after the display gimmicks have come to an end.

But that will be another story.

Cocktail_Test3_ok.ino (7.18 KB)