Serial.print() kills my LEDs??

i am playing around with a circular POV LED writer. i have defined my own pixel font and i got the LEDs writing the text OK :slight_smile: so far so good.

weird thing happens though.... if i comment out my own debugging Serial.print lines, my LEDs stop flashing!!! this is really puzzling me....
i have narrowed it down to the Serial.print line in LEDUpdate() routine....
can anyone help me?

i just updated to arduino0018 - this changed nothing

thanks!
/j

below is my code:

#include <avr/pgmspace.h>
#include <Flash.h>

int ledPin[8] = {
  2,3,4,5,6,7,8,9};

// this is the string to send to the sign - put it in program memory
FLASH_STRING (text,
    "     1. A ROBOT MAY NOT INJURE A HUMAN BEING OR, THROUGH INACTION, ALLOW A HUMAN BEING TO COME TO HARM.     "
    "     2. A ROBOT MUST OBEY ORDERS GIVEN TO IT BY HUMAN BEINGS EXCEPT WHERE SUCH ORDERS WOULD CONFLICT WITH THE FIRST LAW.     "
    "     3. A ROBOT MUST PROTECT ITS OWN EXISTENCE AS LONG AS SUCH PROTECTION DOES NOT CONFLICT WITH THE FIRST OR SECOND LAW.    " 
    ); 

FLASH_ARRAY (byte, characterData, 
  0,0,0,-6,-64,0,-64,40,-2,40,-2,40,100,-110,-1,-110,76,66,-92,72,16,36,74,
  -124,108,-110,-86,68,26,-64,124,-126,0,-126,124,80,32,-8,32,80,16,16,
  124,16,16,1,6,8,8,8,0,2,3,12,16,96,-128,124,-126,-126,-126,124,-128,-2,
  66,-122,-118,-110,98,68,-126,-110,-110,108,24,104,-120,-2,8,-12,-110,
  -110,-110,-116,124,-110,-110,-110,76,-128,-126,-116,-80,-64,108,-110,
  -110,-110,108,100,-110,-110,-110,124,0,34,1,38,16,40,68,40,40,40,68,40,
  16,64,-128,-118,-112,96,60,66,-71,-91,-87,-68,66,60,126,-112,-112,-112,
  126,-2,-110,-110,-110,108,124,-126,-126,-126,68,-2,-126,-126,-126,124,
  -2,-110,-110,-110,-126,-2,-112,-112,-112,-128,124,-126,-110,-108,94,-2,
  16,16,16,-2,-2,4,2,2,-4,-2,16,40,68,-126,-2,2,2,2,-2,64,32,16,32,64,-2,
  -2,64,56,4,-2,124,-126,-126,-126,124,-2,-112,-112,-112,96,124,-126,-126,
  -126,125,-2,-112,-104,-108,98,100,-110,-110,-110,76,-128,-128,-2,-128,
  -128,-4,2,2,2,-4,-64,56,6,56,-64,-4,2,2,-4,2,2,-4,-58,40,16,40,-58,-64,
  32,30,32,-64,-122,-118,-110,-94,-62,-1,-127,-127,-128,96,16,12,3,-127,
  -127,-1,64,-128,64,6,2,2,28,34,36,62,-2,18,34,28,28,34,34,20,28,34,36,
  -2,28,42,42,24,126,-96,-128,56,69,73,126,-2,16,32,30,-66,1,-66,-2,8,20,
  34,-2,62,16,32,30,16,32,30,62,16,32,30,28,34,34,28,127,36,68,56,56,68,
  72,127,62,16,32,18,42,36,32,-2,32,60,2,4,62,60,2,2,60,60,2,2,60,2,2,60,
  54,8,8,54,120,5,9,126,38,42,50,16,110,-127,-1,-127,110,16,64,-128,64,
  -128);
  
//index of each character
FLASH_ARRAY (int, characterIndex,
  0,2,4,7,12,17,24,29,30,32,35,40,45,47,50,52,57,62,64,69,74,79,84,89,94,
  99,104,106,108,111,114,117,122,130,135,140,145,150,155,160,165,170,
  171,175,180,184,191,196,201,206,211,216,221,226,231,236,243,248,253,
  258,261,266,269,272,275,275,279,283,287,291,295,298,302,306,307,309,
  313,314,321,325,329,333,337,340,343,346,350,354,361,365,369,372,375,
  376,379,383); //383 does not exist, is added to calculate width of char at 379
  
int maxStringSize;
int messageIndex;
char myChar;

void setup()   {                
  for (int i=0;i<8;i++) {
    pinMode(ledPin[i], OUTPUT); 
  }
  maxStringSize =  text.length();  // find message size
}
  
void loop()                     
{ 
  myChar =  text[messageIndex]-32; //we start ASCII table at SPACE
  
  LEDUpdate();

  messageIndex++;
  messageIndex = messageIndex % maxStringSize;

}

void LEDUpdate() {
  int dataIndex = characterIndex[myChar];
  
  Serial.print(dataIndex); // if i remove this one the LEDs don't update???
  
  int charWidth = characterIndex[myChar+1]-dataIndex;
  
  for (int i=0;i<charWidth;i++) {
    byte dataValue = characterData[dataIndex+i];
    for (int j=0;j<8;j++) {      
      int bitValue = bitRead(dataValue,j);
      if (bitValue == 0) { //LEDs are active low
        digitalWrite(ledPin[j], HIGH); 
      } else {
        digitalWrite(ledPin[j], LOW);
      }
    }
    delay(1); //pause for pixel width
  }
  
  //insert space between characters
  for (int i=0;i<8;i++) {
    digitalWrite(ledPin[i], HIGH); //all black
  }
  delay(2); //pause for space
}

No baud rate set - how does that work?

Hi, I suspect there is a "delay" caused by the Serial.print() allowing the LEDs to work.

The delays you are using are VERY short ( 1/1000 th of a second). Try a delay of a whole second -- delay(1000);

And Groove is right, you should also initialize the UART by setting the baud rate. You will thank us for this on your future projects...

sorry guys, when cleaning out my code before posting it, i deleted the Serial.begin(2400); line -- ofcourse i need this :slight_smile:

i also need the small delays, since my LEDs are rotating at quite high speed -- so leaving them on/off for about 1/1000 of a second is about right for the pixel width i am after. next step is tweaking the code for more precise LED calibration, but i need serial communication over wireless for this, hence this post. :wink:

thanks for the debug tips. the problem still persist -- even if i set the delay for a full second

but for kicks, i tried entering a small delay(1) in stead of the serial.print() command -- and this works! the LEDs are blinking happily when the delay is in there. hm.... i have no idea why the small delay is needed, but for now my problems are solved i guess....

working code:

#include <avr/pgmspace.h>
#include <Flash.h>

int ledPin[8] = {
  2,3,4,5,6,7,8,9};

// this is the string to send to the sign - put it in program memory
FLASH_STRING (text,
    "     1. A ROBOT MAY NOT INJURE A HUMAN BEING OR, THROUGH INACTION, ALLOW A HUMAN BEING TO COME TO HARM.     "
    "     2. A ROBOT MUST OBEY ORDERS GIVEN TO IT BY HUMAN BEINGS EXCEPT WHERE SUCH ORDERS WOULD CONFLICT WITH THE FIRST LAW.     "
    "     3. A ROBOT MUST PROTECT ITS OWN EXISTENCE AS LONG AS SUCH PROTECTION DOES NOT CONFLICT WITH THE FIRST OR SECOND LAW.    " 
    ); 

