# Help with Millis && While

**URL:** <https://forum.arduino.cc/t/help-with-millis-while/572423>\
**Category:** General Guidance\
**Created:** [February 6, 2019, 7:38pm UTC](https://forum.arduino.cc/t/help-with-millis-while/572423 "2019-02-06T19:38:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![thieaux](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@thieaux](https://forum.arduino.cc/u/thieaux)\
**Post date:** [February 6, 2019, 7:38pm UTC](https://forum.arduino.cc/t/help-with-millis-while/572423/1 "2019-02-06T19:38:40Z")

</div>

Hey guys, its been a long time since ive done some sketching and ive forgotten most of the tips and tricks... i need some help, recently someone asked me if i can whip up a fingerprint access control and tbh it was kind of a straight forward setting up the hardware but i'm stuck in a bit of coding...

The system is a nano with a gt-511 sensor, npn, 4 channel relay board and couple of resistor no biggie....

What im trying to achieve is to turn the gt-511 sensor ON when capacitive sensor reads a certain value stay on for 5 seconds and then turn off, because the gt-511 gets hot when in use for a long period, i followed the modules suggestion of using touch ic to do this...thats what i did... Everything is working fine except the stay on for 5 seconds when the ic detects a "touch"

#include \<CapacitiveSensor.h\>  
#include "FPS\_GT511C3.h"  
#include "SoftwareSerial.h"

CapacitiveSensor cs\_4\_8 = CapacitiveSensor(8, 2);  
FPS\_GT511C3 fps(5, 6);

void setup()  
{  
cs\_4\_8.set\_CS\_AutocaL\_Millis(0xFFFFFFFF);  
pinMode(11, OUTPUT);  
Serial.begin(9600); //set up Arduino's hardware serial UART  
delay(100);

}  
void loop()  
{  
long sensor1 = cs\_4\_8.capacitiveSensor(50);  
if (sensor1 \>= 1000)  
{  
digitalWrite(11, HIGH);  
delay(100); // settle things down  
Fingerprint();  
}

}//end of void loop

Now what im trying to achieve is that once x is over 1000 keep the pin high for 5 seconds then low ....i know is simple enough, i just dont know how to put together the while && millis bit...

Any help is appreciated  
Thieaux

P.S. anyone knows why im showing Newbie again, my account is quite old and doesnt show my previous messages!!

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [February 6, 2019, 8:23pm UTC](https://forum.arduino.cc/t/help-with-millis-while/572423/2 "2019-02-06T20:23:32Z")

</div>

Since you are an old hand at this, here is a refresher.

> **[Blink Without Delay | Arduino Documentation](https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay)**
>
> Blink an LED without using the delay() function.

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [February 6, 2019, 8:48pm UTC](https://forum.arduino.cc/t/help-with-millis-while/572423/3 "2019-02-06T20:48:59Z")

</div>

The demo [Several Things at a Time](http://forum.arduino.cc/index.php?topic=223286.0) is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at [Using millis() for timing. A beginners guide](http://forum.arduino.cc/index.php?topic=503368.0) if you need more explanation.

...R

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 6, 2019, 8:53pm UTC](https://forum.arduino.cc/t/help-with-millis-while/572423/4 "2019-02-06T20:53:15Z")

</div>

> anyone knows why im showing Newbie again

Because you only registered this account this evening.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 6, 2019, 10:00pm UTC](https://forum.arduino.cc/t/help-with-millis-while/572423/5 "2019-02-06T22:00:42Z")

</div>

> i just dont know how to put together the while && millis bit...

Don't use while. If you do then you might as well use delay(). Instead let loop() do what it's name suggests

---

<div class="post-metadata">

**Author:** ![Blackfin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blackfin/32/238871_2.png) [@Blackfin](https://forum.arduino.cc/u/Blackfin)\
**Post date:** [February 6, 2019, 10:17pm UTC](https://forum.arduino.cc/t/help-with-millis-while/572423/6 "2019-02-06T22:17:36Z")

</div>

A basic framework. Not compiled, not tested....

```cpp
void setup()
{
    cs_4_8.set_CS_AutocaL_Millis(0xFFFFFFFF);
    pinMode(11, OUTPUT);
    Serial.begin(9600); //set up Arduino's hardware serial UART
    delay(100);

}

void FingerprintPowerSM( void )
{
    static unsigned long
        timeFinger = 0;
    static byte
        stateFinger = 0;

    switch( stateFinger )
    {
        case    0:
            //wait for cap sense to read > 1000
            if( cs_4_8.capacitiveSensor(50) > (long)1000 )
            {
                //seen...get the time and move to state 1
                stateFinger = 1;
                timeFinger = millis();
                
            }//if
        break;

        case    1:
            //if cap sensor falls below 1000 in this state return to state 0
            if( cs_4_8.capacitiveSensor(50) <= (long)1000 )
                stateFinger = 0;
            else
            {
                //if we've seen >1000 for 5-seconds, turn the sensor on
                if( (millis() - timeFinger) >= 5000 )
                {
                    //turn on the sensor and get the time
                    digitalWrite(11, HIGH);
                    timeFinger = millis();
                    stateFinger = 3;
                    
                }//if
                
            }//else
        break;

        case    3:
            //roughly the same as delay(100) -- sensor start-up time delay
            if( (millis() - timeFinger) < 100 )
                return;
            else
            {
                //do the reading
                Fingerprint();

                //setup for turn-off
                timeFinger = millis();
                state = 4;
                
            }//else
        break;

        case  4:
            //stay on until 5-sec expires or the cap sense reads <= 1000
            if( ((millis() - timeFinger) >= 5000) || (cs_4_8.capacitiveSensor(50) <= 1000) )
            {
                //sensor off
                digitalWrite(11, LOW);
                
                //set up for minimum off time
                timeFinger = millis();
                stateFinger = 5;
                
            }//if
            
        break;

        case    5:
            //minimum off-time after sensor is turned off
            if( millis() - timeFinger < 500 )  //500mS
                return;
                
            stateFinger = 0;
            
        break;
        
    }//switch
    
}//FingerprintPowerSM

void loop()
{ 
    FingerprintPowerSM();
    
}//end of void loop

```

---

<div class="post-metadata">

**Author:** ![thieaux](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@thieaux](https://forum.arduino.cc/u/thieaux)\
**Post date:** [February 6, 2019, 10:36pm UTC](https://forum.arduino.cc/t/help-with-millis-while/572423/7 "2019-02-06T22:36:58Z")

</div>

> UKHeliBob:  
> Don't use while. If you do then you might as well use delay(). Instead let loop() do what it's name suggests

The things is if i use delay then i cant call the fingerprint program... im having flash backs of using while or interrupts, i have to brush up thanks!

---

<div class="post-metadata">

**Author:** ![thieaux](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@thieaux](https://forum.arduino.cc/u/thieaux)\
**Post date:** [February 6, 2019, 10:42pm UTC](https://forum.arduino.cc/t/help-with-millis-while/572423/8 "2019-02-06T22:42:33Z")

</div>

> Robin2:  
> The demo [Several Things at a Time](http://forum.arduino.cc/index.php?topic=223286.0) is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.
> 
> Have a look at [Using millis() for timing. A beginners guide](http://forum.arduino.cc/index.php?topic=503368.0) if you need more explanation.
> 
> ...R

thanks will do

> AWOL:  
> Because you only registered this account this evening.

The last post i remember :  
[http://forum.arduino.cc/index.php?topic=147806.5](http://forum.arduino.cc/index.php?topic=147806.5)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 6, 2019, 10:45pm UTC](https://forum.arduino.cc/t/help-with-millis-while/572423/9 "2019-02-06T22:45:12Z")

</div>

> thieaux:  
> thanks will do  
> The last post i remember :  
> [Help with iPod board. - Project Guidance - Arduino Forum](http://forum.arduino.cc/index.php?topic=147806.5)

Different (defunct) account.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 7, 2019, 7:54am UTC](https://forum.arduino.cc/t/help-with-millis-while/572423/10 "2019-02-07T07:54:30Z")

</div>

> thieaux:  
> The things is if i use delay then i cant call the fingerprint program...

I am well aware of that but you will have exactly the same problem if you use millis() and while incorrectly

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [February 7, 2019, 12:04pm UTC](https://forum.arduino.cc/t/help-with-millis-while/572423/11 "2019-02-07T12:04:39Z")

</div>

> thieaux:  
> I'm having flash backs of using while or interrupts

If that is the case, better get back on your proper medication. 😲

Neither "while" nor interrupts have any part in your project. 😀

---

<div class="post-metadata">

**Author:** ![thieaux](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@thieaux](https://forum.arduino.cc/u/thieaux)\
**Post date:** [February 7, 2019, 2:44pm UTC](https://forum.arduino.cc/t/help-with-millis-while/572423/12 "2019-02-07T14:44:15Z")

</div>

Thank you @BlackFIn, your comment really help, that type of "project guidance" is what im looking for..as you said "basic framework, not tested" i was not looking for someone to write me the code, just some "guidance...

THANK YOU!!!! for shining a light, i completely forgot about switch ..

@UKhelibob i am not questioning your experience and knowledge.... What i meant was since the touch ic is quite sensitive, sometimes the sensor may read low when the finger is pressed, and since i had an if/else statement the module would turn off even when a finger is pressed hence the use of millis to start a counter once it detected a finger to let module do its thing.....

@AWOL you must be a hoot in person..

@Paul\_\_B you have no idea, just thinking back when i started playing with my due gives me a headache, i bought that thing in 08 or 09 and i destroyed every electronic component in my house scavenging for parts at 3am so i would not have to wait for components ...good times

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 6:49pm UTC](https://forum.arduino.cc/t/help-with-millis-while/572423/13 "2021-05-05T18:49:03Z")

</div>

Closed on May 5, 2021, 6:49pm UTC


