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++;
//Serial.println("hey");
if(loop_counter == 0) {
//do_something_important();
}
}
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]=i;
alive[index][1]=j;
index++;
}
}
}
alive[index][0]=NULL; //Null-terminate.
alive[index][1]=NULL;
//383 is a great number
for(int idx=0;idx < index; idx++) {
display(alive[idx][0],alive[idx][1]);
delayMicroseconds(283);
}
}
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
#define NUMBER_OF_CHARACTERS 26
#include <stdlib.h>
int loop_counter = 0; short led_matrix[num_rows][num_cols];
const short letter_a[nrows][ncols] = {{0,1,1,0}, {1,0,0,1}, {1,1,1,1}, {1,0,0,1}}; 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]; #endif
Some more information: - I'm using an Uno (ATMega328)
loop()
function. How should I paint my stack if the only method of output I have (Serial.print()
) is failing me? \$\endgroup\$