title: Arduino 'time out' function using a millis timer
body: <p>I've not been programming for long and I just want to expand from electronic engineering with an Arduino UNO board.</p>
  <p>I've started a new project based on the Secret Knock Detecting Door Lock by Steve Hoefer on Grathio and I'd like to implement the following:</p>
  <p>(<a href="http://grathio.com/2009/11/secret_knock_detecting_door_lock/" rel="nofollow noreferrer">http://grathio.com/2009/11/secret_knock_detecting_door_lock/</a>)
  (<a href="http://grathio.com/assets/secret_knock_detector.pde" rel="nofollow noreferrer">http://grathio.com/assets/secret_knock_detector.pde</a>)</p>
  <h3>Implementation</h3>
  <p>If the global value equals 0 and the valid knock patter is true then flash a yellow LED 4 times using millis rather than delay so that it can still 'listen'.</p>
  <p>If another valid knock pattern is not heard within 6 seconds it will time out and reset global to 0 so that it can acknowledge the initial true pattern and flash the yellow LED.</p>
  <p>If another valid  knock pattern is heard withing 6 seconds, increment a counter.</p>
  <p>If the counter equals 1, wait for another valid knock pattern and if true within 6 seconds, increment the counter again and don't flash the yellow LED.</p>
  <p>Otherwise, time out and reset all values.</p>
  <p>And so on until if the counter is greater than or equal to 4 trigger the master LED array.</p>
  <p>Once is gets to 4 successful knocks, I'd like it to trigger the master LED array I've built.</p>
  <h3>Problems</h3>
  <p>This project was inspired by the test panels used on passenger airplanes. I've seen them a lot and thought it would be a good place to start and learn about timing.</p>
  <p>There are a few problems as I don't wish to reset millis() every time and I'm using a button rather than the boolean within the knock detection script so I don't get lost in the code.</p>
  <p>I understand this won't respond 50 seconds later and it's a beginners mistake but proves what I've got if I hold down the button. The code below also doesn't have a time out after the 1st digitalRead HIGH or true boolean (I am struggling with this).</p>
  <h3>Arduino sketch</h3>
  <pre><code>  int inPin = 2;         // input pin switch
      int outPin = 3;       // output pin LED
      
      long currentTime = 0; // counter
      long nextTime = 0; // counter
      long lastTime = 0; // counter
      
      int patternCounter = 0; // build up 
      int globalValue = 0; // lock out
      int breakIn = 0; // waste of time?
      
      void setup()
      {
        pinMode(inPin, INPUT);
        pinMode(outPin, OUTPUT);
        Serial.begin(9600);
        Serial.println(&quot;GO&quot;);
      }
      
      void loop(){
      
        // boolean true, switch just for testing
        if (digitalRead(inPin)==HIGH&amp;&amp;globalValue==0&amp;&amp;breakIn==0) { 
          Serial.println(&quot;CLEARED 1st&quot;);
          delay (500); // flood protection
          globalValue++;
          breakIn++;
          if (globalValue&gt;0&amp;&amp;breakIn&gt;0){ 
            currentTime = millis(); // start a 'new' counter and 'listen'
            if (currentTime&lt;6000) { // less than
              if (digitalRead(inPin)==HIGH) { // and true
                Serial.println(&quot;CLEARED 2nd&quot;); // cleared the stage
                delay (500); // flood protection 
                patternCounter++;
              } // if counter less
            } // if true or high
            if (currentTime&gt;6000) {
              Serial.println(&quot;TIMEOUT waiting 2nd&quot;); // timed out
              globalValue = 0;
              patternCounter = 0;
              breakIn = 0;
            } // if more than
          } // global master
        }
      
        // 3rd attempt
        if (globalValue==1&amp;&amp;patternCounter==1){ // third round
          nextTime = millis(); // start a 'new' counter and 'listen'
          if (nextTime&lt;6000) { // less than
            if (digitalRead(inPin)==HIGH) { // and true
              Serial.println(&quot;CLEARED 3rd&quot;);
              delay (500); // flood protection
              patternCounter++;
            } // if counter less
          } // if true or high
          if (nextTime&gt;6000) {
            Serial.println(&quot;TIMEOUT waiting 3rd&quot;); // timed out
            globalValue = 0;
            patternCounter = 0;
          } // if more than
         } // global master
      
        // 4th attempt and latch
        if (globalValue==1&amp;&amp;patternCounter==2){ // last round 
          lastTime = millis(); // start a 'new' counter and 'listen'
          if (lastTime&lt;6000) { // less than
            if (digitalRead(inPin)==HIGH) { // and true
              digitalWrite(outPin, HIGH); // LED on
              Serial.println(&quot;CLEARED 4th ARRAY&quot;); // cleared the stage
              delay(500); // flood protection
            } // true or high
          } // counter
          if (lastTime&gt;6000) {
            Serial.println(&quot;TIMEOUT waiting 4th&quot;); // timed out
            globalValue = 0;
            patternCounter = 0;
          } // if more than
         } // global and alarm
      
      
         } // loop end  
  </code></pre>
  <p>That's the current sketch, I understand the counters I've used are near pointless.</p>
  <p>Any help would be greatly appreciated!</p>