Blink fuction with return does not blink, but is always true

Hello,

I am learning to use fucntion, but I am stuck.

/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GENERAL
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/


/***********************************/ 
/*  LIBRARIES
/***********************************/


/***********************************/ 
/*  I/O PIN TOEWIJZING
/***********************************/ 


/***********************************/ 
/*  PARAMETERS
/***********************************/


/***********************************/ 
/*  SETTINGS
/***********************************/


/***********************************/ 
/*  GLOBAL VARIABLES
/***********************************/



// long:
unsigned long currentMillis;
unsigned long tmr_Blink;


/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
void setup() 
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
{

/***********************************/ 
/*  SET INPUTS
/***********************************/


/***********************************/ 
/*  SET OUTPUTS
/***********************************/
  pinMode(LED_BUILTIN, OUTPUT);                         // Configureer outps als Onboard led (pin 13) , ten behoeve van status.

} //end setup()


/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
//  FUNCIONS
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

  /***********************************/ 
  /*  Blink 500ms
  /***********************************/
  bool xBlink500ms(unsigned long currentMillis )
  {
     bool bBlink;
     if (currentMillis > tmr_Blink + 500)
     {
       tmr_Blink = currentMillis;
       bBlink = !bBlink;
       return bBlink;
     } 
  } //end function Blink 500ms

// end FUNCTIONS



/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
void loop() /*Hoofd-programma*/
/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
{
  currentMillis = millis();


  /***********************************/ 
  /*  WRITE OUTPUTS
  /***********************************/
  digitalWrite(LED_BUILTIN,xBlink500ms(currentMillis));                   // Zet de ingebouwde LED aan
  
} //end loop()

Why does it not blink. Did I do something wromg?

With regards, Edwin

You are declaring bBlink every time you call the function so it will get a new random value. Give it an initial value and declare it static to prevent the declaration taking place again or declare it as a global variable

Oke, tanx.

How do you make it static?

Linke this

 static bool xBlink500ms(unsigned long currentMillis )
  {
     static bool bBlink;
     if (currentMillis > tmr_Blink + 500)
     {
       tmr_Blink = currentMillis;
       
       bBlink = !bBlink;
       
       return bBlink;
     } 
  } //end function Blink 500ms

It does not work

The ‘state’ to be able to blink a led with millis() can be static (inside the function), but also a global variable.
The Blink Without Delay example uses a global ‘ledState’.

Can you fix the sketch step-by-step ?

Never add something to a value that has to do with millis(). Never compare millis() with a value.
See the Blink Without Delay example how to make a timer with millis().

Your function xBlink500ms() returns a ‘bool’, which is true or false.
The digitalWrite() wants a HIGH or LOW, not a true or false.
Other do that as well, but I prefer that you fix that.

There are two variables called ‘currentMillis’.
Do you know when the compiler uses one and when the other ?

The best way to solve this is to start with the Blink Without Delay example.
Then try to add your function xBlink500ms() step by step until it does not work anymore, then you know that you have made a mistake.

When you change your sketch, please show the full sketch again. That way we can try the sketch ourself.

Your function does not always return a value; if you set compiler warnings to ALL in file->preferences, you will get below warning

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_619114\sketch_apr19a.ino: In function 'bool xBlink500ms(long unsigned int)':

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_619114\sketch_apr19a.ino:42:1: warning: control reaches end of non-void function [-Wreturn-type]

 } //end function Blink 500ms

Your return bBlink; should not be inside the the if block.

Yes, I just to post that I found the problem. It was indeed the return value.

How do can I set this topic "Solved"

Under your opening post is a link ‘more’. Use more → modify and edit the title. Put something like [solved] in front of it.