Go Down

Topic: Weird Number Appears on Serial.println() (Read 418 times) previous topic - next topic

MathematicsFanatic

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);
  }
}

Techone

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

MathematicsFanatic

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: [Select]

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);
  }
}

Coding Badly

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


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

Code: [Select]
   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: [Select]
   Serial.println((current - previous - interval),DEC);
   previous = current;


robtillaart

try
Code: [Select]

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);
  }
}
Rob Tillaart

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

Go Up