FLASH_ARRAY (byte, characterData, 
  0,0,0,-6,-64,0,-64,40,-2,40,-2,40,100,-110,-1,-110,76,66,-92,72,16,36,74,
  -124,108,-110,-86,68,26,-64,124,-126,0,-126,124,80,32,-8,32,80,16,16,
  124,16,16,1,6,8,8,8,0,2,3,12,16,96,-128,124,-126,-126,-126,124,-128,-2,
  66,-122,-118,-110,98,68,-126,-110,-110,108,24,104,-120,-2,8,-12,-110,
  -110,-110,-116,124,-110,-110,-110,76,-128,-126,-116,-80,-64,108,-110,
  -110,-110,108,100,-110,-110,-110,124,0,34,1,38,16,40,68,40,40,40,68,40,
  16,64,-128,-118,-112,96,60,66,-71,-91,-87,-68,66,60,126,-112,-112,-112,
  126,-2,-110,-110,-110,108,124,-126,-126,-126,68,-2,-126,-126,-126,124,
  -2,-110,-110,-110,-126,-2,-112,-112,-112,-128,124,-126,-110,-108,94,-2,
  16,16,16,-2,-2,4,2,2,-4,-2,16,40,68,-126,-2,2,2,2,-2,64,32,16,32,64,-2,
  -2,64,56,4,-2,124,-126,-126,-126,124,-2,-112,-112,-112,96,124,-126,-126,
  -126,125,-2,-112,-104,-108,98,100,-110,-110,-110,76,-128,-128,-2,-128,
  -128,-4,2,2,2,-4,-64,56,6,56,-64,-4,2,2,-4,2,2,-4,-58,40,16,40,-58,-64,
  32,30,32,-64,-122,-118,-110,-94,-62,-1,-127,-127,-128,96,16,12,3,-127,
  -127,-1,64,-128,64,6,2,2,28,34,36,62,-2,18,34,28,28,34,34,20,28,34,36,
  -2,28,42,42,24,126,-96,-128,56,69,73,126,-2,16,32,30,-66,1,-66,-2,8,20,
  34,-2,62,16,32,30,16,32,30,62,16,32,30,28,34,34,28,127,36,68,56,56,68,
  72,127,62,16,32,18,42,36,32,-2,32,60,2,4,62,60,2,2,60,60,2,2,60,2,2,60,
  54,8,8,54,120,5,9,126,38,42,50,16,110,-127,-1,-127,110,16,64,-128,64,
  -128);
  
//index of each character
FLASH_ARRAY (int, characterIndex,
  0,2,4,7,12,17,24,29,30,32,35,40,45,47,50,52,57,62,64,69,74,79,84,89,94,
  99,104,106,108,111,114,117,122,130,135,140,145,150,155,160,165,170,
  171,175,180,184,191,196,201,206,211,216,221,226,231,236,243,248,253,
  258,261,266,269,272,275,275,279,283,287,291,295,298,302,306,307,309,
  313,314,321,325,329,333,337,340,343,346,350,354,361,365,369,372,375,
  376,379,383); //383 does not exist, is added to calculate width of char at 379
  
int maxStringSize;
int messageIndex;
char myChar;

void setup()   {                
  for (int i=0;i<8;i++) {
    pinMode(ledPin[i], OUTPUT); 
  }
  maxStringSize =  text.length();  // find message size
  Serial.begin(2400);
}
  
void loop()                     
{ 
  myChar =  text[messageIndex]-32; //we start ASCII table at SPACE
  
  LEDUpdate();

  messageIndex++;
  messageIndex = messageIndex % maxStringSize;

}

void LEDUpdate() {
  int dataIndex = characterIndex[myChar];
  
  //Serial.print(dataIndex); // if i remove this one the LEDs don't update???
  
  delay(1); //but when this is entered it works again...
  
  int charWidth = characterIndex[myChar+1]-dataIndex;
  
  for (int i=0;i<charWidth;i++) {
    byte dataValue = characterData[dataIndex+i];
    for (int j=0;j<8;j++) {      
      int bitValue = bitRead(dataValue,j);
      if (bitValue == 0) { //LEDs are active low
        digitalWrite(ledPin[j], HIGH); 
      } else {
        digitalWrite(ledPin[j], LOW);
      }
    }
    delay(1); //pause for pixel width
  }
  
  //insert space between characters
  for (int i=0;i<8;i++) {
    digitalWrite(ledPin[i], HIGH); //all black
  }
  delay(2); //pause for space
}