Pages: [1] 2   Go Down
Author Topic: Problems with Serial.println  (Read 1088 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Newcastle, England
Offline Offline
Sr. Member
****
Karma: 3
Posts: 489
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) smiley-grin

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Doesn't work for me.

Code:
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..
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Here's what just happened using the following code:
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:
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.
Code:
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.75 KB - downloaded 5 times.)
« Last Edit: July 07, 2011, 02:02:48 pm by denniscj7 » Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: July 05, 2011, 03:07:27 pm by James C4S » Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26496
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Sr. Member
****
Karma: 4
Posts: 331
Arduino - very interesting !
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Doesn't work for me.

Code:
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 ?

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26496
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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)

Quote
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"
« Last Edit: July 05, 2011, 03:47:21 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13896
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Not the issue here

Code:
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
Code:
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
Code:
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;
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.)
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Pages: [1] 2   Go Up
Jump to: