How to use variables from the library

For the first time I need to write a library. Programming experience is not much. Question: How do I use the variables from the library? Here's what I mean:

Code from LED13.h

#ifndef LED13_H
#define LED13_H

#include <Arduino.h> //It is very important to remember this! note that if you are using Arduino 1.0 IDE, change "WProgram.h" to "Arduino.h" 

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

#endif

Code from LED13.cpp

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

const byte LED_PIN = 13; //use the LED @ Arduino pin 13, this should not change so make it const (constant)
boolean A = true;
int BTin;
//int led = 13;
int BTvalue = 0;
int BTcmd = 0;
boolean KEYCODE_1 = false;
boolean KEYCODE_0 = false;

//<<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(){
  if (!KEYCODE_1){
	digitalWrite(LED_PIN,HIGH); //set the pin HIGH and thus turn LED on
  }
}

//turn the LED off
void LED13::off(){
  if (!KEYCODE_1){
	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
}

Main code LED13.ino Here everything is working fine.

#include <LED13.h>

LED13 led;//initialize an instance of the class

void setup(){/*nothing to setup*/}

void loop(){
  led.blink(2000);//stay one second on, then a second off
}

And here is what I need, but it gives an error:: LED13:8: error: 'KEYCODE_1' was not declared in this scope.

#include <LED13.h>

LED13 led;//initialize an instance of the class

void setup(){/*nothing to setup*/}

void loop(){
  if (KEYCODE_1){
  led.blink(2000);//stay one second on, then a second off
  }
}

What do I need to do to be able to use KEYCODE_1 and KEYCODE_0 in the main sketch?

P.S: Sorry for my bad English, the text is written using a translator from Google.

Looks to me like you need to move your variable declarations from the .cpp file to the .h file. They can be public or private but they can be prototypes only. Don't assign values. Assign the values in the default constructor. If I'm understanding some of the stuff I've read you should only have private variables and write a method to access it if you need to read or change it's value. But you can make them public and see them in your sketch.

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

Why aren't they class variables? It seems odd to have a class that tests global variables.

Why make a class just to blink one LED? At least pass the pin number into the constructor then you could use it for any pin.

What is the purpose of these two variables? Although they aren't declared const I haven't noticed any code modifying them. Is it intended as a way for the library to control the behaviour of your sketch? The sketch to control the behaviour of the library? Something else?

As it stands, the variables seem pointless. When we know what they're for we can probably advise the best way to achieve that - and I'm pretty sure that, whatever the purpose turns out to be, the best way to achieve it will not involve using global variables.

I want to write a library for the same program. This program sends data on bluetooth. The library should recognize what data is received. For example, if you press 1, then KEYCODE_1 be true (depressed) when the button 1 is released, came other data, then KEYCODE_1 be false (released). There will be a lot of buttons in the program.

In the main sketch, the user would indicate needed a button. For example:

if (KEYCODE_1){
  digitalWrite(LED_PIN,HIGH);
}
if (!KEYCODE_1){
  digitalWrite(LED_PIN,LOW);
}

Blink was an example

Oh, yes? And what does KEYCODE_0 do?

I tested a few buttons, and forgot to remove KEYCODE_0. Any idea how to do this?

Ok. Here's what I wrote:

BTCA2A.h

#ifndef BTCA2A_H
#define BTCA2A_H

#include <Arduino.h>


class BTCA2A {
  public:
  BTCA2A();
  void CommandCheck();
  
};

#endif

BTCA2A.cpp

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

const byte LED_PIN = 13; //use the LED @ Arduino pin 13, this should not change so make it const (constant)
int BTvalue = 0; // Data from serial port1
int BTcmd = 0; // Received command
boolean KEYCODE_1 = false;
boolean KEYCODE_0 = false;


BTCA2A::BTCA2A(){
    pinMode(LED_PIN, OUTPUT); //make that pin an OUTPUT
}


void BTCA2A::CommandCheck(){

  if (Serial1.available()>0){ 
    BTvalue = Serial1.read(); // What receive from serial port1
    BTcmd = BTcmd + BTvalue; // Calculate the command
    Serial.println(BTvalue); // for debugging
        
    if (BTcmd == 172){ //if button KEYCODE_1 is pressed
      KEYCODE_1 = true;
      Serial.println("KEYCODE_1 = true");
    }
    if (BTcmd == 189){ //if button KEYCODE_1 is depressed
      KEYCODE_1 = false;
      Serial.println("KEYCODE_1 = false");
    }
    if (BTcmd == 171){ //if button KEYCODE_0 is pressed
      KEYCODE_0 = true;
      Serial.println("KEYCODE_0 = true");
    }
    if (BTcmd == 188){ //if button KEYCODE_0 is depressed
      KEYCODE_0 = false;
      Serial.println("KEYCODE_0 = false");
    }
     
    // BTcmd = 0, if the command was accepted
    if (BTvalue == 68 && BTcmd != 68 || BTvalue == 85 && BTcmd != 68){
      Serial.println("------"); // for debugging
      Serial.println(BTcmd);
      Serial.println("------");
      BTcmd = 0;
    }
  }
}

Main code BTCA2A_TEST_05.ino

#include <BTCA2A.h>
BTCA2A BTCA2A;
void setup()  
{
 // Open serial communications to PC and wait for port to open:
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("Connected to PC");
  digitalWrite(13, LOW);
}
 
void loop()
{
  BTCA2A.CommandCheck(); // Check which button is pressed 
     
    if (KEYCODE_1 && !KEYCODE_0){ // If button 1 is pressed & button 0 not pressed 
      digitalWrite(13, HIGH); // LED ON
    }
    if (!KEYCODE_1 && KEYCODE_0){ // If button 0 is pressed & button 1 not pressed 
      digitalWrite(13, LOW); // LED OFF
    }
}

I hawe that:
BTCA2A_TEST_05.ino: In function 'void loop()':
BTCA2A_TEST_05:16: error: 'KEYCODE_1' was not declared in this scope
BTCA2A_TEST_05:16: error: 'KEYCODE_0' was not declared in this scope
BTCA2A_TEST_05:19: error: 'KEYCODE_1' was not declared in this scope
BTCA2A_TEST_05:19: error: 'KEYCODE_0' was not declared in this scope

Someone can help me?

Someone can help me?

The message is telling you exactly what is wrong. If you don't understand scope, then put the Arduino away, and get out the C book. Do some research.

The cpp file, with related header file, is one compilation unit, with one scope. The ino file is another compilation unit, with another scope. The variable is defined in one scope, and you are trying to use it in another scope.

You need to tell the sketch that the variable is defined in another scope, using the extern keyword when you (re)define the variable in the sketch.

As I mentioned above you have to declare variable prototypes in the .h file. Then assign their values in the default constructor. I edited the files from your last example. Your variables are out of scope because they are neither declared properly or accessed with the correct syntax.

#ifndef BTCA2A_H
#define BTCA2A_H

#include <Arduino.h>

class BTCA2A {
  byte LED_PIN; //use the LED @ Arduino pin 13, this should not change so make it const (constant)
  int BTvalue; // Data from serial port1
  int BTcmd; // Received command


public:
  boolean KEYCODE_1;
  boolean KEYCODE_0;
  BTCA2A();
  void CommandCheck();
};

#endif

In the .cpp file I assign the variables values in the default constructor.

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



BTCA2A::BTCA2A(){
  LED_PIN = 13; //use the LED @ Arduino pin 13, this should not change so make it const (constant)
  BTvalue = 0; // Data from serial port1
  BTcmd = 0; // Received command
  KEYCODE_1 = false;
  KEYCODE_0 = false;
  pinMode(LED_PIN, OUTPUT); //make that pin an OUTPUT
}


void BTCA2A::CommandCheck(){

  if (Serial1.available()>0){ 
    BTvalue = Serial1.read(); // What receive from serial port1
    BTcmd = BTcmd + BTvalue; // Calculate the command
    Serial.println(BTvalue); // for debugging

    if (BTcmd == 172){ //if button KEYCODE_1 is pressed
      KEYCODE_1 = true;
      Serial.println("KEYCODE_1 = true");
    }
    if (BTcmd == 189){ //if button KEYCODE_1 is depressed
      KEYCODE_1 = false;
      Serial.println("KEYCODE_1 = false");
    }
    if (BTcmd == 171){ //if button KEYCODE_0 is pressed
      KEYCODE_0 = true;
      Serial.println("KEYCODE_0 = true");
    }
    if (BTcmd == 188){ //if button KEYCODE_0 is depressed
      KEYCODE_0 = false;
      Serial.println("KEYCODE_0 = false");
    }

    // BTcmd = 0, if the command was accepted
    if (BTvalue == 68 && BTcmd != 68 || BTvalue == 85 && BTcmd != 68){
      Serial.println("------"); // for debugging
      Serial.println(BTcmd);
      Serial.println("------");
      BTcmd = 0;
    }
  }
}

To access public variables from an object you have to mention the object name. Same as a struct. Notice the change in syntax where you use the variables.

#include "BTCA2A.h"
BTCA2A BTCA2A;
void setup()  
{
 // Open serial communications to PC and wait for port to open:
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("Connected to PC");
  digitalWrite(13, LOW);
}
 
void loop()
{
  BTCA2A.CommandCheck(); // Check which button is pressed 
     
    if (BTCA2A.KEYCODE_1 && !BTCA2A.KEYCODE_0){ // If button 1 is pressed & button 0 not pressed 
      digitalWrite(13, HIGH); // LED ON
    }
    if (!BTCA2A.KEYCODE_1 && BTCA2A.KEYCODE_0){ // If button 0 is pressed & button 1 not pressed 
      digitalWrite(13, LOW); // LED OFF
    }
}

From what I understand you shouldn't do this. You should have a method return a value rather than access the variable directly. I'd make the CommandCheck method return a value that represents the key positions.

ZSeregaA:
I want to write a library for the same program. This program sends data on bluetooth. The library should recognize what data is received. For example, if you press 1, then KEYCODE_1 be true (depressed) when the button 1 is released, came other data, then KEYCODE_1 be false (released). There will be a lot of buttons in the program.

In the main sketch, the user would indicate needed a button. For example:

if (KEYCODE_1){

digitalWrite(LED_PIN,HIGH);
}
if (!KEYCODE_1){
 digitalWrite(LED_PIN,LOW);
}




Blink was an example

This does not seem like a particularly unusual requirement. Wouldn't it be simpler to have the library return each received keycode using a stream interface? Then your application logic would be almost unchanged, but it would only need to execute when a new keycode was received and would work by testing the value of the received keycode rather than the value of global shared variables.