Pages: [1]   Go Down
Author Topic: Confuzzled about Serial.print() behavior  (Read 4666 times)
0 Members and 1 Guest are viewing this topic.
Memphis, TN, USA
Offline Offline
Newbie
*
Karma: 3
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay...I have been trying to give myself a crash course in serial communication was going really well until now), and I came across this little tidbit that makes no sense.  I know C and C++, both of which completely ignore unnecessary spacing unless the situation deems it appropriate (EG when calling an API in C++ or inside the quotation marks on a printf() command in C), and I know that Arduino is based off of Wiring, which is based off of C, which makes me scratch my head and wonder where I went wrong.

My code:
Quote
/*
 * eh, math...
 */
 
 int a=5;
 int b=10;
 int c=20;
 
 void setup()
 {
   Serial.begin(9600);
   Serial.println("a = ");
   Serial.print(a);
   Serial.println("b = ");
   Serial.print(b);
   Serial.println("c = ");
   Serial.print(c);
   Serial.println("a+b = ");
   Serial.print(a+b);
   Serial.println("a*c = ");
   Serial.print(a*c);
   Serial.println("c/b = ");
   Serial.print(c/b);
   Serial.println("b-c = ");
   Serial.print(b-c);
 }
 void loop()
 {
   
 }


The serial output:
Quote
a =
5b =
10c =
20a+b =
15a*c =
100c/b =
2b-c =
-10

Now, for the confusing thing - this is the code copied and pasted from the site where I got it from (http://www.ladyada.net/learn/arduino/lesson4.html)
Quote
/*
 * eh, math...
 */
/*
 * Math is fun!
 */

int a = 5;
int b = 10;
int c = 20;

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.println("Here is some math: ");

  Serial.print("a = ");
  Serial.println(a);
  Serial.print("b = ");
  Serial.println(b);
  Serial.print("c = ");
  Serial.println(c);

  Serial.print("a + b = ");       // add
  Serial.println(a + b);

  Serial.print("a * c = ");       // multiply
  Serial.println(a * c);
  
  Serial.println("c / b = ");       // divide
  Serial.print(c / b);
  
  Serial.print("b - c = ");       // subtract
  Serial.printl(b - c);
}

void loop()                     // we need this to be here even though its empty
{
}

Aaaand the output...
Quote
Here is some math:
a = 5
b = 10
c = 20
a + b = 15
a * c = 100
c / b =
2b - c =
-10

Now, I know that my version of the code isn't exactly "neat" as far as spacing and such goes, but I don't really see any point as it really doesn't matter all that much because I document well enough, and because like I said earlier, it's C.  It usually doesn't matter all that much.  I just can't understand why it's doing it this time.
Logged

Singapore
Offline Offline
Newbie
*
Karma: 4
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You mixed up printIn with print. it is starting a new line with the number, and continuing the new line with the next print.

Coding late at night? ;D
Logged

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

I'm not sure what your problem here is, but I think you need to look carefully at what 'println' does.
I'd be more worried about how yoir cut and paste works.
« Last Edit: January 23, 2011, 04:12:00 am 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.

Memphis, TN, USA
Offline Offline
Newbie
*
Karma: 3
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You mixed up printIn with print. it is starting a new line with the number, and continuing the new line with the next print.

Coding late at night? Grin

That did it...That syntax had worked perfectly with the "Hello world" program that I wrote, so I made the assumption (which made an ass out of me and a guy named umption) that that was the correct way to do it.  I went back over the material I was studying, and sure enough, Serial.println() is described as "prints with line break".  Forest, trees...You get the picture.  Thanks for the assist.
« Last Edit: January 23, 2011, 03:46:24 pm by vulrath » Logged

Pages: [1]   Go Up
Jump to: