user11545: I have the following snippet of code:
const unsigned long fiveMinutes = 5 * 60 * 1000UL;
unsigned long lastCheck = 0 - fiveMinutes;
unsigned long now = 0;

void loop() {
  now = millis();
  if ( now - lastCheck >= fiveMinutes ) {
    doStuff();
    lastCheck = now;
  }
}

The doStuff() routine is getting called on every loop() iteration, because the value of lastCheck isn't getting updated. I thought Icould update global variables inside subroutines, but the behavior I'm seeing says otherwise. What am I doing wrong? (doStuff() is defined and works correctly, I'm not posting it here for brevity.)

user10794: I modified your code to produce a working example:
const unsigned long fiveMinutes = 20 * 1000UL;
unsigned long lastCheck = 0 - fiveMinutes;
unsigned long now = 0;

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  Serial.println ("Starting.");
  Serial.println (fiveMinutes);
  }  // end of setup

void doStuff ()
  {
  Serial.println ("Doing stuff");
  }    

void loop() {
  now = millis();
  if ( now - lastCheck >= fiveMinutes ) {
    doStuff();
    lastCheck = now;
  }
}

I reduced the interval to 20 seconds, and Ignacio Vazquez-Abrams is correct about the initial test, however apart from that, I see "Doing stuff" every 20 seconds.


I thought I could update global variables inside subroutines ...

You can.

Are you sure you didn't have:
void loop() {
  unsigned long lastCheck;
...


11545: <p>I have the following snippet of code:</p>

<pre><code>const unsigned long fiveMinutes = 5 * 60 * 1000UL;
unsigned long lastCheck = 0 - fiveMinutes;
unsigned long now = 0;

void loop() {
  now = millis();
  if ( now - lastCheck &gt;= fiveMinutes ) {
    doStuff();
    lastCheck = now;
  }
}
</code></pre>

<p>The <code>doStuff()</code> routine is getting called on every <code>loop()</code> iteration, because the value of <code>lastCheck</code> isn't getting updated. I thought Icould update global variables inside subroutines, but the behavior I'm seeing says otherwise. What am I doing wrong? (<code>doStuff()</code> is defined and works correctly, I'm not posting it here for brevity.)</p>
 10794: <p>I modified your code to produce a working example:</p>

<pre><code>const unsigned long fiveMinutes = 20 * 1000UL;
unsigned long lastCheck = 0 - fiveMinutes;
unsigned long now = 0;

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  Serial.println ("Starting.");
  Serial.println (fiveMinutes);
  }  // end of setup

void doStuff ()
  {
  Serial.println ("Doing stuff");
  }    

void loop() {
  now = millis();
  if ( now - lastCheck &gt;= fiveMinutes ) {
    doStuff();
    lastCheck = now;
  }
}
</code></pre>

<p>I reduced the interval to 20 seconds, and Ignacio Vazquez-Abrams is correct about the initial test, however apart from that, I see "Doing stuff" every 20 seconds.</p>

<hr>

<blockquote>
  <p>I thought I could update global variables inside subroutines ...</p>
</blockquote>

<p>You can.</p>

<hr>

<p>Are you sure you didn't have:</p>

<pre><code>void loop() {
  unsigned long lastCheck;
...
</code></pre>
