How to pass variable to library?

I'm trying to modify existing library (RF12B) and send to it a variable (boolean) from my sketch. As a simple example, let's say I have

boolean myVar=true;

In the library .cpp file I add:

if (myVar) Serial.prinln "True";

But I get error: RFM12B.cpp:22: error: 'myVar' was not declared in this scope

So how can I pass it to the library's function?

So how can I pass it to the library's function?

Which function in the library? You pass the [u]value[/u] as an argument. You pass the address of the variable as a reference argument. You do not pass a variable anywhere.

The library can't see your sketch's globals. That's why you get the error.

You can add a global to the library, which then your function can see.

Or you could add an additional argument to a library function and pass the boolean in that way.

-br

billroy: The library can't see your sketch's globals. That's why you get the error.

You can add a global to the library, which then your function can see.

Or you could add an additional argument to a library function and pass the boolean in that way.

-br

Thank you. Could you give me an example of how to define global var in library and then address it from the sketch? I added in the .cpp file: uint8_t myVar;

but when I try to assign value to myVar from sketch it tells me "myVar" was no declared in this scope. I do have #include in the beginning...

Could you give me an example of how to define global var in library and then address it from the sketch?

You can't. You need to find another way.

You need to start by providing more details and less hand-waving.

Perhaps looking at some other libraries would be useful.

You also need to declare your new variable in the library's .h file.

As Paul says, you are trying to accomplish A and asking us about C. It would be nice to know A and B.

-br

Ok I’ll try it another way :slight_smile:
Here’s Sketch “A.ino”

#include <B.h> 
void setup() {
  Serial.begin (115200);
}
void loop() {
  if (myVar) Serial.println ("True!")
}

How would I declare “myVar” in B.h/B.cpp so I can access it from “A.ino”?

I’ve tried declaring myVar in B.h,and in B.cpp (as bool myVar;) but I still get error “myVar not declared” :frowning:

For a working example, take a look at bitlash.cpp and bitlash.h here: https://github.com/billroy/bitlash

It would help to see B.cpp and B.h to confirm the syntax is correct etc.

-br

It doesn’t really matter what’s inside .cpp or .h file, I’m just looking for theoretical example how to pass variable between sketch and library without passing it as function argument :slight_smile:
Here’s an example of simple tutorial LED13 library:
LED13.h

#ifndef LED13_H
#define LED13_H

#include "Arduino.h"


class LED13 { 
public:
	LED13();
	~LED13();
    void on();
	void off();
	void blink(int time);
};

#endif

LED13.cpp

#include "LED13.h" //include the declaration for this class

const byte LED_PIN = 13; //use the LED @ Arduino pin 13


//<<constructor>> setup the LED, make pin 13 an OUTPUT
LED13::LED13(){
    pinMode(LED_PIN, OUTPUT); //make that pin an OUTPUT
}

//<<destructor>>
LED13::~LED13(){/*nothing to destruct*/}

//turn the LED on
void LED13::on(){
	digitalWrite(LED_PIN,HIGH); //set the pin HIGH and thus turn LED on
}

//turn the LED off
void LED13::off(){
	digitalWrite(LED_PIN,LOW); //set the pin LOW and thus turn LED off
}

//blink the LED in a period equal to paramterer -time.
void LED13::blink(int time){
	on(); 			//turn LED on
	delay(time/2);  //wait half of the wanted period
	off();			//turn LED off
	delay(time/2);  //wait the last half of the wanted period
}

So in the above example, can someone show me how to declare a variable so I can change it’s value in the main sketch and library will see that it changed?

So in the above example, can someone show me how to declare a variable so I can change it's value in the main sketch and library will see that it changed?

The simplest, and therefore probably best, is to make the variable a public member of the class.

class LED13 { 
public:
    LED13();
    ~LED13();
    void on();
    void off();
    void blink(int time);

    int pinNum; // Add this
};

Then, in the sketch, you can have:

LED13 aLight;

void setup()
{
   aLight.pinNum = 26;
}

void loop()
{
   Serial.print("LED on pin ");
   Serial.print(aLight.pinNum);
   Serial.println(" is in use...");
}

But, it really all depends on what (as-yet-unrevealed) you are trying to do.

Thanks a lot Paul, that's exactly what I was looking for! :) As to revealing what I'm trying to accomplish, well basically (it's kind of complex scenario): I'm using SD card (to play audio via WaveHC library) and RF12B receiver and both are SPI devices. They conflict with each other and I need to disable RF12B when SD card is in use. I can do that by pulling SS pin on receiver to HIGH. However issue is that RF12B uses hardware interrupt and when it's triggered (which happens when chip thinks it received radio signal) library pulls SS pin low, thus killing SD card interface... This interrupt function is part of the RF12B library... At least that's my understanding of how it works. My idea is to add variable "cardinUse" and set to true every time I'm reading SD card and then set it to false after I'm done. I want to add "if" check inside RF12B function that get's triggered by interrupt to exit if (cardinUse). Alternatively I tried using noInterrupts(); but it kills audio and freezes program(looks like waveHC library is also using interrupts). So as I described in OP, I had issues passing this "cardinUse" to the library. Because don't call this function directly (it's triggered by hardware interrupt) I can't pass it as function argument , so I needed to pass variable separately... I can post whole code, but it's enormous, doubt anyone will have fun reading it...

I think even better is to add a method to the library that takes an argument.

void setCardInUse(bool cardIsBeingUsed)
{
   // Store value in private variable...
}

Then, add code that uses that private variable.

The best option you have are to use accessor functions, as PaulS mentioned:

void setMyVar(bool value){ myVar=value; }

bool getMyVar(){ return myVar; }

This is the best and most robust was way, and allows you to ensure that the input is valid.

If you need the files to share a particular variable you can pass the pointer into the library:

char* p_myVar;

void setMyVar(char* p_value){ p_myVar=p_value; }

//In your sketch:
setMyVar(&myVar);

//to use the value of myVar in the library
doSomething(*p_myVar);

An alternative is to use the extern keyword: http://en.wikipedia.org/wiki/External_variable#Example_.28C_programming_language.29 Though this is not good practice if you are writing a library.

Thank you guys it was very educational! I finally found a way around it. Library has Sleep() and Wakeup() functions, so I was able to disable RF12B with those when SD card was in use. It works now! :) http://www.youtube.com/watch?v=TvmaIdtV16s