Value keeps incrementing even though loop has terminated, why? I use a double called delta and use that to turn off an LCD if delta is larger than the timeout threshold. Once that happens, delta is supposed to set to 0 and then other parts of the program run. However, delta continues to increment and I'm not sure why.
This is causing a problem because delta is used to determine if the device should &quot;sleep&quot;. So when I try to wake up the device via button click, delta is always way past the timeout threshold and the device immediately goes back to sleep.
Any thoughts as to why? Thanks in advance.
#include &lt;M5StickC.h&gt;

double BUTTON_PRESS_NEXT_PRESS_TIME= .5; //Time in seconds
double MINIMUM_BUTTON_PRESS_TIME = .05; //Time in seconds
int BUTTON_PRESS_SLEEP = 3; //Time in seconds
int SCREEN_TIMEOUT = 5;
int count;

static int pressCount = 1;
static int nextPress= 1;

static bool buttonIsPressed;

void setup(void) {
  buttonIsPressed = false;
  M5.begin();
  M5.Lcd.setRotation(0);
  M5.Lcd.fillScreen(TFT_BLACK);
  M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);

  // Datum is middle centre
  M5.Lcd.setTextDatum(MC_DATUM);
  M5.Axp.ScreenBreath(100);
  
  updateScreen();      
}

void updateScreen() {
   char tmp[12];
  
  // Datum is middle centre
  M5.Lcd.setTextDatum(MC_DATUM);
  M5.Lcd.drawString(dtostrf(pressCount,4,0,tmp), 80,60,6);
  M5.Lcd.drawString(dtostrf(nextPress,3,0,tmp), 80,100,6);
}

static void evaluatePress(double d) {
   // Evaluate press 
  if (d &gt;= BUTTON_PRESS_NEXT_PRESS_TIME) {
    nextPress++;
    pressCount = 1;
        
    if (nextPress == 19) {
      nextPress= 1;
    }
  }
  else if (d &gt;= MINIMUM_BUTTON_PRESS_TIME &amp;&amp; d &lt; BUTTON_PRESS_NEXT_PRESS_TIME) {
    pressCount++;
  }
    
  updateScreen();
}

void sleepLoop() {
  bool bIsPressed = false;
  Serial.println(&quot;Sleep 1&quot;);
  while(1) {
    Serial.println(&quot;Sleep 2&quot;);
    if (digitalRead(M5_BUTTON_HOME) == LOW &amp;&amp; !bIsPressed) {
      bIsPressed = true;
    }

    if (digitalRead(M5_BUTTON_HOME) == HIGH &amp;&amp; bIsPressed){
      Serial.println(&quot;BREAKY BREAKY&quot;);
      M5.Axp.ScreenBreath(100);

      break;
    }
  }
}

void awakeLoop() {  
  double delta;
  double old_time;
  double current_time;

  Serial.println(&quot;Awake 1&quot;);
  
  while(1) {
    Serial.println(&quot;Awake 2&quot;);
     //Calculate delta time
    old_time = current_time;
    current_time =  millis();
    delta = delta + (current_time - old_time)/1000;

    if (digitalRead(M5_BUTTON_HOME) == LOW &amp;&amp; !buttonIsPressed) {
      buttonIsPressed = true; 
      delta = 0;
    }

    if(digitalRead(M5_BUTTON_HOME) == HIGH &amp;&amp; buttonIsPressed) {
      buttonIsPressed = false;
      evaluatePress(delta);
      delta=0;
    }
 
    if(delta &gt; SCREEN_TIMEOUT &amp;&amp; !buttonIsPressed) {
      Serial.println(delta);
      Serial.print(&quot;BREAK AWAKE&quot;);
      Serial.println(M5.Axp.ScreenBreath(0));
      count = 0;
      break;
    }
    else {
      count++;
    }
  }
  
  delta = 0;
}

void loop() {
  awakeLoop();
  sleepLoop();
}
Here:
void awakeLoop(){  
  double delta;
  // The rest of the function body...
  delta = 0;
}

delta is being defined as a variable local to the function awakeLoop()
and with automatic storage. This means the variable is created whenever
the program enters the function, is not implicitly initialized, and is
destroyed when the function returns.
The last statement delta = 0; has no effect, as it is setting a
variable that is going to be destroyed right away. The compiler most
likely got rid of it.
You most likely want the variable to retain its value across invocations
of the function. For this to be the case, it has to be either global
(defined outside the function) or local with the static qualifier. In
both cases, it will have static storage, i.e. its lifetime will be the
whole program run. Also, if you fail to explicitly initialize it, the
compiler will implicitly initialize it to zero.