how to read a library variable value?

Hi
excuse me if my question is a stupid one but I'm really new to arduino and I want to change a variable value from a library and then access it from the sketch.
something like this:

//in my sketch:
int sample
void setup(){
.
.
.
.
}
void loop(){
if(sample==10){
//do something
}
}
//in my library
mylib::mylib{
if(something==true){
sample=10;
}
}

Please help me with a newbie instruction :slight_smile:

where is the library here? I can see a sketch with 'sample' as a global variable. In this case you can modify the value of 'sample' from any functions in your sketch.

The variable must be defined or it have to have a prototype in scope.

int sample; // missing semicolon here, defined as global variable
void setup(){
.
.
.
.

and

extern int sample; // now it should be visible inside of library file
mylib::mylib{
if(something==true){
sample=10;
}
}

But you need probably opposite - defined in library and prototype in sketch. Usually the definitions are in the header file and you have to include into sketch (with #include "header.h" statement).

Budvar comments cleared my confusions.

In your libraries header file you can find scopes, like private and public section.

So what ever goes in public section can be accessed outside of the library.

So what ever goes in public section can be accessed outside of the library.

Public, private, and protected sections are relevant only within a class definition. Even public fields can not be accessed "outside of the library". They can be accessed by using a class instance in a sketch.

PaulS:
They can be accessed by using a class instance in a sketch.

Sorry for not explicitly mentioning it.

Budvar10:
The variable must be defined or it have to have a prototype in scope.

int sample; // missing semicolon here, defined as global variable

void setup(){
.
.
.
.



and


extern int sample; // now it should be visible inside of library file
mylib::mylib{
if(something==true){
sample=10;
}
}



But you need probably opposite - defined in library and prototype in sketch. Usually the definitions are in the header file and you have to include into sketch (with #include "header.h" statement).

Thank you Budvar10 it works :slight_smile: