Library: bringing private variables into function as a parameter


SOLVED: You cannot bring a value from your sketch and directly assign it to a library variable with a function. You must define a parameter in the function, and pass that parameter to the variable. However, you can directly assign the library variable from the sketch: Library.Variable = value;


Howdy,

I just got started not too long ago with Arduino and right off the bat, I noticed that relying on the delay function will slow down the scan rate, and cause weird issues. I decided a great way to start learning was to make my own Library function for a timer. The timer was functional, and the library worked, and now I am trying to cut the fat out of my logic. I have an issue I keep going round and round over.

tl;dr: attempting to define variable in private: so that it is accessible to multiple functions. the variable is defined in my sketch when calling the EN(uint) function.

.h file:

/*
  Cole's timer - library for timer function

  EN(uint) - calls timer to start with timer duration enclosed
  DN() - true if elapsed time is greater than preset time
  TT() - true if the timer is currently timing
  RES() - sets PRE to 0 and changes bits to false
*/

#ifndef Timer_h
#define Timer_h

#include "Arduino.h"

class Timer
{
  public:
    unsigned int ACC();
    void EN(unsigned int);
    boolean DN();
    boolean TT();
    void RES();
  private:
    unsigned long _startTime;
    unsigned long _timeTemp;
    unsigned int _timeDur;
    bool _EN;
};

#endif

.cpp file:

#include "Arduino.h"
#include "Timer.h"

void Timer::EN(_timeDur)
{
  if (!_EN && _timeDur > 0)
  {
    _startTime = millis();
    _EN = true;
  }
}

bool Timer::TT()
{
  if (_EN && !DN())
  {
    return true;
  }
  else
  {
    return false;
  }
}

bool Timer::DN()
{
  if (_EN)
  {
    if (millis() > _startTime)
    {
      if (millis() - _startTime > _timeDur)
      {
        return true;
      }
      else
      {
        return false;
      }
    }
    else
    {
      _timeTemp = 4294967295 - _startTime + 1 + millis();
      if (_timeTemp > _timeDur)
      {
        return true;
      }
      else
      {
        return false;
      }
    }
  }
  else
  {
    return false;
  }
}

void Timer::RES()
{
  _EN = false;
}

unsigned int Timer::ACC()
{
  if (_EN && !DN())
  {
    if (millis() > _startTime)
    {
      return (millis() - _startTime);
    }
    else
    {
      return (4294967295 - _startTime + 1 + millis());
    }
  }
  else if (DN())
  {
    return _timeDur;
  }
  else return 0;
}

This logic, makes sense to me, but it does not compile correctly. Initially, I had Timer::EN(unsigned int _PRE) and moved _PRE to _timeDur in the function This was because _PRE would not be accessible to all the functions, _timeDur is. It worked fine.

Compile error:

C:\Program Files (x86)\Arduino\libraries\Timer\Timer.cpp:4:24: error: variable or field 'EN' declared void

 void Timer::EN(_timeDur)

                        ^

C:\Program Files (x86)\Arduino\libraries\Timer\Timer.cpp:4:16: error: '_timeDur' was not declared in this scope

 void Timer::EN(_timeDur)

kamandriat: Initially, I had Timer::EN(unsigned int _PRE) and moved _PRE to _timeDur in the function This was because _PRE would not be accessible to all the functions, _timeDur is. It worked fine.

So why did you change the code to a non-compiling 'I-wish-C++-would allow-that'-syntax?

I was trimming the compile size, while maintaining functionality. Even if I can remove just one int, it would be beneficial.

Plus: it would be nice to learn all the tools I have available.

Can I not assign a library variable with a call function in this way?

Library::Function(library_variable) { }

Or do I have to keep the variable in the function, and move it to another variable in my function?

Library::Function(parameter) { library_variable = parameter; }

You have to use the parameter of the function, and move it's value to a class-variable in your class-function.

class Example { private: int variable;

void method(int parameter) { variable = parameter; } };

So, the way I originally had it, is the leanest I can make the code.

Thanks!