Use library global variable outside the library scope

Hello,

I'm developing a library where I have the following functions:

void IRAM_ATTR _think() //this is an Interruptions function
{
//...
}

void robotOnLine::editSensorPin(uint8_t _SS1, uint8_t _SS2, uint8_t _SS3, uint8_t _SS4, uint8_t _SS5, uint8_t __CLP, uint8_t __Near)
{
  _LLS1 = _SS1;
  _LS2 = _SS2;
  _MS3 = _SS3;
  _RS4 = _SS4;
  _RRS5 = _SS5;
  _Near = __Near;
  _CLP = __CLP;
}

The "_think()" functions is called from a few interruptions. I used the attachInterrupt() function for this, and as a result I can't add the _think() function to the library class.

This is the problem:

void IRAM_ATTR _think(){
//...
a=!digitalRead(_LS2);
//...
}

If I change the _LS2 variable value inside the library scope, the change won't happen when the variable is used in the _think() function.

What's the proper way to fix this?

imagine you had 10 instances of your robotOnLine class.. how do you think the ISR would know which _LS2 amongst the 10 to use ?

if _LS2 is unique and shared amongst all the instances (a class variable) then define it static in the class and allocate it outside the class. it has to be volatile too to be handled correctly

it would have to be declared extern for other modules to use it probably

I did as you said, this is in the header file:

//...
 void IRAM_ATTR _think();

class robotOnLine
{
  public:
  
  volatile static byte _LS2;
  //...
};

extern volatile byte robotOnLine::_LS2 = 33;

The _think() function still can't reach the variable, even if I do the allocation in the .cpp file.

Am I doing anything wrong?

Thanks in advance for your help

Edit: Removed the extern from the function declaration, thanks Power_Broker!

"extern" is for objects, not functions. If you already have a function defined in a header, the rest of your code should be able to use it (provided there's no namespace issues and you have the header included wherever you want to use that function).

byte x = digitalRead(robotOnLine::_LS2);

Regards,
Ray L.

Many thanks RayLivingston!
That solved the problem.
So _think() has a scope for itself I guess? Because it is an ISR?

F1_:
Many thanks RayLivingston!
That solved the problem.
So _think() has a scope for itself I guess? Because it is an ISR?

_think, like anything else, has a scope that is determined by the scope in which it is defined. The "robotOnLine::_LS2" syntax indicates _LS2 is a public, static member of the robotOnLine class. :: is the c++ "scope resolution" operator. Google it.

Regards,
Ray L.