Electronic – Arduino Serial print changes behavior of program undesireably


I'm using a loop counter, declared in a header:

int loop_counter = 0;

I use this counter to trigger an event every so often. I used to use a modulo for this same type of behavior, but I simplified it so it's easier to work with (it still results in the same behavior)

void loop() {
    if(loop_counter > 100) loop_counter = 0;
    else loop_counter++;


    if(loop_counter == 0) {

All is well and good, until I try communicating with Serial by uncommenting the //Serial.println("hey"); ("hey" in this example because, to me, this behavior is absurd).

This results in loop_counter never triggering the do_something_important(); section of code. I tried declaring loop_counter as volatile, that didn't change anything. I tried Serial.print ing loop_counter, and I was also getting odd behavior (it would freeze the loop). Serial.println("hey"); works in the sense that in the Serial monitor I get plenty of "hey", (i.e. quickly a lot more than 100 "heys", the number of iterations at which the other section of code should trigger)

What could possibly be causing the usage of Serial, with data that is not (as far as I can tell) tied to loop_counter completely prevent it from working properly?

EDIT: Here is the part of the main file that ended up posing the problem (well, contributing the most to it (using too much memory)):

void display_state() {
  int i,j,index=0;
  short alive[256][2];

 for(i=0;i<num_rows;i++) { 
   for(j=0;j<num_cols;j++) {
     if(led_matrix[i][j]==1) { 
 alive[index][0]=NULL; //Null-terminate.

 //383 is a great number
 for(int idx=0;idx < index; idx++) {

Here is "letters.h":

    #ifndef _MY_LETTERS_H
    #define _MY_LETTERS_H

#define nrows 4
#define ncols 4

#define num_rows 16
#define num_cols 16

#define MAX_WORD_LENGTH 16

#include <stdlib.h>

int loop_counter = 0;
short led_matrix[num_rows][num_cols];

const short letter_a[nrows][ncols] = {{0,1,1,0},
const short letter_b[nrows][ncols] = {{1,0,0,0},{1,1,1,0},{1,0,1,0},{1,1,1,0}};
const short letter_c[nrows][ncols] = {{0,1,1,1},{1,0,0,0},{1,0,0,0},{0,1,1,1}};
const short letter_t[nrows][ncols] = {{1,1,1,1},{0,1,0,0},{0,1,0,0},{0,1,0,0}};

typedef struct letter_node{
const short *data;
letter_node *next;
int x;
int y;
} letter_node;

letter_node aa = {&letter_a[0][0],NULL,1,1};
letter_node bb = {&letter_b[0][0],NULL,1,1};
letter_node cc = {&letter_c[0][0],NULL,1,1};
letter_node tt = {&letter_t[0][0],NULL,1,1};

letter_node letter_map[NUMBER_OF_CHARACTERS];

Some more information:
– I'm using an Uno (ATMega328)

Best Answer

I also had a similar problem to this, and am very sure that yours is also out of stack space related. Try shrinking the code as much as possible.

In my case code would sometimes run when I had a serial message in it, but then it would seem to not run when I did not. I also had a case where sending serial messages would cause the arduino to reset endlessly.

I was also using an arduino328. Likely you should reduce the size of your array if you have any to the smallest size that is acceptable.