[Answered] Constant in function?

Is it OK to put constants in functions or should they always be global?

// off delay

void setup()
{
  bitSet(DDRB,5);
  bitSet(PORTD,2); 
}
void loop()
{
  const int offDelay = 5000; // <<<---
  static uint32_t tStart;
  if(!bitRead(PIND,2))
  {
    bitSet(PORTB,5);
    tStart = millis();
  }  
  if(millis() - tStart > offDelay)
    bitWrite(PORTB,5,!bitRead(PIND,2));
}

edgemoron: Is it OK to put constants in functions?

What did the compiler tell you?

Works fine, just wondered if it was considered bad practice or?

Correct scope is always a good choice.

So, it’s OK as long as no other function needs to access it. Am I right?

Correct.

edgemoron: Works fine, just wondered if it was considered bad practice or?

What is considered bad practice is to use the wrong type of variable. It is probably better not to mix the unsigned long returned by millis() with a signed int in a calculation.

TNX all.