title: 2 timers arduino [ 2x millis() ]
body: <p>I'm having a problem with this code. I know what the problem is, but I can't find a good solution.</p>
  
  <p>I have two functions using <code>millis()</code> - <code>clock()</code> and <code>time()</code>.</p>
  
  <p>The function <code>clock()</code> returns date and time and the function <code>time()</code> returns the time that another action is ON.</p>
  
  <p>The problem is that I'm not able to store the time that the <code>time()</code> function is counting. When I call the function 2 times the time starts on 0:00.</p>
  
  <p>I know it's because I'm overwriting the variable every time I call the loop, but I can't find a good solution for it. It seems stupid, but I have this problem for a while.</p>
  
  <p>PS: I would like to have the time information every loop.</p>
  
  <hr>
  
  <p>Expected results:</p>
  
  <pre><code>Start
  
  2s: press 1 - clock = 2s, time = 0s.
  5s: press 2 - clock = 5s, time = 3s.
  15s: press 1 - clock = 15s, time = 3s.
  20s: press 2 - clock = 20s, time = 8s.
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>void clock() {
    // ... function that returns date and time;
    // ... using millis();
  }
  void time(){
    if (first == 0){
      sec_aux = 0;
      min_aux = 0;
      UtlTime = millis();
      first = -1;
    }
    if(millis() - UtlTime &lt; 0){
      UtlTime = millis();
    }
    else{
      sec_aux = int((millis() - UtlTime) / 1000);
    }
    if(sec_aux &gt; 59){
      sec_aux = 0;
      min_aux++;
      UtlTime = millis(); 
    }
    sec = sec_aux;
    min = min_aux;
    Serial.println("TIME");
    Serial.print(min); Serial.print(":"); Serial.println(sec);
  }
  
  void loop() {
    if (Serial.available()) {
      inChar = Serial.read();
    }
    switch(inChar){
      case '1':
        time();
        clock();
        break;
      case '2':
        clock();
        first = 0;
        break;
      default:
        clock();
    }
  }
  </code></pre>