Creating a Timer in arduino code and making it run from 1 to 30 seconds

Hi everyone . I need some help in writing a Arduino code where I need a timer which starts from 1 to 30 seconds and after 30 seconds it should get stopped and when i start from first it should start newly. Can anyone help with the code i couldn't write and I'm stuck up badly.

Here's the sample which i have written
Start=milli();
End=milli();
End-Start = 30
F=1 (ENABLING FLAG)

How should I write the code??

Thank you.


unsigned long msecPeriod;
unsigned long msecLst;
char buf [80];

void
loop (void)
{
    unsigned long msec = millis ();
    if (msecPeriod && msec - msecLst >= msecPeriod)  {
        msecLst = msec;
        Serial.println ("timer expired");
    }

    if (Serial.available ())  {
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';
        msecPeriod = atoi(buf);
    }
}

void
setup (void)
{
    Serial.begin (9600);
}

Thank you helping me out!!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.