Go Down

Topic: Help with Understanding code, getting to know millis() (Read 926 times) previous topic - next topic

karl101

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

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

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

wildbill

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

PaulS

Why is the previous time something happened called presentMillis?

Nick Gammon


Why is the previous time something happened called presentMillis?


When the measurement was taken, it was the present. :)
http://www.gammon.com.au/electronics

Henry_Best



Why is the previous time something happened called presentMillis?


When the measurement was taken, it was the present. :)


But only for 1 millisecond.

Nick Gammon

The present is always ephemeral like that. It becomes the past, before you even know it. ;)
http://www.gammon.com.au/electronics

Henry_Best

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

boguz

uuuuuuu, i better go and clean my DeLorean!    8)

PaulS

Quote
I'm just waiting for the flux capacitors to arrive from China.

Hope you got the big ones...  8)

PaulS

Quote
i better go and clean my DeLorean!

Did you get the model with the snow tires?  8)

Henry_Best


Quote
I'm just waiting for the flux capacitors to arrive from China.

Hope you got the big ones...  8)

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.  8)

karl101

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.

Nick Gammon

What, did you end up in the past? We've all been there.
http://www.gammon.com.au/electronics

Henry_Best


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

Deja vu ain't what it used to be!

Go Up