Как использовать переменные из библиотеки
- Войдите на сайт для отправки комментариев
Сб, 05/10/2013 - 20:30
Первй раз решил написать свою библиотеку. Опыта и так не много, а с библиотеками его вовсе нет. В ниже написанной библиотека работает.
Вопрос: Как использовать в скетче, переменные которые записаны в библиотеке.
Вот так выглядит код в файле 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
Вот код файла 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
}
А вот сам скетч LED13.ino
#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
}
А вот этот код не работает, в нем суть моего вопроса. В библиотеке объявлена boolean KEYCODE_1 = false; ываыа при использовании этой переменной в скетче возникает ошибка: 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
}
}
Либо добавьте в .h определение
(Это - костыли)
Либо сделайте переменную KEYCODE_1 членом класса LED13.