user20714: I'm learning how to use the millis() function. Instead of having it in the main body of the program I wanted to place it in a function so I can change the time interval with which an led will flash. I modified the code blinkWithoutDelay. My code is below. The output I get is an LED that is always on. I did print the output of the ledState to the serial monitor. Its changing from on to off repeatedly. So I think the led is really flickering too fast to see. I took a screenshot from the serial monitor. The output is below. 
const int LED2 = 2;
int ledState2 = LOW;

void timeCheck(int interval)
{
    unsigned long cMillis = millis();
    unsigned long pMillis;

    if (cMillis - pMillis > interval)
    {
        pMillis = cMillis;

        if (ledState2 == LOW)
        {
            ledState2 = HIGH;
        }
        else
        {
            ledState2 = LOW;
        }
        digitalWrite(LED2, ledState2);
        Serial.println(ledState2);
    }
}

void setup()
{
    Serial.begin(9600);
    pinMode(LED2, OUTPUT); 
}

void loop()
{
    unsigned long flashtime = 1000;
    timeCheck(flashtime);
}



user2944: The problem here is that the variables you declare in your function are not static. The rest of the code works as expected.
Where you declare the variables at the beginning of your function is correct. There is only one variable in question though as it is the cause of the error you are seeing.
What you need to do is change the variable
unsigned long pMillis;

to
static unsigned long pMillis = 0;

By not having the pMillis as a static variable, each time the function is called the value of pMillis is undefined, which means it is unpredictable whether  (cMillis - pMillis > interval) will be true or false. But with the static declaration the Arduino knows to keep the previous value for the next time you call the function.
An excerpt from Wikipedia:

In computer programming, a static variable is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program....
A static local variable is different from a local variable. It is initialized only once no matter how many times that function in which it resides is called.


20714: <p>I'm learning how to use the millis() function. Instead of having it in the main body of the program I wanted to place it in a function so I can change the time interval with which an led will flash. I modified the code blinkWithoutDelay. My code is below. The output I get is an LED that is always on. I did print the output of the ledState to the serial monitor. Its changing from on to off repeatedly. So I think the led is really flickering too fast to see. I took a screenshot from the serial monitor. The output is below. </p>

<pre><code>const int LED2 = 2;
int ledState2 = LOW;

void timeCheck(int interval)
{
    unsigned long cMillis = millis();
    unsigned long pMillis;

    if (cMillis - pMillis &gt; interval)
    {
        pMillis = cMillis;

        if (ledState2 == LOW)
        {
            ledState2 = HIGH;
        }
        else
        {
            ledState2 = LOW;
        }
        digitalWrite(LED2, ledState2);
        Serial.println(ledState2);
    }
}

void setup()
{
    Serial.begin(9600);
    pinMode(LED2, OUTPUT); 
}

void loop()
{
    unsigned long flashtime = 1000;
    timeCheck(flashtime);
}
</code></pre>

<p><a href="https://i.stack.imgur.com/GSLog.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GSLog.jpg" alt="enter image description here"></a></p>
 2944: <p>The problem here is that the variables you declare in your function are not static. The rest of the code works as expected.</p>
<p>Where you declare the variables at the beginning of your function is correct. There is only one variable in question though as it is the cause of the error you are seeing.</p>
<p>What you need to do is change the variable</p>
<pre class="lang-cpp prettyprint-override"><code>unsigned long pMillis;
</code></pre>
<p>to</p>
<pre class="lang-cpp prettyprint-override"><code>static unsigned long pMillis = 0;
</code></pre>
<p>By not having the <code>pMillis</code> as a static variable, each time the function is called the value of <code>pMillis</code> is undefined, which means it is unpredictable whether <code> (cMillis - pMillis &gt; interval)</code> will be true or false. But with the <code>static</code> declaration the Arduino knows to keep the previous value for the next time you call the function.</p>
<p>An excerpt from <a href="https://en.wikipedia.org/wiki/Static_variable" rel="nofollow noreferrer">Wikipedia</a>:</p>
<blockquote>
<p>In computer programming, a static variable is a variable that has been allocated statically—whose lifetime or &quot;extent&quot; extends across the entire run of the program....</p>
<p>A static local variable is different from a local variable. It is initialized only once no matter how many times that function in which it resides is called.</p>
</blockquote>
