A timer in a function

I am making a function with multiple results and in this function a need a timer.

I can't get it to work so I stipt my function to just a blinker. Even this I can't get it to work

Can someone help me?

With regards,
Edwin

******************************************************************************************
#define PinLed  6

bool xBlinkOn;

void Test();

void setup() {
  pinMode(PinLed, OUTPUT);
}

void loop() {
  Test(millis(), &xBlinkOn);

  digitalWrite(PinLed, xBlinkOn);
}

void Test(unsigned long time , bool* xOutput){
  unsigned long timeBlink;
  bool xInit; 

  if (xInit== 0)
  {
    timeBlink = time;
    xInit = 1;
  }

  if ( time - timeBlink >= 500) 
  {
    *xOutput = !*xOutput;
    timeBlink = time;
  }
} //Test

You should initialize local variables.

bool xInit;

if (xInit== 0)

Do you understand how this TIMER snippet works ?


  //**********************************************                h e a r t b e a t   T I M E R
  //is it time to toggle the heartbeatLED ?
  if (millis() - heartbeatTime >= 500ul)
  {
    //restart this TIMER
    heartbeatTime = millis();

    //toggle LED
    digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
  }

won't this turn the LED off (or on) immediately after it is turned on (or off)

try this

const byte PinLed =  LED_BUILTIN;

bool state;
unsigned long msec0;

void loop ()
{
    unsigned long msec = millis ();
    if (msec - msec0 >= 500)  {
        msec0 = msec;
        state = !state;
        digitalWrite (PinLed, state);
    }
}

void setup ()
{
    pinMode (PinLed, OUTPUT);
}

Use 'static' variable to initialize and preserve its value through separate function calls.

#define PinLed  21

bool xBlinkOn;

void Test();

void setup() {
  Serial.begin (115200);
  pinMode(PinLed, OUTPUT | INPUT);
  xBlinkOn = digitalRead(PinLed); // or 0 or 1
}

void loop() {
  Test(millis(), &xBlinkOn);

  digitalWrite(PinLed, xBlinkOn);
}

void Test(unsigned long time , bool* xOutput){
  static unsigned long lastTimeBlink = 0;

  if ( time - lastTimeBlink >= 500) 
  {
    *xOutput = !*xOutput; 
    Serial.println (*xOutput);
    lastTimeBlink = time;
  }
} //Test


Yes. But @rainman has used a pointer to bool in the parameter list for the function, as if she intends to use the function one day for multiple blinking <whatevers>, which won't work because

void Test(unsigned long time , bool* xOutput){
  static unsigned long lastTimeBlink = 0;

There is but one place to store the time of the last blink.

Or maybe that's what is desired, seems odd.

A solution would be to add a parameter for an unsigned long call by reference variable that would play a roll unique to the bool being time toggled.

a7

Of course. I just wanted to simplify the code a little. There is absolutely no reason why xIniti could not be static as well.

#define PinLed  21

bool xBlinkOn;

void Test();

void setup() {
  Serial.begin (115200);
  pinMode(PinLed, OUTPUT | INPUT);
  xBlinkOn = digitalRead(PinLed); // or 0 or 1
}

void loop() {
  Test(millis(), &xBlinkOn);

  digitalWrite(PinLed, xBlinkOn);
}

void Test(unsigned long time , bool* xOutput){
  static unsigned long lastTimeBlink = 0;
  static bool xInit = false; 

  if (xInit == false)
  {
    lastTimeBlink = time;
    xInit = true;
  }  

  if ( time - lastTimeBlink >= 500) 
  {
    *xOutput = !*xOutput; 
    Serial.println (*xOutput);
    lastTimeBlink = time;
  }
} //Test

I'm not quite sure if this is what your question was.

Here you don't need to say false, static variables get initialised.

But this

bool xInit; 

  if (xInit== 0)
  {

is very asking for trouble. It invokes undefined behaviour, which goes way beyond what you might think, that the variable would necessarily have one, or the other, value.

How I know - learned the hard way


a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.