编写库为Arduino的 [英] Writing Libraries for Arduino
问题描述
我试图写一个Arduino来解析和跨preT串行命令一个简单的库。我用这个例子库的目标是读预期的命令,打开一些LED灯。我已经得到了串行通信通过Arduino的工作,我希望它由库处理。比如... ...我有以下的code在我的Arduino
I am trying to write a simple library for an Arduino to parse and interpret serial commands. my goal with the example library is to read the expected command and turn on some LEDs. I have gotten serial communications to work via the arduino, and I want it to be handled by a library. For Example...I have the following code on my arduino
Arduino的code:
Arduino Code:
#include <serialComms.h>
serialComms testing = serialComms();
void setup()
{
Serial.begin(9600);
}
void loop() // not terribly concerned with the main loop, only the serialEvent, which I have tested and works
{
}
void serialEvent()
{
testing.readNewBytes();
testing.assignBytes();
}
serialComms.cpp
serialComms.cpp
#include <Arduino.h>
#include <serialComms.h>
void serialComms::init()
{
// This is where the constructor would be...right now we are too stupid to have one
}
void serialComms::readNewBytes() // Target Pin,Values
{
digitalWrite(11,HIGH);
delay(250);
digitalWrite(11,LOW);
assignBytes();
}
void serialComms::assignBytes()
{
for(int t = 0;t<5;t++)
{
digitalWrite(10,HIGH);
delay(250);
digitalWrite(10,LOW);
}
}
serialComms.h
serialComms.h
#ifndef serialComms_h
#define serialComms_h
/* serialComms Class */
class serialComms
{
public:
serialComms() {};
void init();
void readNewBytes(); // Will be used to create the array --> two variables for now...
void assignBytes();
};
#endif
我的问题如下...
My questions are as follows...
1)我是否有库结构合理?我只是想指示灯闪烁时,我发一条消息,并触发的serialEvent,当我在Arduino的运行code,我得到了以下错误。
1.) Do I have the libraries structured properly? I just want the LEDs to blink when I send a message and trigger the serialEvent, When I run the code in arduino I get the following errors.
testingLibraries:2: error: 'serialComms' does not name a type
testingLibraries.ino: In function 'void serialEvent()':
testingLibraries:16: error: 'testing' was not declared in this scope
我有一个文件夹中的图书馆命名的文件夹serialComms中的.cpp和.h文件。我真的不知道在哪里从这里,有什么想法?
I have the .cpp and .h file in a folder named serialComms in the libraries folder. I am not really sure where to go from here, any thoughts?
推荐答案
首先改变你的
#ifndef serialComms
#define serialComms
到
#ifndef serialComms_h
#define serialComms_h
您不能使用相同的名称作为实例的宏。
You can not have a macro with the same name as an instance.
然后检查你的资本如的ReadBytes VS testing.readbytes();注意对B
Then check you capitalization e.g. readBytes vs testing.readbytes(); notice the B
请确保您关闭了所有的Arduino IDE的做一个新的库目录,并在它的初始文件,第一次的时候。在启动时在IDE缓存文件的列表。他们可以随后更改有内部。但新的文件将不会被称为直到下一个开始。
Make SURE you close out all your Arduino IDE's when making a NEW library directory and initial files with in it, for the first time. The IDE on startup caches a list of the files. They can subsequently change there insides. but new files will not be known until the next start.
以下编译罚款我。有一次,我纠正了所有的错字的:
The following compiles fine for me. Once I corrected all the typo's:
definetest.ino
definetest.ino
#include <serialComms.h>
serialComms testing;
void setup() {
Serial.begin(9600);
}
void loop() {
}
void serialEvent()
{
testing.readBytes();
testing.assignBytes();
}
serialComms.cpp
serialComms.cpp
#ifndef serialComms_h
#define serialComms_h
/* serialComms Class */
class serialComms
{
public:
// serialComms() {};
void init();
void readBytes(); // Will be used to create the array --> two variables for now...
void assignBytes();
};
#endif
serialComms.h
serialComms.h
#include <Arduino.h>
#include <serialComms.h>
void serialComms::init()
{
// This is where the constructor would be...right now we are too stupid to have one
}
void serialComms::readBytes() // Target Pin,Values
{
digitalWrite(11,HIGH);
delay(250);
digitalWrite(11,LOW);
assignBytes();
}
void serialComms::assignBytes()
{
for(int t = 0;t<5;t++)
{
digitalWrite(10,HIGH);
delay(250);
digitalWrite(10,LOW);
}
}
这篇关于编写库为Arduino的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!