Problem calling function from Timer library in new library

Hey!

My problem is, that I get a compiler error when I try to set up a Timer within a library. Syntax works fine in a normal sketch. What could this be?

Get this error:

/Users/Bobby/Documents/Arduino/libraries/Valve/Valve.cpp: In constructor 'Valve::Valve(unsigned char)':
/Users/Bobby/Documents/Arduino/libraries/Valve/Valve.cpp:10: error: no matching function for call to 'Timer::every(int, )'
/Users/Bobby/Documents/Arduino/libraries/Timer/Timer.h:37: note: candidates are: int8_t Timer::every(long unsigned int, void ()())
/Users/Bobby/Documents/Arduino/libraries/Timer/Timer.h:38: note: int8_t Timer::every(long unsigned int, void (
)(), int)

Library:

////////////////////////////////////////////////////////////////////////////////////////////////

.h fil:

////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef Valve_h
#define Valve_h

#include <Arduino.h>  
#include "Timer.h"

class Valve {

public:

	Valve(unsigned char pin);
	void on();
	void off();
	void update();
	void mlDose();


private:

	Timer _t;
	int _timer;
	unsigned char _pin;
	bool logged;
	bool valveOn;
	unsigned int c;
	unsigned int hold;

};

#endif

////////////////////////////////////////////////////////////////////////////////////////////////

.cpp file:

////////////////////////////////////////////////////////////////////////////////////////////////

#include "Valve.h"


Valve::Valve(unsigned char pin) {

	pinMode(pin, OUTPUT);

	_pin = pin;

	_timer = _t.every(1, mlDose);
	
}

void Valve::on() {
	valveOn = true;
	digitalWrite(_pin, HIGH);
}

void Valve::off() {
	valveOn = false;
	digitalWrite(_pin, LOW);
}

void Valve::update() {

	_t.update();

}

void Valve::mlDose() {

  if(valveOn) {
    logged = false;
    c++;
  }
  else {
    if(!logged) {
      hold = c;
      logged = true;
      Serial.print("Time on (ms): ");
      Serial.println(c);
      c = 0;
    } 
  }

}

Sketch:

#include "Valve.h"
#include "Timer.h"


Valve valve(3);

void setup() {
 
 Serial.begin(9600);

}

void loop() {
  
  valve.update();

  
  if(analogRead(A5) > 500) {
     valve.on();
  }
  else {
     valve.off();
  }
 
  
}

The error message indicates a problem in your cpp file. You didn't post that (edit: properly).

You can't use a non-static method as the function for the Timer class to call.

I'm not sure what you mean with non-static method? :confused:

And btw, I'm using this: GitHub - JChristensen/Timer: A fork of Simon Monk's Arduino Timer library

(.cpp file is in the "Library code" above)

on(), off(), update(), and mlDose() are not static members of the class. Each instance of the class knows how to do those things. The class does not.

When the timer fires, which instance's method should it call? Since you can't tell the timer class that, you can tell it about static methods only. Then, in the static method, YOU must figure out which instance should execute some action, and make that instance execute some action.

Thought that every time I made an instance of the Valve class, a new timer would be attached to it :confused: But apparently it's better to use those libraries separately...?

Thought that every time I made an instance of the Valve class, a new timer would be attached to it

It is. But, look at this code:

	_timer = _t.every(1, mlDose);

When _t determines that enough time has passed, it needs to call a function. You want it to call the mlDose() function. But, which of the possibly hundreds of instances of the mlDose() function?

I don't get why I should have more instances of that function when it belongs to the instance I created. Yes, if I create another instance of the class. But then I would reference from my main sketch to the function using

// instanciating
Valve valve;

// valve on
valve.on()

But _t.every(ms, function_to_call) is doing the job in a normal sketch, and I've tried before to call functions within the library from the library, so I don't understand how this is now different? Please have patience, soon it'll just "click" for me and I understand everything :smiley: Sorry to be such a pain in the ass, and thank you for your help!

I don't get why I should have more instances of that function when it belongs to the instance I created.

There are multiple versions of the mlDraw() method if there are multiple instances of the Valve class. When the timer goes off, it doesn't know which of the (possibly) many mlDraw() functions to call.

I know that you think it should know to call the one that the belongs to the instance that the timer belongs to. But, the timer doesn't KNOW it belongs to an instance of a class. Therefore, it has no idea that it needs to tell the instance that it needs to do something.

That's why the method has to be static. If it is, the timer can tell the class to do something. Now, the class could keep track of all the instances, and ask each instance if it was it's timer that went off. If the instance reported yes, the class could tell the instance to do something.

Could you write some pseudo code to back up what you're telling me? I'm really best at learning from examples :slight_smile:

Timer::every(int, void (&)())'
i m getting this error while compling my code what should i do

rudra_prasad:
Timer::every(int, void (&)())'
i m getting this error while compling my code what should i do

resurrect a 5 year old unrelated post?