Hello, I have just debugged a very frustrating bug in a larger project. I'll try to explain this in such a way that you guys don't have to read through hundreds of lines of irrelivant code. So here's the extent to which I've been able to isolate it.
I'm sending the possition of the cursor to the arduino. Sometimes one byte is lost, and the whole protocol gets borked. This only happens, if the DEBUG is pressent in the following function(if I comment it out, then the bytes get lost when I move the cursor fast):
void loop(){
charicter = nextChar();
#if DEBUG
Serial.print("Idle mode, handling char:");Serial.println(String(charicter));
#endif
switch(charicter){
case 3:set_cursor_pos();break;
}
}
Here is the larger code.
/*
Scratch file.
*/
#define DEBUG 1
int x;
int y;
byte charicter;
int buffer_rows = 1;
int buffer_columns =100;
byte buffer[800];
#define dotCount 6
const int dotPins[] = {13,11,9,7,5,3};
void fill_buffer()
{
for(int i = 0;i<buffer_columns;i++)
{
buffer[i]=1;
}
}
void setup()
{
// initialize the serial communication:
Serial.begin(9600);
// initialize the the pins as outputs:
for(int n = 0;n<dotCount;n++)
{
pinMode(dotPins[n], OUTPUT);
}
fill_buffer();
Serial.println("FCHAD");
}
void displayChar(byte charicter)
{
#if DEBUG
Serial.print("Displaying charicter:");Serial.println(charicter);
#endif
for(int n = 0;n<dotCount;n++)
{
if((1 << n) & charicter)
{
digitalWrite(dotPins[n],HIGH);
}else
{
digitalWrite(dotPins[n+8],LOW);
}
}
}
boolean in_buffer(){
return x<buffer_columns&&y<buffer_rows;
}
int readInt(){
while(!Serial.available());//Block untill next char is here.
int b1 = (uint16_t)Serial.read()<<8;
while(!Serial.available());//Block untill next char is here.
int b2 = Serial.read();
return b1+b2;
}
byte nextChar()
{
while(!Serial.available());//Block untill next char is here.
return Serial.read();
}
void set_cursor_pos(){
x=readInt();
y=readInt();
#if DEBUG
Serial.print("CURSOR POSSITION SET TO:");Serial.print(String(x));
Serial.print(",");
Serial.println(String(y));
#endif
if(in_buffer())
displayChar(buffer[x+y*buffer_columns]);
else{x=0;y=0;
#if DEBUG
Serial.println("CURSOR ROUTED TO INVALID REGION WITHIN BUFFER.");
#endif
}
}
void loop(){
charicter = nextChar();
#if DEBUG
Serial.print("Idle mode, handling char:");Serial.println(String(charicter));
#endif
switch(charicter){
case 3:set_cursor_pos();break;
}
}
//EOF
Should I try to debug this further and report a bug to the Arduino team? Or should I just leave it at that, and not use debugging code?