Help with Millis && While

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!!

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

The demo Several Things at a Time 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 if you need more explanation.

...R

anyone knows why im showing Newbie again

Because you only registered this account this evening.

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

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

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

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!

Robin2:
The demo Several Things at a Time 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 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

thieaux:
thanks will do
The last post i remember :
Help with iPod board. - Project Guidance - Arduino Forum

Different (defunct) account.

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

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

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

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

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