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