Problems with Serial.println

I have been using Serial.print and Serial.println for quite a while as a debugging aid with no difficulty. Now, I'm having problems and after much searching and attempts to pin it down, I need some help.

Serial.println ("setup ends"); Serial.println (millis());

for the first line, the serial monitor shows nothing - no characters at all for the second line, the serial monitor shows a number as expected.

I have tried turned groups of statements like these on and off (as comments) in many places throughout a large program with the same results. Variables print and text does not. With all Serial. statements commented out, the program executes as expected. Any idea what to look for ?

I can add that I have tried with 9600 and 19200. The board is an Uno.

What is your code? Do you have a Serial.begin() call? Does the baud rate match up in the code and serial monitor? Try resetting the board, or closing then re-opening serial monitor. Often, random characters appear when I first open serial monitor. These disappear when I re-open it.

Onions.

millis() returns a large number (float/double), or? ascii is 7bit.

try

Serial.println(millis(), DEC); // DEC to convert it to an Number

Doesn't work for me.

Serial.println( (int) millis() );

println can't print float/double. You have to convert it to int.

I confess this isn't a nice solution..

millis() returns a large number (float/double), or?

It returns a long.

print / println should convert ints and longs to an ASCII-decimal. It normally isn't necessary to call it with the optional format parameter.

Without the additional details that Onions is asking for (and/or complete code from the original poster) this is going to be very difficult to debug.

I am running on a Win7 x64 system and using Arduino 022 IDE.

Here’s what just happened using the following code:

 #include "Tlc5940.h"

]#include "math.h"
void setup() {
     Serial.begin(9600);
     Serial.println ("setup start");
     Serial.println (millis());
}
void loop(){
  Serial.print("loop start");
  Serial.println(millis());
  delay (1000);
}

RESULT is fx`f~ actually the line is much longer but this is all I got with copy/paste. Gibberish and all on one line.

The includes are there because they are vital to the main program I’m working with. I also just tried same code without the includes and the results were same as above.

I also just tried with a different UNO board and the results were the same.

NOTE - all of the above was done on a WIN7 x64 platform which is my base system.

NOW - the following is from an XP system in Virtual PC on the same computer.

here’s the code:

#include <math.h>
int ledpinyellow = 3;
int ledpingreen = 5;
int ledpinred = 9;
int currentmillis=0;
int previousmillis=0;
double startmillis=0;
int N=0;
double clockint=50;
double ledstep = -1;
double x=0;
double y1=0;
double y2=0;
double y3=0;
double z1=0;
double z2=0;
double z3=0;
void setup (){
   Serial.begin(9600);
   x=0;
   y1=0;
   Serial.println("millis");
   Serial.println(millis());
   previousmillis=millis();
   currentmillis=millis();
}
void loop (){
  currentmillis=millis();
  while(currentmillis-previousmillis<clockint){
    delay(10);
    currentmillis=millis();
  }
  previousmillis=currentmillis;
ledstep +=1;
x=10* ledstep*(clockint/1000);
 
 if (x >0 & x <9){
   y1= 4*4*x;
   analogWrite(ledpinyellow, y1);
   analogWrite(ledpingreen,255);
 }
 if (x>=9 & x<=10 ){
   analogWrite(ledpingreen,0);
   analogWrite(ledpinyellow,y1);
 }  
 if(x>10  & x<= 20.5){
   y1=4.7*50*sin(.15*x+.1)+18;
   analogWrite(ledpinyellow,y1);
    }
 
 
 
  Serial.println("x"); 
 Serial.println(x);
Serial.println(y1);
Serial.println(y2);
Serial.println(y3);

if (ledstep >=50){
ledstep = -1;
Serial.println("time");
Serial.println(millis()-startmillis);
 
startmillis=millis();
}
}

and the result is:

millis
6
millis
6
x
0.00
0.00
0.00
0.00
x
0.50
and it goes on

so this result looks very good and is what is expected.

However, when I load my main program - here’s the first several lines of setup -
note: I have attached the complete program in a file.

void setup() {

      Serial.begin(9600);
      
	Tlc.init();
	Tlc.clear();
	Tlc.update();
 //      delay(1000);
    Serial.println ("setup start");
    Serial.println (millis());
	//**************************  wakeup  begins  **********************
	   x=0;
   y1=0;
    prevmillis=millis();
   curmillis=millis();
     while(ledstepint<50){
  curmillis=millis();
  while(curmillis-prevmillis<clockintdbl){
    delay(10);
    curmillis=millis();
  }
  prevmillis=curmillis;

ledstepint +=1;
x=10* ledstepint*(clockintdbl/1000);
///////////////////////////////////////////////////////////////////////// 
 if (x >0 & x <9){
   y1= 4*4*x;
     }
 if (x>=9 & x<=10 ){
    y1=255;
    }  
 if(x>10  & x<= 20.5){
   y1=4.7*50*sin(.15*x+.1)+18;
    }
/////////////////////////////////////////////////////////////////////////    
    brt[ledstepint]=y1;
//  	Serial.println(y1);
  } 
 ledstepint=-1;
// delay (2000);
 
 Serial.println ("setup ends");
 Serial.println (millis());
	//***********

THE RESULT is blank - nothing at all. This only thing that changed here is the program I loaded into the UNO.

This is driving me nuts and wasting a lot of time - would very much appreciate help.

I just saw a few posts that came in as I was preparing this reply. Before my current problems, I have used Serial.println (millis()) many times with no problems at all.

LS1cAA.pde (16.7 KB)

When posting code, please wrap them in the "Code" tags (the "#" in the editor's tool bar.) Please edit your post so that your code is easily readable from your results.

The code you posted doesn't seem to match what is in the .pde you attached. Just a note on that code, I did a quick count and before code even starts to run you have allocated 1218bytes of RAM. That's a little over half of the ATmega328's available RAM. Strings, like those used in your Serial.printlns() can add up fast.

Quote millis() returns a large number (float/double), or? It returns a long.

sp. "unsigned long"

apfelmann:
Doesn’t work for me.

Serial.println( (int) millis() );

println can’t print float/double. You have to convert it to int.

I confess this isn’t a nice solution…

crikey ! how the hell do you send a number that has a decimal point ie 1.1234567890 ?

println can't print float/double

This is utter bolleaux. The "print" method even allows you to specify the number of decimal places* to print.

(*however, since "float" == "double" for the Arduino, this isn't many)

I confess this isn't a nice solution..

As my old teacher used to say "if you're not part of the solution, you're part of the precipitate"

Not the issue here

if (x >0 & x <9){
   y1= 4*4*x;
     }
 if (x>=9 & x<=10 ){
    y1=255;
    }  
 if(x>10  & x<= 20.5){
   y1=4.7*50*sin(.15*x+.1)+18;
    }

could be more efficient

if (x >0 & x <9) {
   y1= 4*4*x;
     }
else if (x<=10 ){
    y1=255;
    }  
else if (x<= 20.5){
   y1=4.7*50*sin(.15*x+.1)+18;
    }

The second code prevents some double testing and is faster.

Because there is only one statement in the if’s the brackets may be ommited but that is a question of style & taste

if (x >0 & x <9) y1= 4*4*x;
else if (x<=10 ) y1=255;
else if (x<= 20.5) y1=4.7*50*sin(.15*x+.1)+18;

Good people with good ideas but we're off topic. Please re-read the top posts, I really need help with this.

Did you try moving 'Serial.begin(9600);' after the Tlc calls?

Good people with good ideas but we're off topic. Please re-read the top posts, I really need help with this.

Please address the issues I asked you to fix with your previous post (which was before the off-topic discussion.)

Did you try moving 'Serial.begin(9600);' after the Tlc calls?

Or printing some text before the TLC calls?

I did the update as you requested.

When all else fails, I take the minimal approach. What does this sketch do if you run it?

void setup(void) {
    Serial.begin(9600);
}

void loop(void) {
    Serial.print("At the tone the time will be ");
    Serial.print(millis(), DEC);
    Serial.println(" ms");
    delay(1000);
}

First, I cannot compile your code as I don't have the TLC lib.

PROBLEM ANALYSIS

There is a serious issue with the amount of RAM used. Looking at the code I see many large arrays like

int brt [51]; int list1 [100]; int list2 [100]; int row1 [16]={2,13,7,10,1,9,3,16,12,14,6,11,5,4,8,15}; int row2 [16]={15,1,3,10,2,8,14,12,13,16,7,6,11,9,5,4}; int row3 [16]={13,5,16,8,15,3,9,10,4,6,7,14,1,12,2,11}; int row4 [16]={13,14,1,15,2,9,7,10,5,11,3,8,12,16,6,4}; int row5 [16]={4,7,13,8,12,14,1,10,16,6,2,3,5,15,11,9}; int col1 [15]={5,2,3,1,4,5,2,1,4,3,4,5,2,3,1}; int cpin [5] [16] int mrec [50] [15]

51 + 100 + 100 + 80 + 15 + 80 + 750 = 1176 x 2 bytes = 2352 bytes. Then there are I estimate 100 Bytes non array var's so about 2.5KB var's where there is only room for 2K. Furthermore all strings will be allocated in RAM so the program is severely running out of RAM (UNO has only 2K)

PROPOSED SOLUTION

The values in all these large lists are only 8 bit as far as I can see, so you can conserve 50% of the memory usage by changing the type to

uint8_t instead of int (int = 2 bytes, uint8_t = 1 byte)

This will free 1176 bytes bringing the amount of RAM used to approx 1400Bytes, well below the 2KB limit.

As I cannot compile the code I do not know the impact of the TLC library and the function calls in detail. ,

Please give it a try, Rob

Running the code shown in reply #17, produces a string of characters that look like gibberish, no normal letters or numbers. The length of the line of gibberish looks to be the same as the expected result.

I'm wondering if the problem might have something to do with the equivalent of a "code page" such as we have used for many years with main frames and PCs alike. I poked around in the Arduino -022 files but didn't find anything.

In the meantime, I will delete my Arduino IDE and library and load a fresh one in case it is corrupted in some way.