Pages: [1]   Go Down
Author Topic: Help with Understanding code, getting to know millis()  (Read 769 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I have written this program (below) that puts in a random pause before a function is called, and it appears to work. However I cannot say how it works. I thought millis() "Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days." http://arduino.cc/en/Reference/millis

So why do I see minus numbers on the output? such as the following:
Code:
pause for 3 seconds
pause: -32763 = clock: 29773 + (pause * 1000): 3000
:: doing something

pause for 4 seconds
pause: -31231 = clock: 30305 + (pause * 1000): 4000
:: doing something

I appear to be adding two positive numbers and getting a negative

Code:
const int PAUSE[] = {2,6}; // a pause of between 2 and 6 seconds
const int LED = 13;

boolean pWait = false;
boolean doThing = false;
unsigned long presentMillis = 0;
int pause = 0;

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
}

void loop() {
 
  unsigned long cMillis = millis();
  int clock = cMillis - presentMillis;

  if (pWait == false) {
    pause = random(PAUSE[0],PAUSE[1]);

    Serial.print("pause for ");
    Serial.print(pause);
    Serial.println(" seconds");

    int tmpP = pause;
    pause = clock + (pause * 1000);

    Serial.print("pause: ");
    Serial.print(pause);
    Serial.print(" = clock: ");
    Serial.print(clock);
    Serial.print(" + (pause * 1000): ");
    Serial.println(tmpP * 1000);

    pWait = true;
    presentMillis = millis();
  }

  if (clock > pause) {
    digitalWrite(LED, LOW); 
    doThing = true;
  }
  else {   
    digitalWrite(LED, HIGH);
    doThing = false;
  } 

  if (doThing == true) {
    Serial.println(":: doing something\n");
    delay(500);   // pretend to be doing something
    pWait = false;
  }

}

Thanks
Karl
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3643
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

tmpP is an int (16 bits). You're seeing the results of overflow. Use unsigned long instead.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why is the previous time something happened called presentMillis?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why is the previous time something happened called presentMillis?

When the measurement was taken, it was the present. smiley
Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why is the previous time something happened called presentMillis?

When the measurement was taken, it was the present. smiley

But only for 1 millisecond.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The present is always ephemeral like that. It becomes the past, before you even know it. smiley-wink
Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My next Arduino project is going to be a time machine. I'm just waiting for the flux capacitors to arrive from China.  smiley-cool
Logged

Offline Offline
Sr. Member
****
Karma: 8
Posts: 250
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

uuuuuuu, i better go and clean my DeLorean!    smiley-cool
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm just waiting for the flux capacitors to arrive from China.
Hope you got the big ones...  smiley-cool
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i better go and clean my DeLorean!
Did you get the model with the snow tires?  smiley-cool
Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm just waiting for the flux capacitors to arrive from China.
Hope you got the big ones...  smiley-cool
I could only afford the small ones, but they will get me far enough into the future to see what next weeks winning lottery numbers are.  smiley-cool
Logged

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

Thanks for the replies, I've made the amendment.

As it happens, I did have a couple of flux capacitors once, manufactured in China. I got them of eBay, But they leaked, very poor build quality.

Karl.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What, did you end up in the past? We've all been there.
Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What, did you end up in the past? We've all been there.
Deja vu ain't what it used to be!
Logged

Pages: [1]   Go Up
Jump to: