Pages: [1]   Go Down
Author Topic: Weird Number Appears on Serial.println()  (Read 347 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the code I uploaded to my Arduino UNO. Can someone please tell me why when I open my serial monitor, the number it reads is a column of 4294966296's instead of a column of 0's?

long previous = 0;
long interval = 1000;

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

void loop()
{
  unsigned long current = millis();
  if(current - previous >= interval)
  {
    previous = current;
    Serial.println(current - previous - interval);
  }
}
Logged

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi MathematicsFanatic

First. Welcome to this forum.

Second. use your mouse to select # <-- mean to place your code in between "code" and use "copy & paste" technique.

Third.   your line :  Serial.println(current - previous - interval); <--- ????
What value you want to display ?  It should be : Serial.println(myvalue, DEC); to display a decimal number. Maybe you want to display a result of the math inside the Serial.println() if that is the case : Serial.println((current-previous-interval),DEC);
Logged

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

Thank you for the welcome. I am in the process of teaching myself programming since I am an entering freshman and not in a Programming class yet. I have no clue of how electricity works so I am basically just playing and hopefully I will learn from there. I made the change to my program like you said. However, I was looking at an example program that was put on this forum and it said what I had should display a column of 0's on the display monitor. That is why I was confused that it gave such a huge number. Also when I made the changes it still displayed the large number. Please tell me what I am doing wrong.
Code:
long previous = 0;
long interval = 1000;

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

void loop()
{
  unsigned long current = millis();
  if(current - previous >= interval)
  {
    previous = current;
    Serial.println((current - previous - interval),DEC);
  }
}
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 176
Posts: 12286
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   previous = current;
    Serial.println((current - previous - interval),DEC);

current and previous are equal so the println becomes...

Code:
   Serial.println((0UL - interval),DEC);

0xFFFFFFFF - 1000 + 1 = 0xFFFFFC18 = 4294966296.  Is 4294966296 the output?

I suspect you meant to reset previous after printing the value...

Code:
   Serial.println((current - previous - interval),DEC);
    previous = current;
Logged

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

try
Code:
long previous = 0;
long interval = 1000;

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

void loop()
{
  unsigned long current = millis();
  if(current - previous >= interval)
  {
    previous = current;
    Serial.print(current,DEC);
    Serial.print(" - ");
    Serial.print(previous,DEC);
    Serial.print(" - ");
    Serial.println(interval,DEC);
  }
}
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: