im creating a library, its working just fine, i have a problem in a function, after troubleshooting,
this function contain a statement like this:
_buttonTimer = millis();
and then a condition like this:
if ((millis() - _buttonTimer >1000)
now when i do this and call the function from the library, millis() value are stored in the variable and
cleared in the next loop, so the condition never achieved,
i looked into other library that uses millis(), in the same way, and what he did is calling _buttonTimer
by reference, this is the code:
unsigned int timerOn(unsigned long &timerState, unsigned long timerPeriod) {
if (scanValue == 0) { // timer is disabled
timerState = 0; // Clear timerState (0 = 'not started')
}
else { // Timer is enabled
if (timerState == 0) { // Timer hasn't started counting yet
timerState = millis(); // Set timerState to current time in milliseconds
scanValue = 0; // Result = 'not finished' (0)
}
else { // Timer is active and counting
if (millis() - timerState >= timerPeriod) { // Timer has finished
scanValue = 1; // Result = 'finished' (1)
}
else { // Timer has not finished
scanValue = 0; // Result = 'not finished' (0)
}
}
}
return(scanValue);
he is calling a function from arduino program, how can i do the same, im using a class,
should i reference the variable from the constructor, i would appreciate the help.
you never "call" something by reference, you pass something by reference
I'm not sure I get what you say....
One of the most critical aspects of object-oriented programming allows one to define labels for the data members and member functions, to specify if they are accessible from other classes or not (data members labeled as private can not be directly manipulated by member functions of other classes. In order to be able to manipulate these data members, the creator of the class must provide special member functions, labeled as public)
I you want to be able to read directly from outside an instance of your class the value of a variable then you define that variable in the public section of your class and you can read it directly. if not then you can't directly read or write in that memory, you would need the class to expose methods for doing so.
For example in the code below (just typed it here, so might have some syntax issue) you cannot access directly the _width attribute because I made it private but the _height attribute is visible and so you can call it directly.
class Rectangle {
private:
int _width; // will be hidden
public:
int _height; // is visible from the world
Rectangle () { // default constructor
_width = 10;
_height = 20;
}
Rectangle (int w, int h) {
_width = w;
_height = h;
}
// this is to access the width
int width() {
return _width;
}
// this is to access the height
int height() {
return _height;
}
// this is to calculate the area
int area() {
return _width * _height;
}
};
Rectangle rect(3, 4);
void setup()
{
Serial.begin(115200);
Serial.print(F("Area = "));
Serial.println(rect.area());
// the Height
Serial.print(F("Height = "));
Serial.println(rect.height());
// this works as well since height is public
Serial.print(F("Height = "));
Serial.println(rect._height);
// the Width
Serial.print(F("Width = "));
Serial.println(rect.width());
// but this does not work because width is private, if you uncomment it won't compile
// the compiler will say "error: 'int Rectangle::_width' is private"
// Serial.print(F("Width = "));
// Serial.println(rect._width);
}
void loop() {}