1

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);
}

enter image description here

3 Answers 3

2

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.

6
  • each time the function is called pMillis is set to 0 - it's actually undefined, not zero.
    – Nick Gammon
    Jun 5, 2016 at 6:11
  • @NickGammon thank you that is a bit more accurate, but it is still loosing whatever value it was before.
    – RSM
    Jun 5, 2016 at 6:14
  • Absolutely. I just wanted to clarify that you couldn't rely upon that declaration having an initial value of zero. In fact, if you make it static then it will have an initial value of zero, which might help.
    – Nick Gammon
    Jun 5, 2016 at 6:19
  • @NickGammon thanks Nick. I changed it to be clearer/accurate.
    – RSM
    Jun 5, 2016 at 6:21
  • It seems you still did not grasp the idea of pMillis being undefined. By writing “that means the if (cMillis - pMillis > interval) will always be true.” you are are making implicit assumptions on pMillis. I edited the answer to make clear you cannot know the result of the test. Jun 5, 2016 at 7:30
0

Put it out, so it will be global. LedStaye should be bool: const int LED2 = 2; bool ledState2 = LOW; unsigned long pMillis;

void timeCheck(int interval) { if (millis() - pMillis > interval) { pMillis = millis(); ...

1
  • 1
    Did you test this? I do not think millis() can be called inside the if logical test
    – MichaelT
    Dec 29, 2018 at 22:31
0

You have used pMillis variable locally. Thats why when you call the function, pMillis initialized as 0. So set pMillis variable globally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.