invalid use of non-static member function Error

Hi guys, i’ve been working on a movil robot for a while and I recently got this error message.

exit status 1
invalid use of non-static member function ‘void encoder::ISR_count1()’

My code is the following:

#include “Arduino.h”

#include <TimerOne.h>

class encoder {

public:
byte MOTOR1; // Motor 1 Interrupt Pin - INT 0
byte MOTOR2; // Motor 2 Interrupt Pin - INT 1
int counter2 = 0;
int counter1 = 0;
float diskslots; //
String s;

void setEntradas (byte m1, byte m2, float d){ // Set las entradas de los PIN
MOTOR1 = m1;
MOTOR2 = m2;
diskslots = d;
}

// Motor 1 pulse count ISR
void ISR_count1 ()
{
counter1++; // counter Motor 1 + 1
}

// Motor 2 pulse count ISR
void ISR_count2 ()
{
counter2++; // counter Motor 2 + 1
}

void setUpEntradas(){

Timer1.initialize(1000000); // set timer = 1seg
attachInterrupt(digitalPinToInterrupt (MOTOR1) ,ISR_count1, RISING); //
attachInterrupt(digitalPinToInterrupt (MOTOR2), ISR_count2, RISING); //
Timer1.attachInterrupt(ISR_timerone()); // habilitar el timer
}

// TimerOne ISR
int ISR_timerone()
{
Timer1.detachInterrupt(); // Stop the timer
float rotation1 = (counter1 / diskslots) * 60.00; //
counter1 = 0; //
float rotation2 = (counter2 / diskslots) * 60.00; //
counter2 = 0; // contador vulve a empezar
s = "Motor Speed 1: " + (String)rotation1 + " RPM - Motor Speed 2: " + (String)rotation2 + " RPM ";
Serial.println(s);
Timer1.attachInterrupt(ISR_timerone()); // Start the timer
}

};

Please read the advice in the stick topics at the top of the page and follow the advice on posting code

I think you have several issues here.

Declare the methods in the class,
define the methods outside the class.

Then the class definition can be in the .h file, the method code in the .cpp file

ISRs must be plain functions, and cannot be methods, methods are called with an object, the interrupt
system knows nothing about objects, and even if it did how could it tell which object?

Typically you'd have to store an object in a static variable to be able to call a method from an ISR.

You can't safely call Serial methods from inside an ISR

Don't detach and reattach an interrupt inside an ISR.

You cannot embed and ISR within a class, unless the ISR is declared static. If the ISR is declared static, then it will not be able to access non-static members of the class.

In many cases, this is not a problem since, as long as you only ever create ONE instance of the class, and declare ALL class member data and functions as static, it will all work just fine.

There are a few other ways of accomplishing the same thing, but this is by far the simplest approach to udnerstand, especially for beginners.

Regards,
Ray L.

RayLivingston:
You cannot embed and ISR within a class, unless the ISR is declared static. If the ISR is declared static, then it will not be able to access non-static members of the class.

Sure it can. It just needs to be provided with a pointer to the particular instance ahead of time.

gfvalvo:
Sure it can. It just needs to be provided with a pointer to the particular instance ahead of time.

Yes, which is one of the other methods I alluded to, which is likely too obtuse for a newbie. The singleton approach is the simplest to understand and to use, where it works.

Regards,
Ray L.